mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-06-07 05:57:41 +00:00
change from axios to useFetch for get wishlists
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
ea83fa4a68
commit
e94423a5b6
6 changed files with 44 additions and 18 deletions
2
components.d.ts
vendored
2
components.d.ts
vendored
|
@ -35,4 +35,4 @@ declare module 'vue' {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {}
|
export { }
|
||||||
|
|
|
@ -38,6 +38,7 @@ const { t } = useI18n()
|
||||||
const error = ref()
|
const error = ref()
|
||||||
|
|
||||||
onErrorCaptured((e: unknown) => {
|
onErrorCaptured((e: unknown) => {
|
||||||
|
console.error(e)
|
||||||
error.value = e
|
error.value = e
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,3 +4,4 @@ export * from './useModal'
|
||||||
export * from './useAxios'
|
export * from './useAxios'
|
||||||
export * from './useAuth'
|
export * from './useAuth'
|
||||||
export * from './useEditMode'
|
export * from './useEditMode'
|
||||||
|
export * from './useFetch'
|
||||||
|
|
24
src/composables/useFetch.ts
Normal file
24
src/composables/useFetch.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { apiConfig } from '@/config'
|
||||||
|
import { useAuth } from './useAuth'
|
||||||
|
import { createFetch } from '@vueuse/core'
|
||||||
|
|
||||||
|
const { token } = useAuth()
|
||||||
|
|
||||||
|
export const useFetch = createFetch({
|
||||||
|
baseUrl: apiConfig.baseURL,
|
||||||
|
options: {
|
||||||
|
async beforeFetch({ options }) {
|
||||||
|
if (token.value) {
|
||||||
|
options.headers = {
|
||||||
|
...options.headers,
|
||||||
|
Authorization: `API-Key ${token.value}`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { options }
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fetchOptions: {
|
||||||
|
mode: 'cors',
|
||||||
|
},
|
||||||
|
})
|
|
@ -1,19 +1,13 @@
|
||||||
import useAxios from '@/composables/useAxios'
|
|
||||||
import { Wishlist } from '@/types'
|
import { Wishlist } from '@/types'
|
||||||
import { readonly, ref } from 'vue'
|
import { Ref } from 'vue'
|
||||||
const { client } = useAxios()
|
import { useFetch } from './useFetch'
|
||||||
const prefix = '/wishlist'
|
|
||||||
|
|
||||||
const state = ref<Wishlist[]>([])
|
const { isFinished, error, data } = useFetch('/wishlist').json()
|
||||||
|
|
||||||
export const fetch = async (): Promise<void> => {
|
|
||||||
const { data } = await client.get(prefix)
|
|
||||||
state.value = data
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useWishlistsStore = () => {
|
export const useWishlistsStore = () => {
|
||||||
return {
|
return {
|
||||||
state: readonly(state),
|
state: data as Ref<Wishlist[]>,
|
||||||
fetch,
|
error,
|
||||||
|
isFinished,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,7 @@ import { useI18n } from 'vue-i18n'
|
||||||
import { useWishlistsStore } from '@/composables'
|
import { useWishlistsStore } from '@/composables'
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
const { state: wishlists, fetch } = useWishlistsStore()
|
const { state, isFinished } = useWishlistsStore()
|
||||||
await fetch()
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
@ -12,12 +11,19 @@ await fetch()
|
||||||
{{ t('common.app-title.text') }}
|
{{ t('common.app-title.text') }}
|
||||||
</h1>
|
</h1>
|
||||||
<div
|
<div
|
||||||
v-if="wishlists.length > 0"
|
v-if="!isFinished"
|
||||||
|
class="m-20 flex flex-row content-center items-center justify-center space-x-2"
|
||||||
|
>
|
||||||
|
<IconSpinner class="h-4 w-4" />
|
||||||
|
<span> {{ t('common.loading.text') }} </span>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
v-else-if="state.length > 0"
|
||||||
class="flex flex-row flex-wrap justify-around p-10"
|
class="flex flex-row flex-wrap justify-around p-10"
|
||||||
>
|
>
|
||||||
<router-link
|
<router-link
|
||||||
v-for="(item, index) in wishlists"
|
v-for="item in state"
|
||||||
:key="index"
|
:key="item.id"
|
||||||
:to="'/' + item.slugUrlText"
|
:to="'/' + item.slugUrlText"
|
||||||
>
|
>
|
||||||
<ImageTile :title="item.title" :image-src="item.imageSrc" class="m-4" />
|
<ImageTile :title="item.title" :image-src="item.imageSrc" class="m-4" />
|
||||||
|
|
Loading…
Add table
Reference in a new issue