feat: Adds custom date range component for reports (#2602)

* feat: Adds custom date range for reports

* Review fixes

* Minor fixes

* Review fixes

* Use computed property

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Sivin Varghese 2021-07-13 03:41:03 -07:00 committed by GitHub
parent fae8466a6c
commit f9b55944ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 16 deletions

View file

@ -4,6 +4,7 @@
:range="true" :range="true"
:confirm="true" :confirm="true"
:clearable="false" :clearable="false"
:editable="false"
:confirm-text="confirmText" :confirm-text="confirmText"
:placeholder="placeholder" :placeholder="placeholder"
:value="value" :value="value"
@ -53,5 +54,11 @@ export default {
box-shadow: none; box-shadow: none;
height: 4.6rem; height: 4.6rem;
} }
.mx-input:disabled,
.mx-input[readonly] {
background-color: var(--white);
cursor: pointer;
}
} }
</style> </style>

View file

@ -50,7 +50,15 @@
{ {
"id": 4, "id": 4,
"name": "Last year" "name": "Last year"
},
{
"id": 5,
"name": "Custom date range"
}
],
"CUSTOM_DATE_RANGE": {
"CONFIRM": "Apply",
"PLACEHOLDER": "Select date range"
} }
]
} }
} }

View file

@ -8,6 +8,7 @@
> >
{{ $t('REPORT.DOWNLOAD_AGENT_REPORTS') }} {{ $t('REPORT.DOWNLOAD_AGENT_REPORTS') }}
</woot-button> </woot-button>
<div class="range-selector">
<div class="small-3 pull-right"> <div class="small-3 pull-right">
<multiselect <multiselect
v-model="currentDateRangeSelection" v-model="currentDateRangeSelection"
@ -16,13 +17,21 @@
:placeholder="$t('FORMS.MULTISELECT.SELECT_ONE')" :placeholder="$t('FORMS.MULTISELECT.SELECT_ONE')"
selected-label selected-label
:select-label="$t('FORMS.MULTISELECT.ENTER_TO_SELECT')" :select-label="$t('FORMS.MULTISELECT.ENTER_TO_SELECT')"
:deselect-label="$t('FORMS.MULTISELECT.ENTER_TO_REMOVE')" deselect-label=""
:options="dateRange" :options="dateRange"
:searchable="false" :searchable="false"
:allow-empty="true" :allow-empty="false"
@select="changeDateSelection" @select="changeDateSelection"
/> />
</div> </div>
<woot-date-range-picker
v-if="isDateRangeSelected"
:value="customDateRange"
:confirm-text="$t('REPORT.CUSTOM_DATE_RANGE.CONFIRM')"
:placeholder="$t('REPORT.CUSTOM_DATE_RANGE.PLACEHOLDER')"
@change="onChange"
/>
</div>
<div class="row"> <div class="row">
<woot-report-stats-card <woot-report-stats-card
v-for="(metric, index) in metrics" v-for="(metric, index) in metrics"
@ -57,6 +66,7 @@ import subDays from 'date-fns/subDays';
import getUnixTime from 'date-fns/getUnixTime'; import getUnixTime from 'date-fns/getUnixTime';
import fromUnixTime from 'date-fns/fromUnixTime'; import fromUnixTime from 'date-fns/fromUnixTime';
import format from 'date-fns/format'; import format from 'date-fns/format';
import WootDateRangePicker from 'dashboard/components/ui/DateRangePicker.vue';
const REPORTS_KEYS = { const REPORTS_KEYS = {
CONVERSATIONS: 'conversations_count', CONVERSATIONS: 'conversations_count',
@ -67,12 +77,18 @@ const REPORTS_KEYS = {
RESOLUTION_COUNT: 'resolutions_count', RESOLUTION_COUNT: 'resolutions_count',
}; };
const CUSTOM_DATE_RANGE_ID = 5;
export default { export default {
components: {
WootDateRangePicker,
},
data() { data() {
return { return {
currentSelection: 0, currentSelection: 0,
currentDateRangeSelection: this.$t('REPORT.DATE_RANGE')[0], currentDateRangeSelection: this.$t('REPORT.DATE_RANGE')[0],
dateRange: this.$t('REPORT.DATE_RANGE'), dateRange: this.$t('REPORT.DATE_RANGE'),
customDateRange: [new Date(), new Date()],
}; };
}, },
computed: { computed: {
@ -81,9 +97,15 @@ export default {
accountReport: 'getAccountReports', accountReport: 'getAccountReports',
}), }),
to() { to() {
return getUnixTime(startOfDay(new Date())); if (this.isDateRangeSelected) {
return this.fromCustomDate(this.customDateRange[1]);
}
return this.fromCustomDate(new Date());
}, },
from() { from() {
if (this.isDateRangeSelected) {
return this.fromCustomDate(this.customDateRange[0]);
}
const dateRange = { const dateRange = {
0: 6, 0: 6,
1: 29, 1: 29,
@ -93,7 +115,7 @@ export default {
}; };
const diff = dateRange[this.currentDateRangeSelection.id]; const diff = dateRange[this.currentDateRangeSelection.id];
const fromDate = subDays(new Date(), diff); const fromDate = subDays(new Date(), diff);
return getUnixTime(startOfDay(fromDate)); return this.fromCustomDate(fromDate);
}, },
collection() { collection() {
if (this.accountReport.isFetching) { if (this.accountReport.isFetching) {
@ -130,6 +152,9 @@ export default {
DESC: this.$t(`REPORT.METRICS.${key}.DESC`), DESC: this.$t(`REPORT.METRICS.${key}.DESC`),
})); }));
}, },
isDateRangeSelected() {
return this.currentDateRangeSelection.id === CUSTOM_DATE_RANGE_ID;
},
}, },
mounted() { mounted() {
this.fetchAllData(); this.fetchAllData();
@ -170,6 +195,18 @@ export default {
to, to,
}); });
}, },
fromCustomDate(date) {
return getUnixTime(startOfDay(date));
},
onChange(value) {
this.customDateRange = value;
this.fetchAllData();
},
}, },
}; };
</script> </script>
<style lang="scss" scoped>
.range-selector {
display: flex;
}
</style>