import type { Category } from "~/models/Category"; export const useCategoryStore = defineStore("calendar-category", () => { const { categories } = storeToRefs(useCalendar()) /** * Dummy event to hold creation data */ const isCreatingCategory = ref(false) const isUpdatingCategory = ref(false) const isDeletingCategory = ref(false) const categorySkeleton = ref(null); const operationInProgress = computed(() => isCreatingCategory.value || isUpdatingCategory.value || isDeletingCategory.value) let abortController: AbortController | null = null /** * Resets the dummy category data */ function resetSkeleton() { categorySkeleton.value = null } async function updateCategoryFromSkeleton() { if (!categorySkeleton.value) return abortController = new AbortController() isUpdatingCategory.value = true const body = categorySkeleton.value const { data, error } = await tryCatch( $fetch(`/api/calendars/categories/${categorySkeleton.value.id}`, { method: "PATCH", body, signal: abortController.signal }) ) if (error) { isUpdatingCategory.value = false throw error } // Update the category in the store categories.value = categories.value.map((category) => { if (category.id === data.id) { return { ...category, ...data } } return category }) abortController = null isUpdatingCategory.value = false } return { isCreatingCategory, isUpdatingCategory, isDeletingCategory, operationInProgress, categorySkeleton, resetSkeleton, updateCategoryFromSkeleton } })