This commit is contained in:
2024-09-02 16:58:23 +02:00
parent c77503ed45
commit 1b0dc0f27d
52 changed files with 817 additions and 1379 deletions

View File

@@ -4,42 +4,49 @@ export async function useCategories() {
const { data: categories, refresh }
= await useAsyncData<CategoryType[]>(async () => await useRequestFetch()('/api/categories'))
async function getCategory(id: number): CategoryType {
return categories.data.value.find(category => category.id === id)
}
async function createCategory(category: CreateCategorySchema) {
await $fetch('/api/categories', {
method: 'POST',
body: JSON.stringify(category),
})
.catch(error => useErrorToast('Category creation failed!', `Error: ${error}`))
await refresh()
await useSuccessToast('Category successfully created!')
try {
await useRequestFetch()('/api/categories', {
method: 'POST',
body: JSON.stringify(category),
})
await refresh()
await useSuccessToast('Category successfully created!', category.color)
}
catch (error) {
useErrorToast('Category creation failed!', error as string)
}
}
async function updateCategory(category: UpdateCategorySchema & { id: number }) {
await $fetch(`/api/categories/${category.id}`, {
method: 'PUT',
body: JSON.stringify(category),
})
.catch(error => useErrorToast('Category update failed!', `Error: ${error}`))
await refresh()
await useSuccessToast('Category successfully updated!')
try {
await $fetch(`/api/categories/${category.id}`, {
method: 'PUT',
body: JSON.stringify(category),
})
await refresh()
await useSuccessToast('Category successfully updated!')
}
catch (error) {
useErrorToast('Category update failed!', error as string)
}
}
async function deleteCategory(id: number) {
await $fetch(`/api/categories/${id}`, {
method: 'DELETE',
})
.catch(error => useErrorToast('Category deletion failed!', `Error: ${error}`))
await refresh()
await useSuccessToast('Category successfully deleted!')
try {
await $fetch(`/api/categories/${id}`, {
method: 'DELETE',
})
await refresh()
await useSuccessToast('Category successfully deleted!')
}
catch (error) {
useErrorToast('Category deletion failed!', error as string)
}
}
return {
categories,
getCategory,
createCategory,
updateCategory,
deleteCategory,