-
Notifications
You must be signed in to change notification settings - Fork 387
/
DeleteFileDialog.vue
173 lines (145 loc) · 5.45 KB
/
DeleteFileDialog.vue
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
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
<template>
<v-dialog
v-model="model"
width="410px"
transition="fade-transition"
:persistent="isLoading"
>
<v-card ref="innerContent" rounded="xlg">
<v-card-item class="pa-5 pl-7">
<template #prepend>
<v-sheet
class="border-sm d-flex justify-center align-center"
width="40"
height="40"
rounded="lg"
>
<icon-trash />
</v-sheet>
</template>
<v-card-title class="font-weight-bold text-capitalize">Delete {{ fileTypes }}</v-card-title>
<template #append>
<v-btn
icon="$close"
variant="text"
size="small"
color="default"
:disabled="isLoading"
@click="model = false"
/>
</template>
</v-card-item>
<v-divider />
<div class="px-7 py-6">
<p class="mb-3">The following {{ fileTypes }}<template v-if="isFolder">, and all contained data</template> will be deleted. This action cannot be undone.</p>
<p v-for="file of files" :key="file.path + file.Key" class="mt-2">
<v-chip :title="file.path + file.Key" class="font-weight-bold text-wrap h-100 py-2">
{{ file.path + file.Key }}
</v-chip>
</p>
</div>
<v-divider />
<v-card-actions class="pa-7">
<v-row>
<v-col>
<v-btn variant="outlined" color="default" block :disabled="isLoading" @click="model = false">
Cancel
</v-btn>
</v-col>
<v-col>
<v-btn color="error" variant="flat" block :loading="isLoading" @click="onDeleteClick">
Delete
</v-btn>
</v-col>
</v-row>
</v-card-actions>
</v-card>
</v-dialog>
</template>
<script setup lang="ts">
import { computed, ref, Component, watch } from 'vue';
import {
VDialog,
VCard,
VCardItem,
VSheet,
VCardTitle,
VDivider,
VCardActions,
VRow,
VCol,
VBtn,
VChip,
} from 'vuetify/components';
import { AnalyticsErrorEventSource } from '@/utils/constants/analyticsEventNames';
import { useBucketsStore } from '@/store/modules/bucketsStore';
import { useLoading } from '@/composables/useLoading';
import { useNotify } from '@/utils/hooks';
import { BrowserObject, useObjectBrowserStore } from '@/store/modules/objectBrowserStore';
import IconTrash from '@/components/icons/IconTrash.vue';
const props = defineProps<{
files: BrowserObject[],
}>();
const emit = defineEmits<{
'contentRemoved': [],
'filesDeleted': [],
}>();
const model = defineModel<boolean>({ required: true });
const obStore = useObjectBrowserStore();
const bucketsStore = useBucketsStore();
const { isLoading, withLoading } = useLoading();
const notify = useNotify();
const innerContent = ref<Component | null>(null);
const filePath = computed<string>(() => bucketsStore.state.fileComponentPath);
const fileCount = computed<number>(() => props.files.filter(file => file.type === 'file' && !file.VersionId).length);
const folderCount = computed<number>(() => props.files.filter(file => file.type === 'folder').length);
const versionsCount = computed<number>(() => props.files.filter(file => !!file.VersionId).length);
/**
* types of objects to be deleted.
*/
const fileTypes = computed<string>(() => {
let result = '';
if (versionsCount.value > 0) {
result += `version${versionsCount.value > 1 ? 's' : ''}`;
}
if (fileCount.value > 0) {
result += `${result ? ' and' : ''} file${fileCount.value > 1 ? 's' : ''}`;
}
if (folderCount.value > 0) {
result += `${result ? ' and' : ''} folder${folderCount.value > 1 ? 's' : ''}`;
}
return result;
});
const isFolder = computed<boolean>(() => {
return folderCount.value > 0;
});
async function onDeleteClick(): Promise<void> {
await withLoading(async () => {
try {
if (props.files.length === 1) {
await deleteSingleFile(props.files[0]);
} else if (props.files.length > 1) {
// multiple files selected in the file browser.
await obStore.deleteSelected();
} else return;
} catch (error) {
error.message = `Error deleting ${fileTypes.value}. ${error.message}`;
notify.notifyError(error, AnalyticsErrorEventSource.FILE_BROWSER);
return;
}
emit('filesDeleted');
notify.success(`${fileCount.value + folderCount.value} ${fileTypes.value} deleted`);
model.value = false;
});
}
async function deleteSingleFile(file: BrowserObject): Promise<void> {
if (isFolder.value) {
await obStore.deleteFolder(file, filePath.value ? filePath.value + '/' : '');
} else {
await obStore.deleteObject(filePath.value ? filePath.value + '/' : '', file);
}
}
watch(innerContent, comp => !comp && emit('contentRemoved'));
</script>