Skip to content

Commit

Permalink
Fix: Delete in Viewer removes files from state
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Kellerer <[email protected]>
  • Loading branch information
jkellerer committed Oct 24, 2022
1 parent e787d7d commit 0294851
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/store/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 }
15 changes: 15 additions & 0 deletions src/views/Folders.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -214,6 +215,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()
Expand Down Expand Up @@ -265,6 +274,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
*
Expand Down
28 changes: 24 additions & 4 deletions src/views/Timeline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -231,6 +232,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']),

Expand Down Expand Up @@ -262,12 +271,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))
},
},
}
</script>
Expand Down

0 comments on commit 0294851

Please sign in to comment.