Sukanya Pandey | 34efde2 | 2020-12-02 19:04:09 +0530 | [diff] [blame] | 1 | <template> |
| 2 | <b-container fluid="xl"> |
| 3 | <page-title /> |
| 4 | <b-row class="align-items-end"> |
| 5 | <b-col sm="6" md="5" xl="4"> |
| 6 | <search |
| 7 | :placeholder="$t('pageClientSessions.table.searchSessions')" |
| 8 | @change-search="onChangeSearchInput" |
| 9 | @clear-search="onClearSearchInput" |
| 10 | /> |
| 11 | </b-col> |
| 12 | <b-col sm="3" md="3" xl="2"> |
| 13 | <table-cell-count |
| 14 | :filtered-items-count="filteredRows" |
| 15 | :total-number-of-cells="allConnections.length" |
| 16 | ></table-cell-count> |
| 17 | </b-col> |
| 18 | </b-row> |
| 19 | <b-row> |
| 20 | <b-col> |
| 21 | <table-toolbar |
| 22 | ref="toolbar" |
| 23 | :selected-items-count="selectedRows.length" |
| 24 | :actions="batchActions" |
| 25 | @clear-selected="clearSelectedRows($refs.table)" |
| 26 | @batch-action="onBatchAction" |
| 27 | > |
| 28 | </table-toolbar> |
| 29 | <b-table |
| 30 | id="table-session-logs" |
| 31 | ref="table" |
| 32 | responsive="md" |
| 33 | selectable |
| 34 | no-select-on-click |
| 35 | hover |
| 36 | show-empty |
| 37 | sort-by="clientID" |
| 38 | :fields="fields" |
| 39 | :items="allConnections" |
| 40 | :filter="searchFilter" |
| 41 | :empty-text="$t('global.table.emptyMessage')" |
| 42 | :per-page="perPage" |
| 43 | :current-page="currentPage" |
| 44 | @filtered="onFiltered" |
| 45 | @row-selected="onRowSelected($event, allConnections.length)" |
| 46 | > |
| 47 | <!-- Checkbox column --> |
| 48 | <template #head(checkbox)> |
| 49 | <b-form-checkbox |
| 50 | v-model="tableHeaderCheckboxModel" |
| 51 | data-test-id="sessionLogs-checkbox-selectAll" |
| 52 | :indeterminate="tableHeaderCheckboxIndeterminate" |
| 53 | @change="onChangeHeaderCheckbox($refs.table)" |
| 54 | > |
| 55 | <span class="sr-only">{{ $t('global.table.selectAll') }}</span> |
| 56 | </b-form-checkbox> |
| 57 | </template> |
| 58 | <template #cell(checkbox)="row"> |
| 59 | <b-form-checkbox |
| 60 | v-model="row.rowSelected" |
| 61 | :data-test-id="`sessionLogs-checkbox-selectRow-${row.index}`" |
| 62 | @change="toggleSelectRow($refs.table, row.index)" |
| 63 | > |
| 64 | <span class="sr-only">{{ $t('global.table.selectItem') }}</span> |
| 65 | </b-form-checkbox> |
| 66 | </template> |
| 67 | |
| 68 | <!-- Actions column --> |
| 69 | <template #cell(actions)="row" class="ml-3"> |
| 70 | <table-row-action |
| 71 | v-for="(action, index) in row.item.actions" |
| 72 | :key="index" |
| 73 | :value="action.value" |
| 74 | :title="action.title" |
| 75 | :row-data="row.item" |
| 76 | :data-test-id="`sessionLogs-button-deleteRow-${row.index}`" |
| 77 | @click-table-action="onTableRowAction($event, row.item)" |
| 78 | ></table-row-action> |
| 79 | </template> |
| 80 | </b-table> |
| 81 | </b-col> |
| 82 | </b-row> |
| 83 | |
| 84 | <!-- Table pagination --> |
| 85 | <b-row> |
| 86 | <b-col sm="6"> |
| 87 | <b-form-group |
| 88 | class="table-pagination-select" |
| 89 | :label="$t('global.table.itemsPerPage')" |
| 90 | label-for="pagination-items-per-page" |
| 91 | > |
| 92 | <b-form-select |
| 93 | id="pagination-items-per-page" |
| 94 | v-model="perPage" |
| 95 | :options="itemsPerPageOptions" |
| 96 | /> |
| 97 | </b-form-group> |
| 98 | </b-col> |
| 99 | <b-col sm="6"> |
| 100 | <b-pagination |
| 101 | v-model="currentPage" |
| 102 | first-number |
| 103 | last-number |
| 104 | :per-page="perPage" |
| 105 | :total-rows="getTotalRowCount(allConnections.length)" |
| 106 | aria-controls="table-session-logs" |
| 107 | /> |
| 108 | </b-col> |
| 109 | </b-row> |
| 110 | </b-container> |
| 111 | </template> |
| 112 | |
| 113 | <script> |
| 114 | import PageTitle from '@/components/Global/PageTitle'; |
| 115 | import Search from '@/components/Global/Search'; |
| 116 | import TableCellCount from '@/components/Global/TableCellCount'; |
| 117 | import TableRowAction from '@/components/Global/TableRowAction'; |
| 118 | import TableToolbar from '@/components/Global/TableToolbar'; |
| 119 | |
| 120 | import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin'; |
| 121 | import BVPaginationMixin, { |
| 122 | currentPage, |
| 123 | perPage, |
| 124 | itemsPerPageOptions, |
| 125 | } from '@/components/Mixins/BVPaginationMixin'; |
| 126 | import BVTableSelectableMixin, { |
| 127 | selectedRows, |
| 128 | tableHeaderCheckboxModel, |
| 129 | tableHeaderCheckboxIndeterminate, |
| 130 | } from '@/components/Mixins/BVTableSelectableMixin'; |
| 131 | import BVToastMixin from '@/components/Mixins/BVToastMixin'; |
| 132 | import SearchFilterMixin, { |
| 133 | searchFilter, |
| 134 | } from '@/components/Mixins/SearchFilterMixin'; |
| 135 | |
| 136 | export default { |
| 137 | components: { |
| 138 | PageTitle, |
| 139 | Search, |
| 140 | TableCellCount, |
| 141 | TableRowAction, |
| 142 | TableToolbar, |
| 143 | }, |
| 144 | mixins: [ |
| 145 | BVPaginationMixin, |
| 146 | BVTableSelectableMixin, |
| 147 | BVToastMixin, |
| 148 | LoadingBarMixin, |
| 149 | SearchFilterMixin, |
| 150 | ], |
| 151 | beforeRouteLeave(to, from, next) { |
| 152 | // Hide loader if the user navigates to another page |
| 153 | // before request is fulfilled. |
| 154 | this.hideLoader(); |
| 155 | next(); |
| 156 | }, |
| 157 | data() { |
| 158 | return { |
| 159 | fields: [ |
| 160 | { |
| 161 | key: 'checkbox', |
| 162 | }, |
| 163 | { |
| 164 | key: 'clientID', |
| 165 | label: this.$t('pageClientSessions.table.clientID'), |
| 166 | }, |
| 167 | { |
| 168 | key: 'username', |
| 169 | label: this.$t('pageClientSessions.table.username'), |
| 170 | }, |
| 171 | { |
| 172 | key: 'ipAddress', |
| 173 | label: this.$t('pageClientSessions.table.ipAddress'), |
| 174 | }, |
| 175 | { |
| 176 | key: 'actions', |
| 177 | label: '', |
| 178 | }, |
| 179 | ], |
| 180 | batchActions: [ |
| 181 | { |
| 182 | value: 'disconnect', |
| 183 | label: this.$t('pageClientSessions.action.disconnect'), |
| 184 | }, |
| 185 | ], |
| 186 | currentPage: currentPage, |
| 187 | itemsPerPageOptions: itemsPerPageOptions, |
| 188 | perPage: perPage, |
| 189 | selectedRows: selectedRows, |
| 190 | searchTotalFilteredRows: 0, |
| 191 | tableHeaderCheckboxModel: tableHeaderCheckboxModel, |
| 192 | tableHeaderCheckboxIndeterminate: tableHeaderCheckboxIndeterminate, |
| 193 | searchFilter: searchFilter, |
| 194 | }; |
| 195 | }, |
| 196 | computed: { |
| 197 | filteredRows() { |
| 198 | return this.searchFilter |
| 199 | ? this.searchTotalFilteredRows |
| 200 | : this.allConnections.length; |
| 201 | }, |
| 202 | allConnections() { |
| 203 | return this.$store.getters['clientSessions/allConnections'].map( |
| 204 | (session) => { |
| 205 | return { |
| 206 | ...session, |
| 207 | actions: [ |
| 208 | { |
| 209 | value: 'disconnect', |
| 210 | title: this.$t('pageClientSessions.action.disconnect'), |
| 211 | }, |
| 212 | ], |
| 213 | }; |
| 214 | } |
| 215 | ); |
| 216 | }, |
| 217 | }, |
| 218 | created() { |
| 219 | this.startLoader(); |
| 220 | this.$store |
| 221 | .dispatch('clientSessions/getClientSessionsData') |
| 222 | .finally(() => this.endLoader()); |
| 223 | }, |
| 224 | methods: { |
| 225 | onFiltered(filteredItems) { |
| 226 | this.searchTotalFilteredRows = filteredItems.length; |
| 227 | }, |
| 228 | onChangeSearchInput(event) { |
| 229 | this.searchFilter = event; |
| 230 | }, |
| 231 | disconnectSessions(uris) { |
| 232 | this.$store |
| 233 | .dispatch('clientSessions/disconnectSessions', uris) |
| 234 | .then((messages) => { |
| 235 | messages.forEach(({ type, message }) => { |
| 236 | if (type === 'success') { |
| 237 | this.successToast(message); |
| 238 | } else if (type === 'error') { |
| 239 | this.errorToast(message); |
| 240 | } |
| 241 | }); |
| 242 | }); |
| 243 | }, |
| 244 | onTableRowAction(action, { uri }) { |
| 245 | if (action === 'disconnect') { |
| 246 | this.$bvModal |
| 247 | .msgBoxConfirm( |
| 248 | this.$tc('pageClientSessions.modal.disconnectMessage'), |
| 249 | { |
| 250 | title: this.$tc('pageClientSessions.modal.disconnectTitle'), |
| 251 | okTitle: this.$t('pageClientSessions.action.disconnect'), |
Sukanya Pandey | 3835713 | 2020-12-22 13:40:59 +0530 | [diff] [blame^] | 252 | cancelTitle: this.$t('global.action.cancel'), |
Sukanya Pandey | 34efde2 | 2020-12-02 19:04:09 +0530 | [diff] [blame] | 253 | } |
| 254 | ) |
| 255 | .then((deleteConfirmed) => { |
| 256 | if (deleteConfirmed) this.disconnectSessions([uri]); |
| 257 | }); |
| 258 | } |
| 259 | }, |
| 260 | onBatchAction(action) { |
| 261 | if (action === 'disconnect') { |
| 262 | const uris = this.selectedRows.map((row) => row.uri); |
| 263 | this.$bvModal |
| 264 | .msgBoxConfirm( |
| 265 | this.$tc( |
| 266 | 'pageClientSessions.modal.disconnectMessage', |
| 267 | this.selectedRows.length |
| 268 | ), |
| 269 | { |
| 270 | title: this.$tc( |
| 271 | 'pageClientSessions.modal.disconnectTitle', |
| 272 | this.selectedRows.length |
| 273 | ), |
| 274 | okTitle: this.$t('pageClientSessions.action.disconnect'), |
Sukanya Pandey | 3835713 | 2020-12-22 13:40:59 +0530 | [diff] [blame^] | 275 | cancelTitle: this.$t('global.action.cancel'), |
Sukanya Pandey | 34efde2 | 2020-12-02 19:04:09 +0530 | [diff] [blame] | 276 | } |
| 277 | ) |
| 278 | .then((deleteConfirmed) => { |
| 279 | if (deleteConfirmed) { |
| 280 | this.disconnectSessions(uris); |
| 281 | } |
| 282 | }); |
| 283 | } |
| 284 | }, |
| 285 | }, |
| 286 | }; |
| 287 | </script> |
| 288 | <style lang="scss"> |
| 289 | #table-session-logs { |
| 290 | td .btn-link { |
| 291 | width: auto !important; |
| 292 | } |
| 293 | } |
| 294 | </style> |