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 25, 2022
1 parent ada7822 commit 96bae5a
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 @@ -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()
Expand Down Expand Up @@ -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
*
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 @@ -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']),
Expand Down Expand Up @@ -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))
},
},
}
</script>
Expand Down

0 comments on commit 96bae5a

Please sign in to comment.