feat: attachments store with composition api (#2603)
All checks were successful
continuous-integration/drone/push Build is passing

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: #2603
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-10-31 20:40:26 +00:00 committed by konrad
parent b4f4fd45a4
commit a50eca852f

View File

@ -1,34 +1,34 @@
import {ref, readonly} from 'vue'
import {defineStore, acceptHMRUpdate} from 'pinia' import {defineStore, acceptHMRUpdate} from 'pinia'
import {findIndexById} from '@/helpers/utils' import {findIndexById} from '@/helpers/utils'
import type {IAttachment} from '@/modelTypes/IAttachment' import type {IAttachment} from '@/modelTypes/IAttachment'
export interface AttachmentState { export const useAttachmentStore = defineStore('attachment', () => {
attachments: IAttachment[], const attachments = ref<IAttachment[]>([])
function set(newAttachments: IAttachment[]) {
console.debug('Set attachments', newAttachments)
attachments.value = newAttachments
} }
export const useAttachmentStore = defineStore('attachment', { function add(attachment: IAttachment) {
state: (): AttachmentState => ({
attachments: [],
}),
actions: {
set(attachments: IAttachment[]) {
console.debug('Set attachments', attachments)
this.attachments = attachments
},
add(attachment: IAttachment) {
console.debug('Add attachement', attachment) console.debug('Add attachement', attachment)
this.attachments.push(attachment) attachments.value.push(attachment)
}, }
removeById(id: IAttachment['id']) { function removeById(id: IAttachment['id']) {
const attachmentIndex = findIndexById(this.attachments, id) const attachmentIndex = findIndexById(attachments.value, id)
this.attachments.splice(attachmentIndex, 1) attachments.value.splice(attachmentIndex, 1)
console.debug('Remove attachement', id) console.debug('Remove attachement', id)
}, }
},
return {
attachments: readonly(attachments),
set,
add,
removeById,
}
}) })
// support hot reloading // support hot reloading