-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReviewCleanupDialog.vue
More file actions
330 lines (286 loc) · 8.94 KB
/
ReviewCleanupDialog.vue
File metadata and controls
330 lines (286 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<script setup lang="ts">
import { BFormCheckbox, BPagination } from "bootstrap-vue";
import { computed, ref, watch } from "vue";
import type { TableField } from "@/components/Common/GTable.types";
import localize from "@/utils/localization";
import { bytesToString } from "@/utils/utils";
import { type CleanableItem, type CleanupOperation, PaginationOptions, type SortableKey } from "./model";
import GButton from "@/components/BaseComponents/GButton.vue";
import GModal from "@/components/BaseComponents/GModal.vue";
import GTable from "@/components/Common/GTable.vue";
import Heading from "@/components/Common/Heading.vue";
import UtcDate from "@/components/UtcDate.vue";
interface ReviewCleanupDialogProps {
operation?: CleanupOperation;
totalItems?: number;
}
const props = withDefaults(defineProps<ReviewCleanupDialogProps>(), {
operation: undefined,
totalItems: 0,
});
const emit = defineEmits<{
(event: "onConfirmCleanupSelectedItems", items: CleanableItem[]): void;
}>();
const permanentlyDeleteText = localize("Permanently delete");
const captionText = localize("To free up account space, review and select items to be permanently deleted here.");
const agreementText = localize("I understand that once I delete the items, they cannot be recovered.");
const fields: TableField[] = [
{
key: "selected",
label: "",
sortable: false,
},
{
key: "name",
label: localize("Name"),
sortable: true,
},
{
key: "size",
label: localize("Size"),
sortable: true,
formatter: toNiceSize,
},
{
key: "update_time",
label: localize("Updated"),
sortable: true,
},
];
const MAXIMUM_ITEMS_PER_PAGE = 25;
const sortBy = ref<SortableKey>("size");
const sortDesc = ref(true);
const currentPage = ref(1);
const totalRows = ref(1);
const allSelected = ref(false);
const indeterminate = ref(false);
const showDialog = ref(false);
const items = ref<CleanableItem[]>([]);
const selectedItems = ref<CleanableItem[]>([]);
const confirmChecked = ref(false);
const isBusy = ref(false);
const openConfirmationModal = ref(false);
const selectedItemCount = computed(() => {
return selectedItems.value.length;
});
const hasItemsSelected = computed(() => {
return selectedItemCount.value > 0;
});
const hasPages = computed(() => {
return totalRows.value > MAXIMUM_ITEMS_PER_PAGE;
});
const title = computed(() => {
return props.operation?.name ?? "";
});
const confirmationTitle = computed(() => {
return `Permanently delete ${selectedItemCount.value} item${selectedItemCount.value > 1 ? "s" : ""}?`;
});
const deleteItemsText = computed(() => {
return hasItemsSelected.value ? `${selectedItemCount.value} item${selectedItemCount.value > 1 ? "s" : ""}` : "";
});
async function loadItems() {
if (!props.operation) {
items.value = [];
return;
}
try {
isBusy.value = true;
const page = currentPage.value > 0 ? currentPage.value - 1 : 0;
const offset = page * MAXIMUM_ITEMS_PER_PAGE;
const options = new PaginationOptions({
offset: offset,
limit: MAXIMUM_ITEMS_PER_PAGE,
sortBy: sortBy.value,
sortDesc: sortDesc.value,
});
const result = await props.operation.fetchItems(options);
items.value = result;
} finally {
isBusy.value = false;
}
}
function toNiceSize(sizeInBytes: number) {
return bytesToString(sizeInBytes, true, undefined);
}
async function toggleSelectAll(checked: boolean) {
if (checked) {
await selectAllItems();
} else {
unselectAllItems();
}
}
function openModal() {
showDialog.value = true;
}
function hideModal() {
showDialog.value = false;
}
async function onShowModal() {
resetModal();
await loadItems();
}
function resetModal() {
unselectAllItems();
}
function resetConfirmationModal() {
confirmChecked.value = false;
}
function onConfirmCleanupSelectedItems() {
emit("onConfirmCleanupSelectedItems", selectedItems.value);
hideModal();
}
function onSort(sortByKey: string, sortDescending: boolean) {
sortBy.value = sortByKey as SortableKey;
sortDesc.value = sortDescending;
}
function isItemSelected(item: CleanableItem): boolean {
return selectedItems.value.some((selectedItem) => selectedItem.id === item.id);
}
function toggleItemSelection(item: CleanableItem): void {
const index = selectedItems.value.findIndex((selectedItem) => selectedItem.id === item.id);
if (index === -1) {
selectedItems.value = [...selectedItems.value, item];
} else {
selectedItems.value = selectedItems.value.filter((selectedItem) => selectedItem.id !== item.id);
}
}
async function selectAllItems() {
isBusy.value = true;
const operation = props.operation;
if (!operation) {
return;
}
const allItems = await operation.fetchItems(
new PaginationOptions({
offset: 0,
limit: totalRows.value,
sortBy: sortBy.value,
sortDesc: sortDesc.value,
}),
);
items.value = allItems;
selectedItems.value = allItems;
isBusy.value = false;
}
function unselectAllItems() {
selectedItems.value = [];
}
watch(
() => props.totalItems,
(newVal) => {
currentPage.value = 1;
totalRows.value = newVal;
},
);
watch(
() => props.operation,
() => {
currentPage.value = 1;
},
);
watch([currentPage, sortBy, sortDesc], async () => {
await loadItems();
});
watch(showDialog, (newVal) => {
if (newVal) {
onShowModal();
}
});
watch(openConfirmationModal, (newVal) => {
if (newVal) {
resetConfirmationModal();
}
});
watch(selectedItems, (newVal) => {
if (newVal.length === 0) {
indeterminate.value = false;
allSelected.value = false;
} else if (newVal.length === totalRows.value) {
indeterminate.value = false;
allSelected.value = true;
} else {
indeterminate.value = true;
allSelected.value = false;
}
});
defineExpose({
openModal,
selectedItems,
});
</script>
<template>
<GModal footer :show.sync="showDialog" size="medium">
<template v-slot:header>
<Heading class="w-100 d-flex justify-content-between mb-0" size="md">
<div>{{ title }}</div>
<div class="text-primary">{{ totalRows }} {{ totalRows === 1 ? "item" : "items" }}</div>
</Heading>
</template>
<div>
{{ captionText }}
</div>
<GTable
v-if="operation"
hover
:fields="fields"
:items="items"
:sort-by="sortBy"
:sort-desc="sortDesc"
:loading="isBusy"
:local-filtering="false"
:local-sorting="false"
sticky-header="50vh"
data-test-id="review-table"
@sort-changed="onSort">
<template v-slot:head(selected)>
<BFormCheckbox
v-model="allSelected"
:indeterminate="indeterminate"
data-test-id="select-all-checkbox"
@change="toggleSelectAll" />
</template>
<template v-slot:cell(selected)="data">
<BFormCheckbox
:key="data.index"
:checked="isItemSelected(data.item)"
@change="toggleItemSelection(data.item)" />
</template>
<template v-slot:cell(update_time)="data">
<UtcDate :date="data.value" mode="elapsed" />
</template>
</GTable>
<template v-slot:footer>
<div
class="d-flex align-items-center"
:class="hasPages ? 'justify-content-between' : 'justify-content-end'">
<BPagination
v-if="hasPages"
v-model="currentPage"
class="mb-0"
:total-rows="totalRows"
:per-page="MAXIMUM_ITEMS_PER_PAGE" />
<GButton
:disabled="!hasItemsSelected"
color="red"
class="mx-2"
data-test-id="delete-button"
@click="openConfirmationModal = true">
{{ permanentlyDeleteText }} {{ deleteItemsText }}
</GButton>
</div>
</template>
<GModal
id="confirmation-modal"
confirm
:show.sync="openConfirmationModal"
:title="confirmationTitle"
:ok-text="permanentlyDeleteText"
ok-color="red"
:ok-disabled="!confirmChecked"
@ok="onConfirmCleanupSelectedItems">
<BFormCheckbox id="confirm-delete-checkbox" v-model="confirmChecked" data-test-id="agreement-checkbox">
{{ agreementText }}
</BFormCheckbox>
</GModal>
</GModal>
</template>