1
0

feat: port attachments store to pinia

This commit is contained in:
Dominik Pschenitschni
2022-09-02 11:23:47 +02:00
committed by Gitea
parent c2ba1b2828
commit 20e9420638
4 changed files with 38 additions and 30 deletions

View File

@ -1,29 +1,34 @@
import type { Module } from 'vuex'
import {defineStore, acceptHMRUpdate} from 'pinia'
import {findIndexById} from '@/helpers/utils'
import type { AttachmentState, RootStoreState } from '@/store/types'
import type { IAttachment } from '@/modelTypes/IAttachment'
import type {AttachmentState} from '@/store/types'
import type {IAttachment} from '@/modelTypes/IAttachment'
const store : Module<AttachmentState, RootStoreState> = {
namespaced: true,
state: () => ({
export const useAttachmentStore = defineStore('attachment', {
state: (): AttachmentState => ({
attachments: [],
}),
mutations: {
set(state, attachments: IAttachment[]) {
actions: {
set(attachments: IAttachment[]) {
console.debug('Set attachments', attachments)
state.attachments = attachments
this.attachments = attachments
},
add(state, attachment: IAttachment) {
add(attachment: IAttachment) {
console.debug('Add attachement', attachment)
state.attachments.push(attachment)
this.attachments.push(attachment)
},
removeById(state, id: IAttachment['id']) {
const attachmentIndex = findIndexById<IAttachment>(state.attachments, id)
state.attachments.splice(attachmentIndex, 1)
removeById(id: IAttachment['id']) {
const attachmentIndex = findIndexById(this.attachments, id)
this.attachments.splice(attachmentIndex, 1)
console.debug('Remove attachement', id)
},
},
}
})
export default store
// support hot reloading
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useAttachmentStore, import.meta.hot))
}