Skip to content

Commit 6b4bf3e

Browse files
committed
Add basic file deletion logic
1 parent ea9cdea commit 6b4bf3e

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {withCredentials} from "@/api/http";
2+
import {ServerApplicationCredentials} from "@/store/types";
3+
4+
/**
5+
* Deletes files and/or folders from the server. You should pass through an array of
6+
* file or folder paths to be deleted.
7+
*/
8+
export function deleteElement(server: string, credentials: ServerApplicationCredentials, items: Array<string>): Promise<any> {
9+
return new Promise((resolve, reject) => {
10+
withCredentials(server, credentials).post('/v1/server/file/delete', { items })
11+
.then(resolve)
12+
.catch(reject);
13+
})
14+
}

resources/assets/scripts/components/server/components/filemanager/modals/DeleteFileModal.vue

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<template>
22
<Modal :show="visible" v-on:close="visible = false">
3+
<MessageBox
4+
class="alert error mb-8"
5+
title="Error"
6+
:message="error"
7+
v-if="error"
8+
/>
39
<div v-if="object">
410
<h3 class="font-medium mb-6">Really delete {{ object.name }}?</h3>
511
<p class="text-sm text-neutral-700">
6-
Deletion is a permanent operation: <strong>{{ object.name }}</strong><span v-if="object.folder">, as well as its contents,</span> will be removed immediately.
12+
Deletion is a permanent operation: <strong>{{ object.name }}</strong><span v-if="object.directory">, as well as its contents,</span> will be removed immediately.
713
</p>
814
<div class="mt-8 text-right">
915
<button class="btn btn-secondary btn-sm" v-on:click.prevent="visible = false">Cancel</button>
10-
<button class="btn btn-red btn-sm ml-2">Yes, Delete</button>
16+
<button class="btn btn-red btn-sm ml-2" v-on:click="deleteItem">Yes, Delete</button>
1117
</div>
1218
</div>
1319
</Modal>
@@ -17,10 +23,16 @@
1723
import Vue from 'vue';
1824
import Modal from '@/components/core/Modal.vue';
1925
import {DirectoryContentObject} from "@/api/server/types";
26+
import {deleteElement} from '@/api/server/files/deleteElement';
27+
import {mapState} from "vuex";
28+
import {AxiosError} from "axios";
29+
import { join } from 'path';
2030
2131
type DataStructure = {
32+
isLoading: boolean,
2233
object: null | DirectoryContentObject,
2334
visible: boolean,
35+
error: string | null,
2436
};
2537
2638
export default Vue.extend({
@@ -29,11 +41,17 @@
2941
3042
data: function (): DataStructure {
3143
return {
44+
isLoading: false,
3245
visible: false,
3346
object: null,
47+
error: null,
3448
};
3549
},
3650
51+
computed: {
52+
...mapState('server', ['fm', 'server', 'credentials']),
53+
},
54+
3755
mounted: function () {
3856
window.events.$on('server:files:delete', (object: DirectoryContentObject) => {
3957
this.visible = true;
@@ -44,5 +62,35 @@
4462
beforeDestroy: function () {
4563
window.events.$off('server:files:delete');
4664
},
65+
66+
methods: {
67+
deleteItem: function () {
68+
if (!this.object) {
69+
return;
70+
}
71+
72+
this.isLoading = true;
73+
74+
deleteElement(this.server.uuid, this.credentials, [
75+
join(this.fm.currentDirectory, this.object.name)
76+
])
77+
.then(() => {
78+
this.$emit('close');
79+
this.closeModal();
80+
})
81+
.catch((error: AxiosError) => {
82+
this.error = `There was an error deleting the requested ${(this.object && this.object.directory) ? 'folder' : 'file'}. Response was: ${error.message}`;
83+
console.error('Error at Server::Files::Delete', { error });
84+
})
85+
.then(() => this.isLoading = false);
86+
},
87+
88+
closeModal: function () {
89+
this.object = null;
90+
this.isLoading = false;
91+
this.visible = false;
92+
this.error = null;
93+
},
94+
},
4795
});
4896
</script>

0 commit comments

Comments
 (0)