feat: label store with composition api (#2605)
Co-authored-by: Dominik Pschenitschni <mail@celement.de> Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2605 Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de> Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
parent
5ae8bace82
commit
1002579173
@ -1,4 +1,5 @@
|
|||||||
import { acceptHMRUpdate, defineStore } from 'pinia'
|
import {computed, ref} from 'vue'
|
||||||
|
import {acceptHMRUpdate, defineStore} from 'pinia'
|
||||||
|
|
||||||
import LabelService from '@/services/label'
|
import LabelService from '@/services/label'
|
||||||
import {success} from '@/message'
|
import {success} from '@/message'
|
||||||
@ -21,122 +22,134 @@ async function getAllLabels(page = 1): Promise<ILabel[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface LabelState {
|
export interface LabelState {
|
||||||
labels: {
|
[id: ILabel['id']]: ILabel
|
||||||
[id: ILabel['id']]: ILabel
|
|
||||||
},
|
|
||||||
isLoading: boolean,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useLabelStore = defineStore('label', {
|
export const useLabelStore = defineStore('label', () => {
|
||||||
state: () : LabelState => ({
|
// The labels are stored as an object which has the label ids as keys.
|
||||||
// The labels are stored as an object which has the label ids as keys.
|
const labels = ref<LabelState>({})
|
||||||
labels: {},
|
const isLoading = ref(false)
|
||||||
isLoading: false,
|
|
||||||
}),
|
|
||||||
|
|
||||||
getters: {
|
const getLabelsByIds = computed(() => {
|
||||||
getLabelsByIds(state) {
|
return (ids: ILabel['id'][]) => Object.values(labels.value).filter(({id}) => ids.includes(id))
|
||||||
return (ids: ILabel['id'][]) => Object.values(state.labels).filter(({id}) => ids.includes(id))
|
})
|
||||||
},
|
|
||||||
// **
|
|
||||||
// * Checks if a list of labels is available in the store and filters them then query
|
|
||||||
// **
|
|
||||||
filterLabelsByQuery(state) {
|
|
||||||
return (labelsToHide: ILabel[], query: string) => {
|
|
||||||
const labelIdsToHide: number[] = labelsToHide.map(({id}) => id)
|
|
||||||
|
|
||||||
return search(query)
|
// **
|
||||||
?.filter(value => !labelIdsToHide.includes(value))
|
// * Checks if a list of labels is available in the store and filters them then query
|
||||||
.map(id => state.labels[id])
|
// **
|
||||||
|| []
|
const filterLabelsByQuery = computed(() => {
|
||||||
}
|
return (labelsToHide: ILabel[], query: string) => {
|
||||||
},
|
const labelIdsToHide: number[] = labelsToHide.map(({id}) => id)
|
||||||
getLabelsByExactTitles(state) {
|
|
||||||
return (labelTitles: string[]) => Object
|
|
||||||
.values(state.labels)
|
|
||||||
.filter(({title}) => labelTitles.some(l => l.toLowerCase() === title.toLowerCase()))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
actions: {
|
return search(query)
|
||||||
setIsLoading(isLoading: boolean) {
|
?.filter(value => !labelIdsToHide.includes(value))
|
||||||
this.isLoading = isLoading
|
.map(id => labels.value[id])
|
||||||
},
|
|| []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
setLabels(labels: ILabel[]) {
|
const getLabelsByExactTitles = computed(() => {
|
||||||
labels.forEach(l => {
|
return (labelTitles: string[]) => Object
|
||||||
this.labels[l.id] = l
|
.values(labels.value)
|
||||||
add(l)
|
.filter(({title}) => labelTitles.some(l => l.toLowerCase() === title.toLowerCase()))
|
||||||
})
|
})
|
||||||
},
|
|
||||||
|
|
||||||
setLabel(label: ILabel) {
|
|
||||||
this.labels[label.id] = label
|
|
||||||
update(label)
|
|
||||||
},
|
|
||||||
|
|
||||||
removeLabelById(label: ILabel) {
|
function setIsLoading(newIsLoading: boolean) {
|
||||||
remove(label)
|
isLoading.value = newIsLoading
|
||||||
delete this.labels[label.id]
|
}
|
||||||
},
|
|
||||||
|
|
||||||
async loadAllLabels({forceLoad} : {forceLoad?: boolean} = {}) {
|
function setLabels(newLabels: ILabel[]) {
|
||||||
if (this.isLoading && !forceLoad) {
|
newLabels.forEach(l => {
|
||||||
return
|
labels.value[l.id] = l
|
||||||
}
|
add(l)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const cancel = setModuleLoading(this)
|
function setLabel(label: ILabel) {
|
||||||
|
labels.value[label.id] = label
|
||||||
|
update(label)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
function removeLabelById(label: ILabel) {
|
||||||
const labels = await getAllLabels()
|
remove(label)
|
||||||
this.setLabels(labels)
|
delete labels.value[label.id]
|
||||||
return labels
|
}
|
||||||
} finally {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async deleteLabel(label: ILabel) {
|
async function loadAllLabels({forceLoad} : {forceLoad?: boolean} = {}) {
|
||||||
const cancel = setModuleLoading(this)
|
if (isLoading.value && !forceLoad) {
|
||||||
const labelService = new LabelService()
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
const result = await labelService.delete(label)
|
|
||||||
this.removeLabelById(label)
|
|
||||||
success({message: i18n.global.t('label.deleteSuccess')})
|
|
||||||
return result
|
|
||||||
} finally {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async updateLabel(label: ILabel) {
|
try {
|
||||||
const cancel = setModuleLoading(this)
|
const newLabels = await getAllLabels()
|
||||||
const labelService = new LabelService()
|
setLabels(newLabels)
|
||||||
|
return newLabels
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
async function deleteLabel(label: ILabel) {
|
||||||
const newLabel = await labelService.update(label)
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
this.setLabel(newLabel)
|
const labelService = new LabelService()
|
||||||
success({message: i18n.global.t('label.edit.success')})
|
|
||||||
return newLabel
|
|
||||||
} finally {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async createLabel(label: ILabel) {
|
try {
|
||||||
const cancel = setModuleLoading(this)
|
const result = await labelService.delete(label)
|
||||||
const labelService = new LabelService()
|
removeLabelById(label)
|
||||||
|
success({message: i18n.global.t('label.deleteSuccess')})
|
||||||
|
return result
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
async function updateLabel(label: ILabel) {
|
||||||
const newLabel = await labelService.create(label) as ILabel
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
this.setLabel(newLabel)
|
const labelService = new LabelService()
|
||||||
return newLabel
|
|
||||||
} finally {
|
try {
|
||||||
cancel()
|
const newLabel = await labelService.update(label)
|
||||||
}
|
setLabel(newLabel)
|
||||||
},
|
success({message: i18n.global.t('label.edit.success')})
|
||||||
},
|
return newLabel
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createLabel(label: ILabel) {
|
||||||
|
const cancel = setModuleLoading(this, setIsLoading)
|
||||||
|
const labelService = new LabelService()
|
||||||
|
|
||||||
|
try {
|
||||||
|
const newLabel = await labelService.create(label) as ILabel
|
||||||
|
setLabel(newLabel)
|
||||||
|
return newLabel
|
||||||
|
} finally {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
labels,
|
||||||
|
isLoading,
|
||||||
|
|
||||||
|
getLabelsByIds,
|
||||||
|
filterLabelsByQuery,
|
||||||
|
getLabelsByExactTitles,
|
||||||
|
|
||||||
|
setLabels,
|
||||||
|
setLabel,
|
||||||
|
removeLabelById,
|
||||||
|
loadAllLabels,
|
||||||
|
deleteLabel,
|
||||||
|
updateLabel,
|
||||||
|
createLabel,
|
||||||
|
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// support hot reloading
|
// support hot reloading
|
||||||
|
Loading…
x
Reference in New Issue
Block a user