1<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        >
41          <template #toolbar-buttons>
42            <table-toolbar-export
43              :data="batchExportData"
44              :file-name="exportFileNameByDate()"
45            />
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"
108              :show-button="false"
109            >
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(filteredRows)"
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 TableSortMixin from '@/components/Mixins/TableSortMixin';
174import TableRowExpandMixin, {
175  expandRowLabel,
176} from '@/components/Mixins/TableRowExpandMixin';
177import SearchFilterMixin, {
178  searchFilter,
179} from '@/components/Mixins/SearchFilterMixin';
180
181export default {
182  components: {
183    IconExport,
184    IconDownload,
185    PageTitle,
186    Search,
187    TableCellCount,
188    TableRowAction,
189    TableToolbar,
190    TableToolbarExport,
191    TableDateFilter,
192  },
193  mixins: [
194    BVPaginationMixin,
195    BVTableSelectableMixin,
196    BVToastMixin,
197    LoadingBarMixin,
198    TableFilterMixin,
199    TableSortMixin,
200    TableRowExpandMixin,
201    SearchFilterMixin,
202  ],
203  beforeRouteLeave(to, from, next) {
204    // Hide loader if the user navigates to another page
205    // before request is fulfilled.
206    this.hideLoader();
207    next();
208  },
209  data() {
210    return {
211      fields: [
212        {
213          key: 'checkbox',
214          sortable: false,
215        },
216        {
217          key: 'date',
218          label: this.$t('pagePostCodeLogs.table.created'),
219          sortable: true,
220        },
221        {
222          key: 'timeStampOffset',
223          label: this.$t('pagePostCodeLogs.table.timeStampOffset'),
224        },
225        {
226          key: 'bootCount',
227          label: this.$t('pagePostCodeLogs.table.bootCount'),
228        },
229        {
230          key: 'postCode',
231          label: this.$t('pagePostCodeLogs.table.postCode'),
232        },
233        {
234          key: 'actions',
235          label: '',
236          tdClass: 'text-right text-nowrap',
237        },
238      ],
239      expandRowLabel,
240      activeFilters: [],
241      currentPage: currentPage,
242      filterStartDate: null,
243      filterEndDate: null,
244      itemsPerPageOptions: itemsPerPageOptions,
245      perPage: perPage,
246      searchFilter: searchFilter,
247      searchTotalFilteredRows: 0,
248      selectedRows: selectedRows,
249      tableHeaderCheckboxModel: tableHeaderCheckboxModel,
250      tableHeaderCheckboxIndeterminate: tableHeaderCheckboxIndeterminate,
251    };
252  },
253  computed: {
254    href() {
255      return `data:text/json;charset=utf-8,${this.exportAllLogsString()}`;
256    },
257    filteredRows() {
258      return this.searchFilter
259        ? this.searchTotalFilteredRows
260        : this.filteredLogs.length;
261    },
262    allLogs() {
263      return this.$store.getters['postCodeLogs/allPostCodes'].map(
264        (postCodes) => {
265          return {
266            ...postCodes,
267            actions: [
268              {
269                value: 'export',
270                title: this.$t('pagePostCodeLogs.action.exportLogs'),
271              },
272              {
273                value: 'download',
274                title: this.$t('pagePostCodeLogs.action.downloadDetails'),
275              },
276            ],
277          };
278        }
279      );
280    },
281    batchExportData() {
282      return this.selectedRows.map((row) => omit(row, 'actions'));
283    },
284    filteredLogsByDate() {
285      return this.getFilteredTableDataByDate(
286        this.allLogs,
287        this.filterStartDate,
288        this.filterEndDate
289      );
290    },
291    filteredLogs() {
292      return this.getFilteredTableData(
293        this.filteredLogsByDate,
294        this.activeFilters
295      );
296    },
297  },
298  created() {
299    this.startLoader();
300    this.$store
301      .dispatch('postCodeLogs/getPostCodesLogData')
302      .finally(() => this.endLoader());
303  },
304  methods: {
305    exportAllLogsString() {
306      {
307        return this.$store.getters['postCodeLogs/allPostCodes'].map(
308          (postCodes) => {
309            const allLogsString = JSON.stringify(postCodes);
310            return allLogsString;
311          }
312        );
313      }
314    },
315    onFilterChange({ activeFilters }) {
316      this.activeFilters = activeFilters;
317    },
318    onChangeDateTimeFilter({ fromDate, toDate }) {
319      this.filterStartDate = fromDate;
320      this.filterEndDate = toDate;
321    },
322    onFiltered(filteredItems) {
323      this.searchTotalFilteredRows = filteredItems.length;
324    },
325    // Create export file name based on date and action
326    exportFileNameByDate(value) {
327      let date = new Date();
328      date =
329        date.toISOString().slice(0, 10) +
330        '_' +
331        date.toString().split(':').join('-').split(' ')[4];
332      let fileName;
333      if (value === 'download') {
334        fileName = this.$t('pagePostCodeLogs.downloadFilePrefix');
335      } else if (value === 'export') {
336        fileName = this.$t('pagePostCodeLogs.exportFilePrefix');
337      } else {
338        fileName = this.$t('pagePostCodeLogs.allExportFilePrefix');
339      }
340      return fileName + date;
341    },
342  },
343};
344</script>
345