From 6158b7a3958a790e498efd63facb05b569adf8dc Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Sat, 19 Feb 2022 01:27:02 +0100 Subject: [PATCH] use useFetch for Wishlist Signed-off-by: Benny Samir Hierl --- src/composables/useWishlistStore.ts | 79 +++++++++------------------- src/composables/useWishlistsStore.ts | 10 ++-- src/views/DetailView.vue | 9 ++-- 3 files changed, 35 insertions(+), 63 deletions(-) diff --git a/src/composables/useWishlistStore.ts b/src/composables/useWishlistStore.ts index 5617e04..bcbd319 100644 --- a/src/composables/useWishlistStore.ts +++ b/src/composables/useWishlistStore.ts @@ -1,24 +1,11 @@ -import { computed, ref } from 'vue' -import useAxios, { CustomAxiosError } from '@/composables/useAxios' +import { computed, ref, unref } from 'vue' import { Wishlist, WishlistItem } from '@/types' import { useEditMode } from './useEditMode' -const { client } = useAxios() const { isActive: editModeIsActive } = useEditMode() +import { useFetch } from './useFetch' +import { syncRef } from '@vueuse/core' const state = ref() -const isReady = ref(false) - -const fetch = async (slugText: string): Promise => { - try { - const { data } = await client.get(`/wishlist/${slugText}`) - state.value = data - isReady.value = true - } catch (e: CustomAxiosError | any) { - if (e.isAxiosError && !(e.ignore)) { - throw e - } - } -} const update = async (updatedData: Wishlist): Promise => { const id = state.value?.id @@ -26,16 +13,10 @@ const update = async (updatedData: Wishlist): Promise => { ...state.value, ...updatedData, } - try { - const { data } = await client.put(`/wishlist/${id}`, payload) - state.value = { - ...state.value, - ...data, - } - } catch (e: CustomAxiosError | any) { - if (e.isAxiosError && !(e.ignore)) { - throw e - } + const { data } = await useFetch(`/wishlist/${id}`).put(payload).json() + state.value = { + ...state.value, + ...(data.value), } } @@ -44,14 +25,8 @@ const createItem = async (values: WishlistItem): Promise => { const payload = { ...values, } - try { - const { data } = await client.post(`/wishlist/${id}/item`, payload) - state.value?.items?.push(data) - } catch (e: CustomAxiosError | any) { - if (e.isAxiosError && !(e.ignore)) { - throw e - } - } + const { data } = await useFetch(`/wishlist/${id}/item`).post(payload).json() + state.value?.items?.push(unref(data)) } const updateItem = async ( @@ -63,30 +38,24 @@ const updateItem = async ( ...currentValues, ...newValues, } - try { - const { data } = await client.put( - `/wishlist/${id}/item/${currentValues.id}`, - payload - ) - state.value?.items?.splice( - state.value.items.indexOf(currentValues), - 1, - data - ) - } catch (e: CustomAxiosError | any) { - if (e.isAxiosError && !(e.ignore)) { - throw e - } - } + + const { data } = await useFetch(`/wishlist/${id}/item/${currentValues.id}`) + .put(payload) + .json() + state.value?.items?.splice( + state.value.items.indexOf(currentValues), + 1, + unref(data) + ) } const itemBought = async (item: WishlistItem): Promise => { - await client.post(`/wishlist/${item.wishlistId}/item/${item.id}/bought`) + await useFetch(`/wishlist/${item.wishlistId}/item/${item.id}/bought`).post() item.bought = true } const itemDelete = async (item: WishlistItem): Promise => { - await client.delete(`/wishlist/${item.wishlistId}/item/${item.id}`) + await useFetch(`/wishlist/${item.wishlistId}/item/${item.id}`).delete() state.value?.items?.splice(state.value.items.indexOf(item), 1) } @@ -99,11 +68,13 @@ const filteredItems = computed(() => { return state.value.items.filter((item: WishlistItem) => item.bought === false) }) -export const useWishlistStore = () => { +export const useWishlistStore = (slugText: string) => { + const { isFinished, error, data } = useFetch(`/wishlist/${slugText}`).json() + syncRef(data, state) return { state, - isReady, - fetch, + isFinished, + error, update, createItem, updateItem, diff --git a/src/composables/useWishlistsStore.ts b/src/composables/useWishlistsStore.ts index ef462d1..a8a9069 100644 --- a/src/composables/useWishlistsStore.ts +++ b/src/composables/useWishlistsStore.ts @@ -1,12 +1,16 @@ import { Wishlist } from '@/types' -import { Ref } from 'vue' +import { syncRef } from '@vueuse/core' +import { ref } from 'vue' import { useFetch } from './useFetch' -const { isFinished, error, data } = useFetch('/wishlist').json() +const state = ref([]) export const useWishlistsStore = () => { + const { isFinished, error, data } = useFetch('/wishlist').json() + syncRef(data, state) + return { - state: data as Ref, + state, error, isFinished, } diff --git a/src/views/DetailView.vue b/src/views/DetailView.vue index 34dc3fc..ecfd049 100644 --- a/src/views/DetailView.vue +++ b/src/views/DetailView.vue @@ -15,15 +15,14 @@ const toast = useToast() const { isActive: editModeIsActive } = useEditMode() const { state, - fetch, - isReady, + isFinished, update, createItem, updateItem, itemBought, itemDelete, filteredItems, -} = useWishlistStore() +} = useWishlistStore(route.params.slug as string) const title = computed(() => { return state.value?.title @@ -85,12 +84,10 @@ const handleDeleteItem = async (item: WishlistItemType): Promise => { itemDelete(item) } } - -await fetch(route.params.slug as string)