use useFetch for Wishlist

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-19 01:27:02 +01:00
parent d4720ffd3e
commit 6158b7a395
3 changed files with 35 additions and 63 deletions

View file

@ -1,24 +1,11 @@
import { computed, ref } from 'vue' import { computed, ref, unref } from 'vue'
import useAxios, { CustomAxiosError } from '@/composables/useAxios'
import { Wishlist, WishlistItem } from '@/types' import { Wishlist, WishlistItem } from '@/types'
import { useEditMode } from './useEditMode' import { useEditMode } from './useEditMode'
const { client } = useAxios()
const { isActive: editModeIsActive } = useEditMode() const { isActive: editModeIsActive } = useEditMode()
import { useFetch } from './useFetch'
import { syncRef } from '@vueuse/core'
const state = ref<Wishlist>() const state = ref<Wishlist>()
const isReady = ref(false)
const fetch = async (slugText: string): Promise<void> => {
try {
const { data } = await client.get(`/wishlist/${slugText}`)
state.value = data
isReady.value = true
} catch (e: CustomAxiosError | any) {
if (e.isAxiosError && !(<CustomAxiosError>e.ignore)) {
throw e
}
}
}
const update = async (updatedData: Wishlist): Promise<void> => { const update = async (updatedData: Wishlist): Promise<void> => {
const id = state.value?.id const id = state.value?.id
@ -26,16 +13,10 @@ const update = async (updatedData: Wishlist): Promise<void> => {
...state.value, ...state.value,
...updatedData, ...updatedData,
} }
try { const { data } = await useFetch(`/wishlist/${id}`).put(payload).json()
const { data } = await client.put(`/wishlist/${id}`, payload) state.value = {
state.value = { ...state.value,
...state.value, ...(<Wishlist>data.value),
...data,
}
} catch (e: CustomAxiosError | any) {
if (e.isAxiosError && !(<CustomAxiosError>e.ignore)) {
throw e
}
} }
} }
@ -44,14 +25,8 @@ const createItem = async (values: WishlistItem): Promise<void> => {
const payload = { const payload = {
...values, ...values,
} }
try { const { data } = await useFetch(`/wishlist/${id}/item`).post(payload).json()
const { data } = await client.post(`/wishlist/${id}/item`, payload) state.value?.items?.push(unref(data))
state.value?.items?.push(data)
} catch (e: CustomAxiosError | any) {
if (e.isAxiosError && !(<CustomAxiosError>e.ignore)) {
throw e
}
}
} }
const updateItem = async ( const updateItem = async (
@ -63,30 +38,24 @@ const updateItem = async (
...currentValues, ...currentValues,
...newValues, ...newValues,
} }
try {
const { data } = await client.put( const { data } = await useFetch(`/wishlist/${id}/item/${currentValues.id}`)
`/wishlist/${id}/item/${currentValues.id}`, .put(payload)
payload .json()
) state.value?.items?.splice(
state.value?.items?.splice( state.value.items.indexOf(currentValues),
state.value.items.indexOf(currentValues), 1,
1, unref(data)
data )
)
} catch (e: CustomAxiosError | any) {
if (e.isAxiosError && !(<CustomAxiosError>e.ignore)) {
throw e
}
}
} }
const itemBought = async (item: WishlistItem): Promise<void> => { const itemBought = async (item: WishlistItem): Promise<void> => {
await client.post(`/wishlist/${item.wishlistId}/item/${item.id}/bought`) await useFetch(`/wishlist/${item.wishlistId}/item/${item.id}/bought`).post()
item.bought = true item.bought = true
} }
const itemDelete = async (item: WishlistItem): Promise<void> => { const itemDelete = async (item: WishlistItem): Promise<void> => {
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) 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) 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 { return {
state, state,
isReady, isFinished,
fetch, error,
update, update,
createItem, createItem,
updateItem, updateItem,

View file

@ -1,12 +1,16 @@
import { Wishlist } from '@/types' import { Wishlist } from '@/types'
import { Ref } from 'vue' import { syncRef } from '@vueuse/core'
import { ref } from 'vue'
import { useFetch } from './useFetch' import { useFetch } from './useFetch'
const { isFinished, error, data } = useFetch('/wishlist').json() const state = ref<Wishlist[]>([])
export const useWishlistsStore = () => { export const useWishlistsStore = () => {
const { isFinished, error, data } = useFetch('/wishlist').json()
syncRef(data, state)
return { return {
state: data as Ref<Wishlist[]>, state,
error, error,
isFinished, isFinished,
} }

View file

@ -15,15 +15,14 @@ const toast = useToast()
const { isActive: editModeIsActive } = useEditMode() const { isActive: editModeIsActive } = useEditMode()
const { const {
state, state,
fetch, isFinished,
isReady,
update, update,
createItem, createItem,
updateItem, updateItem,
itemBought, itemBought,
itemDelete, itemDelete,
filteredItems, filteredItems,
} = useWishlistStore() } = useWishlistStore(route.params.slug as string)
const title = computed(() => { const title = computed(() => {
return state.value?.title return state.value?.title
@ -85,12 +84,10 @@ const handleDeleteItem = async (item: WishlistItemType): Promise<void> => {
itemDelete(item) itemDelete(item)
} }
} }
await fetch(route.params.slug as string)
</script> </script>
<template> <template>
<div v-if="isReady" class="h-full"> <div v-if="isFinished" class="h-full">
<div <div
class="flex flex-col items-center space-x-0 space-y-2 md:flex-row md:space-x-6 md:space-y-0" class="flex flex-col items-center space-x-0 space-y-2 md:flex-row md:space-x-6 md:space-y-0"
v-if="state !== undefined" v-if="state !== undefined"