135 lines
3.3 KiB
TypeScript
135 lines
3.3 KiB
TypeScript
import {computed, ref, shallowReactive, unref, watch} from 'vue'
|
|
import {useRouter} from 'vue-router'
|
|
import {useI18n} from 'vue-i18n'
|
|
import type {MaybeRef} from '@vueuse/core'
|
|
|
|
import type {IList} from '@/modelTypes/IList'
|
|
import type {INamespace} from '@/modelTypes/INamespace'
|
|
|
|
import {useListStore} from '@/stores/lists'
|
|
import {useNamespaceStore} from '@/stores/namespaces'
|
|
|
|
import AbstractService from '@/services/abstractService'
|
|
import TaskService from '@/services/task'
|
|
import ListDuplicateService from '@/services/listDuplicateService'
|
|
|
|
import ListModel from '@/models/list'
|
|
import ListDuplicateModel from '@/models/listDuplicateModel'
|
|
|
|
import {colorFromHex} from '@/helpers/color/colorFromHex'
|
|
import {success} from '@/message'
|
|
|
|
export default class ListService extends AbstractService<IList> {
|
|
constructor() {
|
|
super({
|
|
create: '/namespaces/{namespaceId}/lists',
|
|
get: '/lists/{id}',
|
|
getAll: '/lists',
|
|
update: '/lists/{id}',
|
|
delete: '/lists/{id}',
|
|
})
|
|
}
|
|
|
|
modelFactory(data) {
|
|
return new ListModel(data)
|
|
}
|
|
|
|
beforeUpdate(model) {
|
|
if(typeof model.tasks !== 'undefined') {
|
|
const taskService = new TaskService()
|
|
model.tasks = model.tasks.map(task => {
|
|
return taskService.beforeUpdate(task)
|
|
})
|
|
}
|
|
|
|
if(typeof model.hexColor !== 'undefined') {
|
|
model.hexColor = colorFromHex(model.hexColor)
|
|
}
|
|
|
|
return model
|
|
}
|
|
|
|
beforeCreate(list: IList) {
|
|
list.hexColor = colorFromHex(list.hexColor)
|
|
return list
|
|
}
|
|
|
|
async background(list: Pick<IList, 'id' | 'backgroundInformation'>) {
|
|
if (list.backgroundInformation === null) {
|
|
return ''
|
|
}
|
|
|
|
const response = await this.http({
|
|
url: `/lists/${list.id}/background`,
|
|
method: 'GET',
|
|
responseType: 'blob',
|
|
})
|
|
return window.URL.createObjectURL(new Blob([response.data]))
|
|
}
|
|
|
|
async removeBackground(list: Pick<IList, 'id'>) {
|
|
const cancel = this.setLoading()
|
|
|
|
try {
|
|
const response = await this.http.delete<IList>(`/lists/${list.id}/background`)
|
|
return response.data
|
|
} finally {
|
|
cancel()
|
|
}
|
|
}
|
|
}
|
|
|
|
export function useList(listId?: MaybeRef<IList['id']>) {
|
|
const {t} = useI18n({useScope: 'global'})
|
|
const router = useRouter()
|
|
|
|
const listStore = useListStore()
|
|
const namespaceStore = useNamespaceStore()
|
|
|
|
const currentListId = computed(() => unref(listId))
|
|
const list = ref<IList>(new ListModel())
|
|
|
|
const listDuplicateService = shallowReactive(new ListDuplicateService())
|
|
const isDuplicatingList = computed(() => listDuplicateService.loading)
|
|
|
|
// load list
|
|
watch(() => unref(listId), async (watchedListId) => {
|
|
if (watchedListId === undefined) {
|
|
return
|
|
}
|
|
list.value = listStore.getListById(watchedListId) || list.value
|
|
|
|
// TODO: load list from server
|
|
|
|
}, {immediate: true})
|
|
|
|
async function duplicateList(namespaceId: INamespace['id']) {
|
|
const listDuplicate = new ListDuplicateModel({
|
|
listId: currentListId.value,
|
|
namespaceId: namespaceId,
|
|
})
|
|
|
|
const duplicate = await listDuplicateService.create(listDuplicate)
|
|
|
|
namespaceStore.addListToNamespace(duplicate.list)
|
|
listStore.setList(duplicate.list)
|
|
success({message: t('list.duplicate.success')})
|
|
router.push({name: 'list.index', params: {listId: duplicate.list.id}})
|
|
}
|
|
|
|
async function deleteList() {
|
|
if (!list.value) {
|
|
return
|
|
}
|
|
|
|
await listStore.deleteList(list.value)
|
|
success({message: t('list.delete.success')})
|
|
router.push({name: 'home'})
|
|
}
|
|
|
|
return {
|
|
duplicateList,
|
|
|
|
isDuplicatingList,
|
|
}
|
|
} |