2021-06-03 22:23:04 +02:00
|
|
|
import LabelService from '@/services/label'
|
|
|
|
import {setLoading} from '@/store/helper'
|
2021-11-13 21:26:03 +01:00
|
|
|
import {success} from '@/message'
|
2021-10-09 16:34:57 +02:00
|
|
|
import {i18n} from '@/i18n'
|
2021-10-17 16:06:58 +02:00
|
|
|
import {getLabelsByIds, filterLabelsByQuery} from '@/helpers/labels'
|
2021-10-06 22:25:06 +02:00
|
|
|
|
2021-10-17 16:06:58 +02:00
|
|
|
async function getAllLabels(page = 1) {
|
2021-10-17 17:06:28 +02:00
|
|
|
const labelService = new LabelService()
|
2021-10-09 16:34:57 +02:00
|
|
|
const labels = await labelService.getAll({}, {}, page)
|
|
|
|
if (page < labelService.totalPages) {
|
|
|
|
const nextLabels = await getAllLabels(page + 1)
|
|
|
|
return labels.concat(nextLabels)
|
|
|
|
} else {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:23:04 +02:00
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: () => ({
|
2021-10-06 22:25:06 +02:00
|
|
|
// The labels are stored as an object which has the label ids as keys.
|
2021-06-03 22:23:04 +02:00
|
|
|
labels: {},
|
|
|
|
loaded: false,
|
|
|
|
}),
|
|
|
|
mutations: {
|
|
|
|
setLabels(state, labels) {
|
|
|
|
labels.forEach(l => {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.labels[l.id] = l
|
2021-06-03 22:23:04 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
setLabel(state, label) {
|
2021-08-19 21:35:38 +02:00
|
|
|
state.labels[label.id] = label
|
2021-06-03 22:23:04 +02:00
|
|
|
},
|
|
|
|
removeLabelById(state, label) {
|
2021-08-19 21:35:38 +02:00
|
|
|
delete state.labels[label.id]
|
2021-06-03 22:23:04 +02:00
|
|
|
},
|
|
|
|
setLoaded(state, loaded) {
|
|
|
|
state.loaded = loaded
|
|
|
|
},
|
|
|
|
},
|
2021-10-06 22:25:06 +02:00
|
|
|
getters: {
|
|
|
|
getLabelsByIds(state) {
|
|
|
|
return (ids) => getLabelsByIds(state, ids)
|
|
|
|
},
|
|
|
|
filterLabelsByQuery(state) {
|
2021-10-17 13:20:51 +02:00
|
|
|
return (labelsToHide, query) => filterLabelsByQuery(state, labelsToHide, query)
|
2021-10-06 22:25:06 +02:00
|
|
|
},
|
2021-11-13 21:26:03 +01:00
|
|
|
getLabelsByExactTitles(state) {
|
|
|
|
return labelTitles => Object
|
|
|
|
.values(state.labels)
|
|
|
|
.filter(({title}) => labelTitles.some(l => l.toLowerCase() === title.toLowerCase()))
|
|
|
|
},
|
2021-10-06 22:25:06 +02:00
|
|
|
},
|
2021-06-03 22:23:04 +02:00
|
|
|
actions: {
|
2021-10-09 16:34:57 +02:00
|
|
|
async loadAllLabels(ctx, {forceLoad} = {}) {
|
2021-06-03 22:23:04 +02:00
|
|
|
if (ctx.state.loaded && !forceLoad) {
|
2021-10-09 16:34:57 +02:00
|
|
|
return
|
2021-06-03 22:23:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const cancel = setLoading(ctx, 'labels')
|
|
|
|
|
2021-10-09 16:34:57 +02:00
|
|
|
try {
|
|
|
|
const labels = await getAllLabels()
|
|
|
|
ctx.commit('setLabels', labels)
|
|
|
|
ctx.commit('setLoaded', true)
|
|
|
|
return labels
|
|
|
|
} finally {
|
|
|
|
cancel()
|
2021-06-03 22:23:04 +02:00
|
|
|
}
|
|
|
|
},
|
2021-10-09 16:34:57 +02:00
|
|
|
async deleteLabel(ctx, label) {
|
2021-06-03 22:23:04 +02:00
|
|
|
const cancel = setLoading(ctx, 'labels')
|
|
|
|
const labelService = new LabelService()
|
|
|
|
|
2021-10-09 16:34:57 +02:00
|
|
|
try {
|
|
|
|
const result = await labelService.delete(label)
|
|
|
|
ctx.commit('removeLabelById', label)
|
|
|
|
success({message: i18n.global.t('label.deleteSuccess')})
|
|
|
|
return result
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-06-03 22:23:04 +02:00
|
|
|
},
|
2021-10-09 16:34:57 +02:00
|
|
|
async updateLabel(ctx, label) {
|
2021-06-03 22:23:04 +02:00
|
|
|
const cancel = setLoading(ctx, 'labels')
|
|
|
|
const labelService = new LabelService()
|
|
|
|
|
2021-10-09 16:34:57 +02:00
|
|
|
try {
|
|
|
|
const newLabel = await labelService.update(label)
|
|
|
|
ctx.commit('setLabel', newLabel)
|
|
|
|
success({message: i18n.global.t('label.edit.success')})
|
|
|
|
return newLabel
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-06-03 22:23:04 +02:00
|
|
|
},
|
2021-10-09 16:34:57 +02:00
|
|
|
async createLabel(ctx, label) {
|
2021-06-03 22:23:04 +02:00
|
|
|
const cancel = setLoading(ctx, 'labels')
|
|
|
|
const labelService = new LabelService()
|
|
|
|
|
2021-10-09 16:34:57 +02:00
|
|
|
try {
|
|
|
|
const newLabel = await labelService.create(label)
|
|
|
|
ctx.commit('setLabel', newLabel)
|
|
|
|
return newLabel
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-06-03 22:23:04 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|