feat: attachments store with composition api #2603

Merged
konrad merged 1 commits from dpschen/frontend:feature/feat-pinia-composition-attachments into main 2022-10-31 20:40:29 +00:00
1 changed files with 22 additions and 22 deletions
Showing only changes of commit 17c07a54f9 - Show all commits

View File

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