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 #export>
42            <table-toolbar-export
43              :data="batchExportData"
44              :file-name="exportFileNameByDate('export')"
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            >
109              <template #icon>
110                <icon-export v-if="action.value === 'export'" />
111                <icon-download v-if="action.value === 'download'" />
112              </template>
113            </table-row-action>
114          </template>
115        </b-table>
116      </b-col>
117    </b-row>
118
119    <!-- Table pagination -->
120    <b-row>
121      <b-col sm="6">
122        <b-form-group
123          class="table-pagination-select"
124          :label="$t('global.table.itemsPerPage')"
125          label-for="pagination-items-per-page"
126        >
127          <b-form-select
128            id="pagination-items-per-page"
129            v-model="perPage"
130            :options="itemsPerPageOptions"
131          />
132        </b-form-group>
133      </b-col>
134      <b-col sm="6">
135        <b-pagination
136          v-model="currentPage"
137          first-number
138          last-number
139          :per-page="perPage"
140          :total-rows="getTotalRowCount(filteredLogs.length)"
141          aria-controls="table-post-code-logs"
142        />
143      </b-col>
144    </b-row>
145  </b-container>
146</template>
147
148<script>
149import IconDownload from '@carbon/icons-vue/es/download/20';
150import IconExport from '@carbon/icons-vue/es/document--export/20';
151import { omit } from 'lodash';
152import PageTitle from '@/components/Global/PageTitle';
153import Search from '@/components/Global/Search';
154import TableCellCount from '@/components/Global/TableCellCount';
155import TableDateFilter from '@/components/Global/TableDateFilter';
156import TableRowAction from '@/components/Global/TableRowAction';
157import TableToolbar from '@/components/Global/TableToolbar';
158import TableToolbarExport from '@/components/Global/TableToolbarExport';
159import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
160import TableFilterMixin from '@/components/Mixins/TableFilterMixin';
161import BVPaginationMixin, {
162  currentPage,
163  perPage,
164  itemsPerPageOptions,
165} from '@/components/Mixins/BVPaginationMixin';
166import BVTableSelectableMixin, {
167  selectedRows,
168  tableHeaderCheckboxModel,
169  tableHeaderCheckboxIndeterminate,
170} from '@/components/Mixins/BVTableSelectableMixin';
171import BVToastMixin from '@/components/Mixins/BVToastMixin';
172import TableDataFormatterMixin from '@/components/Mixins/TableDataFormatterMixin';
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    TableDataFormatterMixin,
200    TableSortMixin,
201    TableRowExpandMixin,
202    SearchFilterMixin,
203  ],
204  beforeRouteLeave(to, from, next) {
205    // Hide loader if the user navigates to another page
206    // before request is fulfilled.
207    this.hideLoader();
208    next();
209  },
210  data() {
211    return {
212      fields: [
213        {
214          key: 'checkbox',
215          sortable: false,
216        },
217        {
218          key: 'date',
219          label: this.$t('pagePostCodeLogs.table.created'),
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