From 31bee89c1d419b5682c4fee521be6c2e23be22ef Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sat, 19 Feb 2022 01:27:25 +0100 Subject: [PATCH] some improvments Signed-off-by: Benny Samir Hierl --- components.d.ts | 2 +- src/App.vue | 17 ++---- src/composables/useFetch.ts | 10 +++- src/composables/useWishlistStore.ts | 33 +++++++++-- src/views/DetailView.vue | 91 ++++++++++++++++------------- src/views/NotFoundView.vue | 2 + 6 files changed, 93 insertions(+), 62 deletions(-) diff --git a/components.d.ts b/components.d.ts index 32eebef..cbe40ef 100644 --- a/components.d.ts +++ b/components.d.ts @@ -35,4 +35,4 @@ declare module 'vue' { } } -export { } +export {} diff --git a/src/App.vue b/src/App.vue index 65c4594..bc4f260 100644 --- a/src/App.vue +++ b/src/App.vue @@ -11,19 +11,7 @@ {{ t('errors.generic.text') }} - - - - + @@ -31,6 +19,7 @@ diff --git a/src/composables/useFetch.ts b/src/composables/useFetch.ts index f7f5d7f..276c5d0 100644 --- a/src/composables/useFetch.ts +++ b/src/composables/useFetch.ts @@ -1,13 +1,14 @@ import { apiConfig } from '@/config' import { useAuth } from './useAuth' import { createFetch } from '@vueuse/core' +import router from '../router' const { token } = useAuth() export const useFetch = createFetch({ baseUrl: apiConfig.baseURL, options: { - async beforeFetch({ options }) { + beforeFetch({ options }) { if (token.value) { options.headers = { ...options.headers, @@ -17,5 +18,12 @@ export const useFetch = createFetch({ return { options } }, + onFetchError(ctx) { + if (ctx.data && ctx.data.statusCode === 404) { + router.push({ name: 'notFound' }) + } + + return ctx + }, }, }) diff --git a/src/composables/useWishlistStore.ts b/src/composables/useWishlistStore.ts index bcbd319..bab0eb0 100644 --- a/src/composables/useWishlistStore.ts +++ b/src/composables/useWishlistStore.ts @@ -13,7 +13,10 @@ const update = async (updatedData: Wishlist): Promise => { ...state.value, ...updatedData, } - const { data } = await useFetch(`/wishlist/${id}`).put(payload).json() + const { data, error } = await useFetch(`/wishlist/${id}`).put(payload).json() + if (error.value) { + throw error.value + } state.value = { ...state.value, ...(data.value), @@ -25,7 +28,12 @@ const createItem = async (values: WishlistItem): Promise => { const payload = { ...values, } - const { data } = await useFetch(`/wishlist/${id}/item`).post(payload).json() + const { data, error } = await useFetch(`/wishlist/${id}/item`) + .post(payload) + .json() + if (error.value) { + throw error.value + } state.value?.items?.push(unref(data)) } @@ -39,9 +47,14 @@ const updateItem = async ( ...newValues, } - const { data } = await useFetch(`/wishlist/${id}/item/${currentValues.id}`) + const { data, error } = await useFetch( + `/wishlist/${id}/item/${currentValues.id}` + ) .put(payload) .json() + if (error.value) { + throw error.value + } state.value?.items?.splice( state.value.items.indexOf(currentValues), 1, @@ -50,12 +63,22 @@ const updateItem = async ( } const itemBought = async (item: WishlistItem): Promise => { - await useFetch(`/wishlist/${item.wishlistId}/item/${item.id}/bought`).post() + const { error } = await useFetch( + `/wishlist/${item.wishlistId}/item/${item.id}/bought` + ).post() + if (error.value) { + throw error.value + } item.bought = true } const itemDelete = async (item: WishlistItem): Promise => { - await useFetch(`/wishlist/${item.wishlistId}/item/${item.id}`).delete() + const { error } = await useFetch( + `/wishlist/${item.wishlistId}/item/${item.id}` + ).delete() + if (error.value) { + throw error.value + } state.value?.items?.splice(state.value.items.indexOf(item), 1) } diff --git a/src/views/DetailView.vue b/src/views/DetailView.vue index ecfd049..cebdd02 100644 --- a/src/views/DetailView.vue +++ b/src/views/DetailView.vue @@ -87,53 +87,60 @@ const handleDeleteItem = async (item: WishlistItemType): Promise => {