This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
frontend/src/services/list.ts

134 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 {success} from '@/message'
import AbstractService from './abstractService'
import ListModel from '@/models/list'
import TaskService from './task'
import {colorFromHex} from '@/helpers/color/colorFromHex'
import ListDuplicateModel from '@/models/listDuplicateModel'
import ListDuplicateService from './listDuplicateService'
import {useListStore} from '@/stores/lists'
import {useNamespaceStore} from '@/stores/namespaces'
import type {IList} from '@/modelTypes/IList'
import type {INamespace} from '@/modelTypes/INamespace'
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) {
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(`/lists/${list.id}/background`, list)
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,
}
}