mirror of
https://github.com/ArthurDanjou/arthome.git
synced 2026-02-04 04:57:49 +01:00
Working
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user