blob: a6892fb5ad47dd6c91a5c98547db0d848af16cd3 [file] [log] [blame]
Sandeepa Singh06d53862021-05-24 13:51:09 +05301<template>
2 <b-container fluid="xl">
3 <page-title />
4 <b-row class="align-items-start">
5 <b-col sm="8" xl="6" class="d-sm-flex align-items-end mb-4">
6 <search
7 :placeholder="$t('pagePostCodeLogs.table.searchLogs')"
8 @change-search="onChangeSearchInput"
9 @clear-search="onClearSearchInput"
10 />
11 <div class="ml-sm-4">
12 <table-cell-count
13 :filtered-items-count="filteredRows"
14 :total-number-of-cells="allLogs.length"
15 ></table-cell-count>
16 </div>
17 </b-col>
18 <b-col sm="8" md="7" xl="6">
19 <table-date-filter @change="onChangeDateTimeFilter" />
20 </b-col>
21 </b-row>
22 <b-row>
23 <b-col xl="12" class="text-right">
24 <b-button
25 variant="primary"
26 :disabled="allLogs.length === 0"
27 :download="exportFileNameByDate()"
28 :href="href"
29 >
30 <icon-export /> {{ $t('pagePostCodeLogs.button.exportAll') }}
31 </b-button>
32 </b-col>
33 </b-row>
34 <b-row>
35 <b-col>
36 <table-toolbar
37 ref="toolbar"
38 :selected-items-count="selectedRows.length"
39 @clear-selected="clearSelectedRows($refs.table)"
40 >
Sandeepa Singhb8182882021-08-27 13:20:11 +053041 <template #toolbar-buttons>
Sandeepa Singh06d53862021-05-24 13:51:09 +053042 <table-toolbar-export
43 :data="batchExportData"
Sandeepa Singhb8182882021-08-27 13:20:11 +053044 :file-name="exportFileNameByDate()"
Sandeepa Singh06d53862021-05-24 13:51:09 +053045 />
46 </template>
47 </table-toolbar>
48 <b-table
49 id="table-post-code-logs"
50 ref="table"
51 responsive="md"
52 selectable
53 no-select-on-click
54 sort-icon-left
55 hover
56 no-sort-reset
57 sort-desc
58 show-empty
59 sort-by="id"
60 :fields="fields"
61 :items="filteredLogs"
62 :empty-text="$t('global.table.emptyMessage')"
63 :empty-filtered-text="$t('global.table.emptySearchMessage')"
64 :per-page="perPage"
65 :current-page="currentPage"
66 :filter="searchFilter"
67 @filtered="onFiltered"
68 @row-selected="onRowSelected($event, filteredLogs.length)"
69 >
70 <!-- Checkbox column -->
71 <template #head(checkbox)>
72 <b-form-checkbox
73 v-model="tableHeaderCheckboxModel"
74 data-test-id="postCode-checkbox-selectAll"
75 :indeterminate="tableHeaderCheckboxIndeterminate"
76 @change="onChangeHeaderCheckbox($refs.table)"
77 >
78 <span class="sr-only">{{ $t('global.table.selectAll') }}</span>
79 </b-form-checkbox>
80 </template>
81 <template #cell(checkbox)="row">
82 <b-form-checkbox
83 v-model="row.rowSelected"
84 :data-test-id="`postCode-checkbox-selectRow-${row.index}`"
85 @change="toggleSelectRow($refs.table, row.index)"
86 >
87 <span class="sr-only">{{ $t('global.table.selectItem') }}</span>
88 </b-form-checkbox>
89 </template>
90 <!-- Date column -->
91 <template #cell(date)="{ value }">
92 <p class="mb-0">{{ value | formatDate }}</p>
93 <p class="mb-0">{{ value | formatTime }}</p>
94 </template>
95
96 <!-- Actions column -->
97 <template #cell(actions)="row">
98 <table-row-action
99 v-for="(action, index) in row.item.actions"
100 :key="index"
101 :value="action.value"
102 :title="action.title"
103 :row-data="row.item"
104 :btn-icon-only="true"
105 :export-name="exportFileNameByDate(action.value)"
106 :download-location="row.item.uri"
107 :download-in-new-tab="true"
Sandeepa Singh450bdb02021-08-05 15:51:56 +0530108 :show-button="false"
Sandeepa Singh06d53862021-05-24 13:51:09 +0530109 >
110 <template #icon>
111 <icon-export v-if="action.value === 'export'" />
112 <icon-download v-if="action.value === 'download'" />
113 </template>
114 </table-row-action>
115 </template>
116 </b-table>
117 </b-col>
118 </b-row>
119
120 <!-- Table pagination -->
121 <b-row>
122 <b-col sm="6">
123 <b-form-group
124 class="table-pagination-select"
125 :label="$t('global.table.itemsPerPage')"
126 label-for="pagination-items-per-page"
127 >
128 <b-form-select
129 id="pagination-items-per-page"
130 v-model="perPage"
131 :options="itemsPerPageOptions"
132 />
133 </b-form-group>
134 </b-col>
135 <b-col sm="6">
136 <b-pagination
137 v-model="currentPage"
138 first-number
139 last-number
140 :per-page="perPage"
141 :total-rows="getTotalRowCount(filteredLogs.length)"
142 aria-controls="table-post-code-logs"
143 />
144 </b-col>
145 </b-row>
146 </b-container>
147</template>
148
149<script>
150import IconDownload from '@carbon/icons-vue/es/download/20';
151import IconExport from '@carbon/icons-vue/es/document--export/20';
152import { omit } from 'lodash';
153import PageTitle from '@/components/Global/PageTitle';
154import Search from '@/components/Global/Search';
155import TableCellCount from '@/components/Global/TableCellCount';
156import TableDateFilter from '@/components/Global/TableDateFilter';
157import TableRowAction from '@/components/Global/TableRowAction';
158import TableToolbar from '@/components/Global/TableToolbar';
159import TableToolbarExport from '@/components/Global/TableToolbarExport';
160import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
161import TableFilterMixin from '@/components/Mixins/TableFilterMixin';
162import BVPaginationMixin, {
163 currentPage,
164 perPage,
165 itemsPerPageOptions,
166} from '@/components/Mixins/BVPaginationMixin';
167import BVTableSelectableMixin, {
168 selectedRows,
169 tableHeaderCheckboxModel,
170 tableHeaderCheckboxIndeterminate,
171} from '@/components/Mixins/BVTableSelectableMixin';
172import BVToastMixin from '@/components/Mixins/BVToastMixin';
173import TableDataFormatterMixin from '@/components/Mixins/TableDataFormatterMixin';
174import TableSortMixin from '@/components/Mixins/TableSortMixin';
175import TableRowExpandMixin, {
176 expandRowLabel,
177} from '@/components/Mixins/TableRowExpandMixin';
178import SearchFilterMixin, {
179 searchFilter,
180} from '@/components/Mixins/SearchFilterMixin';
181
182export default {
183 components: {
184 IconExport,
185 IconDownload,
186 PageTitle,
187 Search,
188 TableCellCount,
189 TableRowAction,
190 TableToolbar,
191 TableToolbarExport,
192 TableDateFilter,
193 },
194 mixins: [
195 BVPaginationMixin,
196 BVTableSelectableMixin,
197 BVToastMixin,
198 LoadingBarMixin,
199 TableFilterMixin,
200 TableDataFormatterMixin,
201 TableSortMixin,
202 TableRowExpandMixin,
203 SearchFilterMixin,
204 ],
205 beforeRouteLeave(to, from, next) {
206 // Hide loader if the user navigates to another page
207 // before request is fulfilled.
208 this.hideLoader();
209 next();
210 },
211 data() {
212 return {
213 fields: [
214 {
215 key: 'checkbox',
216 sortable: false,
217 },
218 {
219 key: 'date',
220 label: this.$t('pagePostCodeLogs.table.created'),
Sandeepa Singhb8182882021-08-27 13:20:11 +0530221 sortable: true,
Sandeepa Singh06d53862021-05-24 13:51:09 +0530222 },
223 {
224 key: 'timeStampOffset',
225 label: this.$t('pagePostCodeLogs.table.timeStampOffset'),
226 },
227 {
228 key: 'bootCount',
229 label: this.$t('pagePostCodeLogs.table.bootCount'),
230 },
231 {
232 key: 'postCode',
233 label: this.$t('pagePostCodeLogs.table.postCode'),
234 },
235 {
236 key: 'actions',
237 label: '',
238 tdClass: 'text-right text-nowrap',
239 },
240 ],
241 expandRowLabel,
242 activeFilters: [],
243 currentPage: currentPage,
244 filterStartDate: null,
245 filterEndDate: null,
246 itemsPerPageOptions: itemsPerPageOptions,
247 perPage: perPage,
248 searchFilter: searchFilter,
249 searchTotalFilteredRows: 0,
250 selectedRows: selectedRows,
251 tableHeaderCheckboxModel: tableHeaderCheckboxModel,
252 tableHeaderCheckboxIndeterminate: tableHeaderCheckboxIndeterminate,
253 };
254 },
255 computed: {
256 href() {
257 return `data:text/json;charset=utf-8,${this.exportAllLogsString()}`;
258 },
259 filteredRows() {
260 return this.searchFilter
261 ? this.searchTotalFilteredRows
262 : this.filteredLogs.length;
263 },
264 allLogs() {
265 return this.$store.getters['postCodeLogs/allPostCodes'].map(
266 (postCodes) => {
267 return {
268 ...postCodes,
269 actions: [
270 {
271 value: 'export',
272 title: this.$t('pagePostCodeLogs.action.exportLogs'),
273 },
274 {
275 value: 'download',
276 title: this.$t('pagePostCodeLogs.action.downloadDetails'),
277 },
278 ],
279 };
280 }
281 );
282 },
283 batchExportData() {
284 return this.selectedRows.map((row) => omit(row, 'actions'));
285 },
286 filteredLogsByDate() {
287 return this.getFilteredTableDataByDate(
288 this.allLogs,
289 this.filterStartDate,
290 this.filterEndDate
291 );
292 },
293 filteredLogs() {
294 return this.getFilteredTableData(
295 this.filteredLogsByDate,
296 this.activeFilters
297 );
298 },
299 },
300 created() {
301 this.startLoader();
302 this.$store
303 .dispatch('postCodeLogs/getPostCodesLogData')
304 .finally(() => this.endLoader());
305 },
306 methods: {
307 exportAllLogsString() {
308 {
309 return this.$store.getters['postCodeLogs/allPostCodes'].map(
310 (postCodes) => {
311 const allLogsString = JSON.stringify(postCodes);
312 return allLogsString;
313 }
314 );
315 }
316 },
317 onFilterChange({ activeFilters }) {
318 this.activeFilters = activeFilters;
319 },
320 onChangeDateTimeFilter({ fromDate, toDate }) {
321 this.filterStartDate = fromDate;
322 this.filterEndDate = toDate;
323 },
324 onFiltered(filteredItems) {
325 this.searchTotalFilteredRows = filteredItems.length;
326 },
327 // Create export file name based on date and action
328 exportFileNameByDate(value) {
329 let date = new Date();
330 date =
331 date.toISOString().slice(0, 10) +
332 '_' +
333 date.toString().split(':').join('-').split(' ')[4];
334 let fileName;
335 if (value === 'download') {
336 fileName = this.$t('pagePostCodeLogs.downloadFilePrefix');
337 } else if (value === 'export') {
338 fileName = this.$t('pagePostCodeLogs.exportFilePrefix');
339 } else {
340 fileName = this.$t('pagePostCodeLogs.allExportFilePrefix');
341 }
342 return fileName + date;
343 },
344 },
345};
346</script>