Files
arthome/app/composables/tabs.ts
2024-08-30 18:15:07 +02:00

68 lines
1.8 KiB
TypeScript

import type { CreateTabSchema, type TabType, UpdateTabSchema } from '~~/types/types'
export async function useTabs() {
const { data: tabs, refresh }
= await useAsyncData<TabType[]>(async () => await useRequestFetch()('/api/tabs'))
function getTabsForCategory(categoryId: number): TabType[] {
return tabs.value.filter(tab => tab.categoryId === categoryId)
}
async function createTab(tab: CreateTabSchema) {
await $fetch('/api/tabs', {
method: 'POST',
body: JSON.stringify(tab),
})
.then(async () => {
await refresh()
useSuccessToast('Tab successfully created!')
})
.catch(error => useErrorToast('Tab creation failed!', `Error: ${error}`))
}
async function updateTab(tab: UpdateTabSchema) {
await $fetch(`/api/tabs/${tab.id}`, {
method: 'PUT',
body: JSON.stringify(tab),
})
.then(async () => {
await refresh()
useSuccessToast('Tab successfully updated!')
})
.catch(error => useErrorToast('Tab update failed!', `Error: ${error}`))
}
async function setTabPrimary(tab, primary: boolean) {
await $fetch(`/api/tabs/${tab.id}`, {
method: 'PUT',
body: JSON.stringify({
primary,
categoryId: tab.categoryId,
}),
})
.then(async () => {
await refresh()
useSuccessToast('Tab favorite toggled with success!')
})
.catch(error => useErrorToast('Cannot toggle Tab favorite!', `Error: ${error}`))
}
async function deleteTab(id: number) {
await $fetch(`/api/tabs/${id}`, {
method: 'DELETE',
})
.catch(error => useErrorToast('Tab deletion failed!', `Error: ${error}`))
await refresh()
useSuccessToast('Tab successfully deleted!')
}
return {
tabs,
createTab,
deleteTab,
getTabsForCategory,
updateTab,
setTabPrimary,
}
}