diff --git a/components/calendar/dialog/Delete.vue b/components/calendar/dialog/Delete.vue index cdad74e..ca24365 100644 --- a/components/calendar/dialog/Delete.vue +++ b/components/calendar/dialog/Delete.vue @@ -22,26 +22,25 @@ async function handleAction(): Promise { isLoading.value = true - try { - await $fetch(`/api/calendars/${props.calendar.id}`, { method: "DELETE" }) - emit("on-close") + const { error } = await tryCatch($fetch(`/api/calendars/${props.calendar.id}`, { method: "DELETE" })) + if (error) { toast({ - title: t("entity.calendar.deletedToast.title", { calendar: props.calendar.name }), - variant: "success", - duration: ToastLifetime.SHORT + title: error.message, + variant: "destructive" }) - } catch (err) { - console.log(err) - if (err instanceof Error) { - toast({ - title: err.message, - variant: "destructive" - }) - } - } finally { isLoading.value = false + return } + + toast({ + title: t("entity.calendar.deletedToast.title", { calendar: props.calendar.name }), + variant: "success", + duration: ToastLifetime.SHORT + }) + + emit("on-close") + isLoading.value = false } /** diff --git a/components/calendar/form/Create.vue b/components/calendar/form/Create.vue index 0899ef3..cd1056a 100644 --- a/components/calendar/form/Create.vue +++ b/components/calendar/form/Create.vue @@ -47,7 +47,7 @@ async function handleSubmit() { const { error } = await tryCatch($fetch("/api/calendars/create", { method: "POST", body: { ...calendarSkeleton.value, worldId: props.worldId } })) if (error) { - console.log(error) + console.log(error.message) isCreatingCalendar.value = false return } diff --git a/components/calendar/form/CreateEvent.vue b/components/calendar/form/CreateEvent.vue index a9636fc..4d20b93 100644 --- a/components/calendar/form/CreateEvent.vue +++ b/components/calendar/form/CreateEvent.vue @@ -29,17 +29,16 @@ async function handleSubmit() { isLoading.value = true - try { - await submitSkeleton() + const { error } = await tryCatch(submitSkeleton()) - emit("event-created") - } catch (err) { - if (err instanceof Error) { - formErrors.message = err.message - } - } finally { + if (error) { + formErrors.message = error.message isLoading.value = false + return } + + emit("event-created") + isLoading.value = false } /** diff --git a/components/calendar/form/DeleteCategory.vue b/components/calendar/form/DeleteCategory.vue index 096f49e..bf6982e 100644 --- a/components/calendar/form/DeleteCategory.vue +++ b/components/calendar/form/DeleteCategory.vue @@ -22,21 +22,20 @@ async function handleAction(): Promise { const categoryName = categorySkeleton.value?.name - try { - await deleteCategoryFromSkeleton() + const { error } = await tryCatch(deleteCategoryFromSkeleton()) - toast({ - title: t("entity.category.deletedToast.title", { category: categoryName }), - variant: "success", - duration: ToastLifetime.MEDIUM - }) - } catch (err) { - if (err instanceof Error) { - formErrors.message = err.message - } - } finally { + if (error) { + formErrors.message = error.message isLoading.value = false + return } + + toast({ + title: t("entity.category.deletedToast.title", { category: categoryName }), + variant: "success", + duration: ToastLifetime.MEDIUM + }) + isLoading.value = false } /** diff --git a/components/calendar/form/DeleteEvent.vue b/components/calendar/form/DeleteEvent.vue index e090675..ca18381 100644 --- a/components/calendar/form/DeleteEvent.vue +++ b/components/calendar/form/DeleteEvent.vue @@ -22,24 +22,23 @@ async function handleAction(): Promise { const eventTitle = eventSkeleton.value.title - try { - await deleteEventFromSkeleton() + const { error } = await tryCatch(deleteEventFromSkeleton()) - isDeleteEventModalOpen.value = false - resetSkeleton() - - toast({ - title: t("entity.calendar.event.deletedToast.title", { event: eventTitle }), - variant: "success", - duration: ToastLifetime.MEDIUM - }) - } catch (err) { - if (err instanceof Error) { - formErrors.message = err.message - } - } finally { + if (error) { + formErrors.message = error.message isLoading.value = false + return } + + isDeleteEventModalOpen.value = false + resetSkeleton() + + toast({ + title: t("entity.calendar.event.deletedToast.title", { event: eventTitle }), + variant: "success", + duration: ToastLifetime.MEDIUM + }) + isLoading.value = false } /** diff --git a/components/calendar/form/Update.vue b/components/calendar/form/Update.vue index 72d2713..a4dbdc1 100644 --- a/components/calendar/form/Update.vue +++ b/components/calendar/form/Update.vue @@ -34,16 +34,20 @@ const validSkeleton = computed(() => validSkeletonGeneral.value && validSkeleton const isUpdatingCalendar = ref(false) async function handleSubmit() { - try { - isUpdatingCalendar.value = true - await $fetch(`/api/calendars/${calendarSkeleton.value.id}`, { method: "PATCH", body: { ...calendarSkeleton.value, worldId: props.world?.id } }) + isUpdatingCalendar.value = true - emit("on-close") - } catch (err) { - console.log(err) - } finally { + const { error } = await tryCatch( + $fetch(`/api/calendars/${calendarSkeleton.value.id}`, { method: "PATCH", body: { ...calendarSkeleton.value, worldId: props.world?.id } }) + ) + + if (error) { + console.log(error.message) isUpdatingCalendar.value = false + return } + + emit("on-close") + isUpdatingCalendar.value = false } /** diff --git a/components/calendar/form/UpdateEvent.vue b/components/calendar/form/UpdateEvent.vue index 5e3118e..31c5dc0 100644 --- a/components/calendar/form/UpdateEvent.vue +++ b/components/calendar/form/UpdateEvent.vue @@ -23,19 +23,11 @@ async function handleAction() { isLoading.value = true - try { - await updateEventFromSkeleton() + const { error } = await tryCatch(updateEventFromSkeleton()) - emit("event-updated") - - toast({ - title: t("entity.calendar.event.updatedToast.title", { event: eventSkeleton.value.title }), - variant: "success", - duration: ToastLifetime.SHORT - }) - } catch (err) { + if (error) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - const apiError = (err as any).data as APIError + const apiError = (error as any).data as APIError apiError.data.errors.forEach((error) => { toast({ @@ -45,10 +37,21 @@ async function handleAction() { duration: ToastLifetime.MEDIUM, }) }) - } finally { - resetSkeleton() + isLoading.value = false + return } + + emit("event-updated") + + toast({ + title: t("entity.calendar.event.updatedToast.title", { event: eventSkeleton.value.title }), + variant: "success", + duration: ToastLifetime.SHORT + }) + + isLoading.value = false + resetSkeleton() } /** diff --git a/components/global/user/CTA.vue b/components/global/user/CTA.vue index 5075222..2d516e1 100644 --- a/components/global/user/CTA.vue +++ b/components/global/user/CTA.vue @@ -21,29 +21,27 @@ function closeMenu() { watch(user, closeMenu) async function handleGoogleLogin() { - try { - auth.signInWithOAuth({ - provider: "google", - options: { - queryParams: { - access_type: "offline", - prompt: "consent" - }, - redirectTo: profileUrl - } - }) - } catch (err) { - console.log(err) + const { error } = await auth.signInWithOAuth({ + provider: "google", + options: { + queryParams: { + access_type: "offline", + prompt: "consent" + }, + redirectTo: profileUrl + } + }) + + if (error) { + console.log(error.message) } } async function handleLogout() { - try { - const { error } = await auth.signOut() + const { error } = await auth.signOut() - if (error) throw error - } catch (err) { - console.log(err) + if (error) { + console.log(error.message) } } diff --git a/components/world/dialog/Delete.vue b/components/world/dialog/Delete.vue index e15f5e8..58ef904 100644 --- a/components/world/dialog/Delete.vue +++ b/components/world/dialog/Delete.vue @@ -17,31 +17,30 @@ const isLoading = ref(false) const emit = defineEmits(["on-close"]) async function handleAction(): Promise { - if (isLoading.value) return - if (!props.world) return + if (isLoading.value || !props.world) return isLoading.value = true - try { - await $fetch(`/api/worlds/${props.world.id}`, { method: "DELETE" }) - emit("on-close") + const { error } = await tryCatch( + $fetch(`/api/worlds/${props.world.id}`, { method: "DELETE" }) + ) + if (error) { toast({ - title: t("entity.world.deletedToast.title", { world: props.world.name }), - variant: "success", - duration: ToastLifetime.SHORT + title: error.message, + variant: "destructive" }) - } catch (err) { - console.log(err) - if (err instanceof Error) { - toast({ - title: err.message, - variant: "destructive" - }) - } - } finally { isLoading.value = false + return } + + toast({ + title: t("entity.world.deletedToast.title", { world: props.world.name }), + variant: "success", + duration: ToastLifetime.SHORT + }) + emit("on-close") + isLoading.value = false } /** diff --git a/components/world/form/Create.vue b/components/world/form/Create.vue index b9d41f6..84ba3af 100644 --- a/components/world/form/Create.vue +++ b/components/world/form/Create.vue @@ -22,16 +22,20 @@ const validSkeleton = computed(() => worldSkeleton.value.name) async function handleSubmit() { if (!user.value) return - try { - isLoading.value = true - await $fetch("/api/worlds/create", { method: "POST", body: { ...worldSkeleton.value } }) + isLoading.value = true - emit("on-close") - } catch (err) { - console.log(err) - } finally { + const { error } = await tryCatch( + $fetch("/api/worlds/create", { method: "POST", body: { ...worldSkeleton.value } }) + ) + + if (error) { + console.log(error.message) isLoading.value = false + return } + + emit("on-close") + isLoading.value = false } /** diff --git a/components/world/form/Update.vue b/components/world/form/Update.vue index 353c9f5..7050c31 100644 --- a/components/world/form/Update.vue +++ b/components/world/form/Update.vue @@ -30,22 +30,24 @@ const validSkeleton = computed(() => worldSkeleton.value.name) async function handleSubmit() { if (!user.value || !worldSkeleton.value) return - try { - isLoading.value = true - await $fetch(`/api/worlds/${worldSkeleton.value.id}`, { method: "PATCH", body: { ...worldSkeleton.value } }) + isLoading.value = true - toast({ - title: t("entity.world.updatedToast.title", { world: worldSkeleton.value.name }), - variant: "success", - duration: ToastLifetime.SHORT - }) + const { error } = await tryCatch( + $fetch(`/api/worlds/${worldSkeleton.value.id}`, { method: "PATCH", body: { ...worldSkeleton.value } }) + ) - emit("on-close") - } catch (err) { - console.log(err) - } finally { - isLoading.value = false + if (error) { + console.log(error.message) } + + toast({ + title: t("entity.world.updatedToast.title", { world: worldSkeleton.value.name }), + variant: "success", + duration: ToastLifetime.SHORT + }) + + emit("on-close") + isLoading.value = false } /** diff --git a/pages/my/index.vue b/pages/my/index.vue index ac987c0..b0e1fe9 100644 --- a/pages/my/index.vue +++ b/pages/my/index.vue @@ -38,22 +38,14 @@ function handleInsertedWorld(newWorld: WorldChannelPayload) { newWorld.createdAt = newWorld.created_at; newWorld.gmId = newWorld.gm_id; - try { - worlds.value?.data.push(newWorld) - } catch (err) { - console.log(err) - } + worlds.value?.data.push(newWorld) } /** Handles world deletion realtime events */ function handleDeletedWorld(id: number) { if (!worlds.value?.data) return - try { - worlds.value.data.splice(worlds.value.data.findIndex(w => w.id === id), 1) - } catch (err) { - console.log(err) - } + worlds.value.data.splice(worlds.value.data.findIndex(w => w.id === id), 1) } onMounted(() => { diff --git a/pages/my/worlds/[id].vue b/pages/my/worlds/[id].vue index 662d6d7..5a45cee 100644 --- a/pages/my/worlds/[id].vue +++ b/pages/my/worlds/[id].vue @@ -51,25 +51,17 @@ let worldChannel: RealtimeChannel function handleInsertedCalendar(newCalendar: CalendarChannelPayload) { if (!world.value) return - newCalendar.createdAt = newCalendar.created_at; - newCalendar.eventNb = [{ count: 0 }]; + newCalendar.createdAt = newCalendar.created_at + newCalendar.eventNb = [{ count: 0 }] - try { - world.value.data.calendars?.push(newCalendar) - } catch (err) { - console.log(err) - } + world.value.data.calendars?.push(newCalendar) } /** Handles calendar deletion realtime events */ function handleDeletedCalendar(id: number) { if (!world.value) return - try { - world.value.data.calendars?.splice(world.value.data.calendars.findIndex(c => c.id === id), 1) - } catch (err) { - console.log(err) - } + world.value.data.calendars?.splice(world.value.data.calendars.findIndex(c => c.id === id), 1) } onMounted(() => { diff --git a/stores/CalendarStore.ts b/stores/CalendarStore.ts index 5e6545f..5d37f8b 100644 --- a/stores/CalendarStore.ts +++ b/stores/CalendarStore.ts @@ -61,37 +61,33 @@ export const useCalendar = defineStore("calendar", () => { const months = ref([]) function setActiveCalendar(calendarData: Calendar) { - try { - if (!calendarData.id) return + if (!calendarData.id) return - activeCalendar.value = { - id: calendarData.id, - name: calendarData.name, - today: calendarData.today, - gmId: calendarData.world?.gmId - } - - setDefaultDate(activeCalendar.value.today) - selectDate(activeCalendar.value.today) - setReadStatus(activeCalendar.value.gmId!) - - if (!params.day) { - params.day = defaultDate.value.day.toString() - } - if (!params.month) { - params.month = defaultDate.value.month.toString() - } - if (!params.year) { - params.year = defaultDate.value.year.toString() - } - - months.value = calendarData.months - - baseEvents.value = calendarData.events - categories.value = calendarData.categories - } catch (err) { - console.log(err) + activeCalendar.value = { + id: calendarData.id, + name: calendarData.name, + today: calendarData.today, + gmId: calendarData.world?.gmId } + + setDefaultDate(activeCalendar.value.today) + selectDate(activeCalendar.value.today) + setReadStatus(activeCalendar.value.gmId!) + + if (!params.day) { + params.day = defaultDate.value.day.toString() + } + if (!params.month) { + params.month = defaultDate.value.month.toString() + } + if (!params.year) { + params.year = defaultDate.value.year.toString() + } + + months.value = calendarData.months + + baseEvents.value = calendarData.events + categories.value = calendarData.categories } const params = useUrlSearchParams("history", {