Skip to content

Commit 6632097

Browse files
committed
Improve file deletion logic to not require a refresh
1 parent 6b4bf3e commit 6632097

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

resources/assets/scripts/components/server/components/filemanager/FileContextMenu.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@
7979
},
8080
8181
openDeleteModal: function () {
82-
window.events.$emit('server:files:delete', this.object);
83-
this.$emit('close');
82+
this.$emit('action:delete', this.object);
8483
}
8584
}
8685
});

resources/assets/scripts/components/server/components/filemanager/FileRow.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
</div>
1313
<FileContextMenu
1414
class="context-menu"
15-
v-bind:object="file"
15+
:object="file"
1616
v-show="contextMenuVisible"
1717
v-on:close="contextMenuVisible = false"
18+
v-on:action:delete="showDeleteFileModal"
1819
ref="contextMenu"
1920
/>
21+
<DeleteFileModal :visible.sync="deleteModalVisible" :object="file" v-on:deleted="$emit('deleted')" v-on:close="deleteModalVisible = false"/>
2022
</div>
2123
</template>
2224

@@ -27,10 +29,11 @@
2729
import {formatDate, readableSize} from '../../../../helpers'
2830
import FileContextMenu from "./FileContextMenu.vue";
2931
import {DirectoryContentObject} from "@/api/server/types";
32+
import DeleteFileModal from "@/components/server/components/filemanager/modals/DeleteFileModal.vue";
3033
3134
export default Vue.extend({
3235
name: 'FileRow',
33-
components: {Icon, FileContextMenu},
36+
components: {DeleteFileModal, Icon, FileContextMenu},
3437
3538
props: {
3639
file: {
@@ -46,6 +49,7 @@
4649
data: function () {
4750
return {
4851
contextMenuVisible: false,
52+
deleteModalVisible: false,
4953
};
5054
},
5155
@@ -67,6 +71,11 @@
6771
},
6872
6973
methods: {
74+
showDeleteFileModal: function () {
75+
this.contextMenuVisible = false;
76+
this.deleteModalVisible = true;
77+
},
78+
7079
/**
7180
* Handle a right-click action on a file manager row.
7281
*/

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

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
</p>
1414
<div class="mt-8 text-right">
1515
<button class="btn btn-secondary btn-sm" v-on:click.prevent="visible = false">Cancel</button>
16-
<button class="btn btn-red btn-sm ml-2" v-on:click="deleteItem">Yes, Delete</button>
16+
<button class="btn btn-red btn-sm ml-2" v-on:click="deleteItem" :disabled="isLoading">
17+
<span v-if="isLoading" class="spinner white">&nbsp;</span>
18+
<span v-else>Yes, Delete</span>
19+
</button>
1720
</div>
1821
</div>
1922
</Modal>
@@ -30,20 +33,27 @@
3033
3134
type DataStructure = {
3235
isLoading: boolean,
33-
object: null | DirectoryContentObject,
34-
visible: boolean,
3536
error: string | null,
3637
};
3738
3839
export default Vue.extend({
3940
name: 'DeleteFileModal',
4041
components: {Modal},
4142
43+
props: {
44+
visible: { type: Boolean, default: false },
45+
object: { type: Object as () => DirectoryContentObject, required: true }
46+
},
47+
48+
watch: {
49+
visible: function (value: boolean) {
50+
this.$emit('update:visible', value);
51+
},
52+
},
53+
4254
data: function (): DataStructure {
4355
return {
4456
isLoading: false,
45-
visible: false,
46-
object: null,
4757
error: null,
4858
};
4959
},
@@ -52,45 +62,20 @@
5262
...mapState('server', ['fm', 'server', 'credentials']),
5363
},
5464
55-
mounted: function () {
56-
window.events.$on('server:files:delete', (object: DirectoryContentObject) => {
57-
this.visible = true;
58-
this.object = object;
59-
});
60-
},
61-
62-
beforeDestroy: function () {
63-
window.events.$off('server:files:delete');
64-
},
65-
6665
methods: {
6766
deleteItem: function () {
68-
if (!this.object) {
69-
return;
70-
}
71-
7267
this.isLoading = true;
7368
7469
deleteElement(this.server.uuid, this.credentials, [
7570
join(this.fm.currentDirectory, this.object.name)
7671
])
77-
.then(() => {
78-
this.$emit('close');
79-
this.closeModal();
80-
})
72+
.then(() => this.$emit('deleted'))
8173
.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 });
74+
this.error = `There was an error deleting the requested ${(this.object.directory) ? 'folder' : 'file'}. Response was: ${error.message}`;
75+
console.error('Error at Server::Files::Delete', {error});
8476
})
8577
.then(() => this.isLoading = false);
8678
},
87-
88-
closeModal: function () {
89-
this.object = null;
90-
this.isLoading = false;
91-
this.visible = false;
92-
this.error = null;
93-
},
9479
},
9580
});
9681
</script>

resources/assets/scripts/components/server/subpages/FileManager.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
<div class="flex-none w-1/6">Actions</div>
3333
</div>
3434
<div v-for="directory in directories">
35-
<FolderRow :directory="directory"/>
35+
<FolderRow :directory="directory" :key="directory.name"/>
3636
</div>
3737
<div v-for="file in files">
38-
<FileRow :file="file" :editable="editableFiles"/>
38+
<FileRow :file="file" :editable="editableFiles" v-on:deleted="fileRowDeleted(file)" :key="file.name"/>
3939
</div>
4040
</div>
4141
</div>
@@ -50,7 +50,6 @@
5050
</div>
5151
<CreateFolderModal v-on:close="listDirectory"/>
5252
<RenameModal/>
53-
<DeleteFileModal v-on:close="listDirectory"/>
5453
</div>
5554
</template>
5655

@@ -179,7 +178,11 @@
179178
180179
openNewFolderModal: function () {
181180
window.events.$emit('server:files:open-directory-modal');
182-
}
181+
},
182+
183+
fileRowDeleted: function (file: DirectoryContentObject) {
184+
this.files = this.files.filter(data => data !== file);
185+
},
183186
},
184187
});
185188
</script>

0 commit comments

Comments
 (0)