diff --git a/frontend/src/stores/labels.ts b/frontend/src/stores/labels.ts index 3c470be4e..0c8b99f05 100644 --- a/frontend/src/stores/labels.ts +++ b/frontend/src/stores/labels.ts @@ -1,4 +1,4 @@ -import {computed, ref} from 'vue' +import {computed, readonly, ref} from 'vue' import {acceptHMRUpdate, defineStore} from 'pinia' import LabelService from '@/services/label' @@ -21,23 +21,28 @@ async function getAllLabels(page = 1): Promise { } } -export interface LabelState { - [id: ILabel['id']]: ILabel -} - export const useLabelStore = defineStore('label', () => { - // The labels are stored as an object which has the label ids as keys. - const labels = ref({}) - const isLoading = ref(false) + const labels = ref<{ [id: ILabel['id']]: ILabel }>({}) - const getLabelsByIds = computed(() => { - return (ids: ILabel['id'][]) => Object.values(labels.value).filter(({id}) => ids.includes(id)) - }) + // Alphabetically sort the labels + const labelsArray = computed(() => Object.values(labels.value) + .sort((a, b) => a.title.localeCompare( + b.title, i18n.global.locale.value, + { ignorePunctuation: true }, + )), + ) + + const isLoading = ref(false) const getLabelById = computed(() => { - return (labelId: ILabel['id']) => Object.values(labels.value).find(({id}) => id === labelId) + return (labelId: ILabel['id']) => labels.value[labelId] }) + const getLabelsByIds = computed(() => (ids: ILabel['id'][]) => + ids.map(id => labels.value[id]).filter(Boolean), + ) + + // ** // * Checks if a project of labels is available in the store and filters them then query // ** @@ -53,14 +58,12 @@ export const useLabelStore = defineStore('label', () => { }) const getLabelsByExactTitles = computed(() => { - return (labelTitles: string[]) => Object - .values(labels.value) + return (labelTitles: string[]) => labelsArray.value .filter(({title}) => labelTitles.some(l => l.toLowerCase() === title.toLowerCase())) }) const getLabelByExactTitle = computed(() => { - return (labelTitle: string) => Object - .values(labels.value) + return (labelTitle: string) => labelsArray.value .find(l => l.title.toLowerCase() === labelTitle.toLowerCase()) }) @@ -144,11 +147,12 @@ export const useLabelStore = defineStore('label', () => { } return { - labels, + labels: readonly(labels), + labelsArray: readonly(labelsArray), isLoading, - getLabelsByIds, getLabelById, + getLabelsByIds, filterLabelsByQuery, getLabelsByExactTitles, getLabelByExactTitle, diff --git a/frontend/src/views/labels/ListLabels.vue b/frontend/src/views/labels/ListLabels.vue index 2cacc14a2..254df3f00 100644 --- a/frontend/src/views/labels/ListLabels.vue +++ b/frontend/src/views/labels/ListLabels.vue @@ -13,7 +13,7 @@

{{ $t('label.manage') }}

-

+

{{ $t('label.description') }}

- {{ l.title }} + {{ label.title }} - {{ l.title }} + {{ label.title }}
@@ -155,7 +155,7 @@ const labelEditLabel = ref(new LabelModel()) const isLabelEdit = ref(false) const editorActive = ref(false) const showDeleteModal = ref(false) -const labelToDelete = ref(null) +const labelToDelete = ref(undefined) useTitle(() => t('label.title')) @@ -165,11 +165,13 @@ const userInfo = computed(() => authStore.info) const labelStore = useLabelStore() labelStore.loadAllLabels() -// Alphabetically sort the labels -const labels = computed(() => Object.values(labelStore.labels).sort((f, s) => f.title > s.title ? 1 : -1)) const loading = computed(() => labelStore.isLoading) -function deleteLabel(label: ILabel) { +function deleteLabel(label?: ILabel) { + if (!label) { + return + } + showDeleteModal.value = false isLabelEdit.value = false return labelStore.deleteLabel(label)