small changes on stores

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-19 20:31:01 +01:00
parent 31bee89c1d
commit d78cb19886
5 changed files with 27 additions and 10 deletions

2
components.d.ts vendored
View file

@ -35,4 +35,4 @@ declare module 'vue' {
} }
} }
export {} export { }

View file

@ -6,6 +6,15 @@ import { useFetch } from './useFetch'
import { syncRef } from '@vueuse/core' import { syncRef } from '@vueuse/core'
const state = ref<Wishlist>() const state = ref<Wishlist>()
const isFinished = ref<boolean>(false)
const error = ref<any>()
const fetch = async (slugText: string) => {
const request = await useFetch(`/wishlist/${slugText}`).json()
state.value = request.data.value
isFinished.value = request.isFinished.value
error.value = request.error.value
}
const update = async (updatedData: Wishlist): Promise<void> => { const update = async (updatedData: Wishlist): Promise<void> => {
const id = state.value?.id const id = state.value?.id
@ -91,10 +100,9 @@ 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 = (slugText: string) => { export const useWishlistStore = () => {
const { isFinished, error, data } = useFetch(`/wishlist/${slugText}`).json()
syncRef(data, state)
return { return {
fetch,
state, state,
isFinished, isFinished,
error, error,

View file

@ -1,15 +1,21 @@
import { Wishlist } from '@/types' import { Wishlist } from '@/types'
import { syncRef } from '@vueuse/core'
import { ref } from 'vue' import { ref } from 'vue'
import { useFetch } from './useFetch' import { useFetch } from './useFetch'
const state = ref<Wishlist[]>([]) const state = ref<Wishlist[]>([])
const isFinished = ref<boolean>(false)
const error = ref<any>()
const fetch = async () => {
const request = await useFetch('/wishlist').json()
state.value = request.data.value
isFinished.value = request.isFinished.value
error.value = request.error.value
}
export const useWishlistsStore = () => { export const useWishlistsStore = () => {
const { isFinished, error, data } = useFetch('/wishlist').json()
syncRef(data, state)
return { return {
fetch,
state, state,
error, error,
isFinished, isFinished,

View file

@ -14,6 +14,7 @@ const { t } = useI18n()
const toast = useToast() const toast = useToast()
const { isActive: editModeIsActive } = useEditMode() const { isActive: editModeIsActive } = useEditMode()
const { const {
fetch,
state, state,
isFinished, isFinished,
update, update,
@ -22,7 +23,8 @@ const {
itemBought, itemBought,
itemDelete, itemDelete,
filteredItems, filteredItems,
} = useWishlistStore(route.params.slug as string) } = useWishlistStore()
fetch(route.params.slug as string)
const title = computed(() => { const title = computed(() => {
return state.value?.title return state.value?.title

View file

@ -3,7 +3,8 @@ import { useI18n } from 'vue-i18n'
import { useWishlistsStore } from '@/composables' import { useWishlistsStore } from '@/composables'
const { t } = useI18n() const { t } = useI18n()
const { state, isFinished } = useWishlistsStore() const { state, isFinished, fetch } = useWishlistsStore()
fetch()
</script> </script>
<template> <template>