|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { defineStore } from 'pinia' |
| 7 | +import Vue from 'vue' |
| 8 | +import { AttachmentApi } from '../services/AttachmentApi.js' |
| 9 | + |
| 10 | +const apiClient = new AttachmentApi() |
| 11 | + |
| 12 | +export const useAttachmentStore = defineStore('attachment', { |
| 13 | + state: () => ({ |
| 14 | + attachments: {}, |
| 15 | + }), |
| 16 | + getters: { |
| 17 | + attachmentsByCard: (state) => (cardId) => { |
| 18 | + if (typeof state.attachments[cardId] === 'undefined') { |
| 19 | + return [] |
| 20 | + } |
| 21 | + return state.attachments[cardId] |
| 22 | + }, |
| 23 | + }, |
| 24 | + actions: { |
| 25 | + async createAttachment({ cardId, formData, onUploadProgress }) { |
| 26 | + const boardId = this.$vuex.state.currentBoard.id |
| 27 | + const attachment = await apiClient.createAttachment({ cardId, formData, onUploadProgress, boardId }) |
| 28 | + if (typeof this.attachments[cardId] === 'undefined') { |
| 29 | + Vue.set(this.attachments, cardId, [attachment]) |
| 30 | + } else { |
| 31 | + this.attachments[cardId].push(attachment) |
| 32 | + } |
| 33 | + this.$vuex.commit('cardSetAttachmentCount', { cardId }) |
| 34 | + }, |
| 35 | + async fetchAttachments(cardId) { |
| 36 | + const boardId = this.$vuex.state.currentBoard.id |
| 37 | + const attachments = await apiClient.fetchAttachments(cardId, boardId) |
| 38 | + Vue.set(this.attachments, cardId, attachments) |
| 39 | + this.$vuex.commit('cardSetAttachmentCount', { cardId, count: attachments.length }) |
| 40 | + }, |
| 41 | + async updateAttachment({ cardId, attachment, formData }) { |
| 42 | + const boardId = this.$vuex.state.currentBoard.id |
| 43 | + const result = await apiClient.updateAttachment({ cardId, attachment, formData, boardId }) |
| 44 | + const existingIndex = this.attachments[attachment.cardId].findIndex(a => a.id === attachment.id && a.type === attachment.type) |
| 45 | + if (existingIndex !== -1) { |
| 46 | + Vue.set(this.attachments[cardId], existingIndex, result) |
| 47 | + } |
| 48 | + }, |
| 49 | + async deleteAttachment(attachment) { |
| 50 | + const boardId = this.$vuex.state.currentBoard.id |
| 51 | + await apiClient.deleteAttachment(attachment, boardId) |
| 52 | + const existingIndex = this.attachments[attachment.cardId].findIndex(a => a.id === attachment.id && a.type === attachment.type) |
| 53 | + if (existingIndex !== -1) { |
| 54 | + Vue.set(this.attachments[attachment.cardId][existingIndex], 'deletedAt', Date.now() / 1000 | 0) |
| 55 | + } |
| 56 | + this.$vuex.commit('cardDecreaseAttachmentCount', { cardId: attachment.cardId }) |
| 57 | + }, |
| 58 | + async unshareAttachment(attachment) { |
| 59 | + const boardId = this.$vuex.state.currentBoard.id |
| 60 | + await apiClient.deleteAttachment(attachment, boardId) |
| 61 | + const existingIndex = this.attachments[attachment.cardId].findIndex(a => a.id === attachment.id && a.type === attachment.type) |
| 62 | + if (existingIndex !== -1) { |
| 63 | + Vue.set(this.attachments[attachment.cardId][existingIndex], 'deletedAt', -1) |
| 64 | + } |
| 65 | + this.$vuex.commit('cardDecreaseAttachmentCount', { cardId: attachment.cardId }) |
| 66 | + }, |
| 67 | + async restoreAttachment(attachment) { |
| 68 | + const boardId = this.$vuex.state.currentBoard.id |
| 69 | + const restoredAttachment = await apiClient.restoreAttachment(attachment, boardId) |
| 70 | + const existingIndex = this.attachments[restoredAttachment.cardId].findIndex(a => a.id === restoredAttachment.id && a.type === restoredAttachment.type) |
| 71 | + if (existingIndex !== -1) { |
| 72 | + Vue.set(this.attachments[restoredAttachment.cardId][existingIndex], 'deletedAt', 0) |
| 73 | + } |
| 74 | + this.$vuex.commit('cardIncreaseAttachmentCount', attachment.cardId) |
| 75 | + }, |
| 76 | + }, |
| 77 | + |
| 78 | +}) |
0 commit comments