From 96bae5a330ab39e82f0f2985647b9e99bc626826 Mon Sep 17 00:00:00 2001 From: Juergen Kellerer Date: Mon, 24 Oct 2022 19:22:20 +0200 Subject: [PATCH] Fix: Delete in Viewer removes files from state Signed-off-by: Juergen Kellerer --- src/store/folders.js | 31 +++++++++++++++++++++++++++++++ src/views/Folders.vue | 15 +++++++++++++++ src/views/Timeline.vue | 28 ++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/store/folders.js b/src/store/folders.js index 198488241..b7fa1d1bf 100644 --- a/src/store/folders.js +++ b/src/store/folders.js @@ -82,6 +82,25 @@ const mutations = { Vue.set(state.folders, fileid, [...list, ...state.folders[fileid]]) } }, + + /** + * Remove a fileid from all folders that contain it + * + * @param {object} state vuex state + * @param {object} data destructuring object + * @param {number} data.fileid id of this folder + * @param {Array} data.files list of files to remove + */ + removeFilesFromFolder(state, { fileid, files }) { + const removedIds = files + .filter(file => file && file.fileid) + .map(file => `${file.fileid}`) + + const folderFileIds = state.folders[fileid] + if (folderFileIds) { + Vue.set(state.folders, fileid, folderFileIds.filter(id => !removedIds.includes(`${id}`))) + } + }, } const getters = { @@ -130,6 +149,18 @@ const actions = { addFilesToFolder(context, { fileid, files }) { context.commit('addFilesToFolder', { fileid, files }) }, + + /** + * Remove a fileid from all folders that contain it + * + * @param {object} context vuex context + * @param {object} data destructuring object + * @param {number} data.fileid id of this folder + * @param {Array} data.files list of files to remove + */ + removeFilesFromFolder(context, { fileid, files }) { + context.commit('removeFilesFromFolder', { fileid, files }) + }, } export default { state, mutations, getters, actions } diff --git a/src/views/Folders.vue b/src/views/Folders.vue index 81e31d3c5..55f2d6c20 100644 --- a/src/views/Folders.vue +++ b/src/views/Folders.vue @@ -65,6 +65,7 @@ import { mapGetters } from 'vuex' import { UploadPicker } from '@nextcloud/upload' import { NcEmptyContent } from '@nextcloud/vue' +import { subscribe, unsubscribe } from '@nextcloud/event-bus' import VirtualGrid from 'vue-virtual-grid' import FileLegacy from '../components/FileLegacy.vue' @@ -213,6 +214,14 @@ export default { this.fetchFolderContent() }, + created() { + subscribe('files:file:deleted', this.onDelete) // listen for delete in Viewer.vue + }, + + beforeDestroy() { + unsubscribe('files:file:deleted', this.onDelete) + }, + methods: { onRefresh() { this.fetchFolderContent() @@ -260,6 +269,12 @@ export default { } }, + onDelete(file) { + if (file && file.fileid) { + this.$store.dispatch('removeFilesFromFolder', { fileid: this.folderId, files: [file] }) + } + }, + /** * Fetch file Info and add them into the store * diff --git a/src/views/Timeline.vue b/src/views/Timeline.vue index 85fc96166..957d934d2 100644 --- a/src/views/Timeline.vue +++ b/src/views/Timeline.vue @@ -133,6 +133,7 @@ import PlusBoxMultiple from 'vue-material-design-icons/PlusBoxMultiple' import Download from 'vue-material-design-icons/Download' import { NcModal, NcActions, NcActionButton, NcButton, NcEmptyContent, isMobile } from '@nextcloud/vue' +import { subscribe, unsubscribe } from '@nextcloud/event-bus' import moment from '@nextcloud/moment' import { allMimes } from '../services/AllowedMimes.js' @@ -230,6 +231,14 @@ export default { ]), }, + created() { + subscribe('files:file:deleted', this.onDeleteFile) // listen for delete in Viewer.vue + }, + + beforeDestroy() { + unsubscribe('files:file:deleted', this.onDeleteFile) + }, + methods: { ...mapActions(['deleteFiles', 'addFilesToAlbum']), @@ -261,12 +270,23 @@ export default { }, async deleteSelection() { - // Need to store the file ids so it is not changed before the deleteFiles call. - const fileIds = this.selectedFileIds - this.onUncheckFiles(fileIds) - this.fetchedFileIds = this.fetchedFileIds.filter(fileid => !fileIds.includes(fileid)) + const fileIds = [...this.selectedFileIds] + this.removeFromFetchedFiles(fileIds) await this.deleteFiles(fileIds) }, + + onDeleteFile(file) { + const fileId = (file && file.fileid && file.fileid.toString()) + if (fileId) { + this.removeFromFetchedFiles([fileId]) + this.$store.commit('deleteFile', fileId) + } + }, + + removeFromFetchedFiles(fileIds) { + this.onUncheckFiles(fileIds) + this.fetchedFileIds = this.fetchedFileIds.filter(fileId => !fileIds.includes(fileId)) + }, }, }