mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-06-07 05:57:41 +00:00
some changes...
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
1fc2cc0233
commit
b41843345b
7 changed files with 102 additions and 90 deletions
37
src/App.vue
37
src/App.vue
|
@ -1,8 +1,43 @@
|
|||
<template>
|
||||
<div class="app max-w-[900px] mx-auto p-10">
|
||||
<main>
|
||||
<router-view />
|
||||
<router-view v-slot="{ Component }">
|
||||
<template v-if="Component">
|
||||
<keep-alive>
|
||||
<div
|
||||
v-if="error"
|
||||
class="flex flex-row space-x-2 items-center content-center justify-center m-20 text-red-500"
|
||||
>
|
||||
<IconError class="w-4 h-4" />
|
||||
<span>Es ist ein Fehler aufgetreten...</span>
|
||||
</div>
|
||||
<suspense v-else>
|
||||
<template #default>
|
||||
<component :is="Component"></component>
|
||||
</template>
|
||||
<template #fallback>
|
||||
<div
|
||||
class="flex flex-row space-x-2 items-center content-center justify-center m-20"
|
||||
>
|
||||
<IconSpinner class="w-4 h-4" />
|
||||
<span> Lade... </span>
|
||||
</div>
|
||||
</template>
|
||||
</suspense>
|
||||
</keep-alive>
|
||||
</template>
|
||||
</router-view>
|
||||
</main>
|
||||
<modal-overlay />
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { onErrorCaptured } from 'vue'
|
||||
import useAxios from './composables/useAxios'
|
||||
import { IconSpinner, IconError } from '@/components/icons'
|
||||
const { error } = useAxios()
|
||||
onErrorCaptured((e: any) => {
|
||||
error.value = e
|
||||
return true
|
||||
})
|
||||
</script>
|
||||
|
|
45
src/composables/useAxios.ts
Normal file
45
src/composables/useAxios.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||
import { apiConfig } from '@/config'
|
||||
import { ref } from 'vue'
|
||||
|
||||
const isLoading = ref(false)
|
||||
const error = ref(null)
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
baseURL: apiConfig.baseURL,
|
||||
}
|
||||
|
||||
const client: AxiosInstance = axios.create(config)
|
||||
|
||||
client.interceptors.request.use(
|
||||
function (config) {
|
||||
isLoading.value = true
|
||||
error.value = null
|
||||
return config
|
||||
},
|
||||
function (err) {
|
||||
isLoading.value = false
|
||||
error.value = err
|
||||
return Promise.reject(err)
|
||||
}
|
||||
)
|
||||
|
||||
client.interceptors.response.use(
|
||||
function (response) {
|
||||
isLoading.value = false
|
||||
return response
|
||||
},
|
||||
function (err) {
|
||||
isLoading.value = false
|
||||
error.value = err
|
||||
return Promise.reject(err)
|
||||
}
|
||||
)
|
||||
|
||||
export default () => {
|
||||
return {
|
||||
client,
|
||||
isLoading,
|
||||
error,
|
||||
}
|
||||
}
|
|
@ -1,35 +1,23 @@
|
|||
import apiService from '@/services/apiService'
|
||||
import useAxios from '@/composables/useAxios'
|
||||
import { Wishlist, WishlistItem } from '@/types'
|
||||
import { ref } from 'vue'
|
||||
const apiClient = apiService.getClient()
|
||||
const { client } = useAxios()
|
||||
|
||||
const refState = ref<Wishlist | any>({})
|
||||
const isLoading = ref(false)
|
||||
const hasError = ref(false)
|
||||
|
||||
const fetchBySlugUrl = async (slugText: string): Promise<void> => {
|
||||
isLoading.value = true
|
||||
try {
|
||||
const { data } = await apiClient.get(`/wishlist/${slugText}`)
|
||||
refState.value = data
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
hasError.value = true
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
const fetch = async (slugText: string): Promise<void> => {
|
||||
const { data } = await client.get(`/wishlist/${slugText}`)
|
||||
refState.value = data
|
||||
}
|
||||
|
||||
const updateItem = async (item: WishlistItem): Promise<void> => {
|
||||
await apiClient.put(`/wishlist/${item.wishlistId}/item/${item.id}`, item)
|
||||
await client.put(`/wishlist/${item.wishlistId}/item/${item.id}`, item)
|
||||
}
|
||||
|
||||
export const useWishlistStore = (slugText: string) => {
|
||||
fetchBySlugUrl(slugText)
|
||||
export const useWishlistStore = () => {
|
||||
return {
|
||||
list: refState,
|
||||
isLoading,
|
||||
hasError,
|
||||
fetch,
|
||||
updateItem,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,19 @@
|
|||
import apiService from '@/services/apiService'
|
||||
import useAxios from '@/composables/useAxios'
|
||||
import { Wishlist } from '@/types'
|
||||
import { ref } from 'vue'
|
||||
const apiClient = apiService.getClient()
|
||||
const { client } = useAxios()
|
||||
const prefix = '/wishlist'
|
||||
|
||||
const refState = ref<Wishlist[]>([])
|
||||
const isLoading = ref(false)
|
||||
const hasError = ref(false)
|
||||
|
||||
export const loadAll = async (): Promise<void> => {
|
||||
isLoading.value = true
|
||||
try {
|
||||
const { data } = await apiClient.get(prefix)
|
||||
refState.value = data
|
||||
} catch (error: any) {
|
||||
hasError.value = true
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
export const fetch = async (): Promise<void> => {
|
||||
const { data } = await client.get(prefix)
|
||||
refState.value = data
|
||||
}
|
||||
|
||||
export const useWishlistsStore = () => {
|
||||
loadAll()
|
||||
return {
|
||||
lists: refState,
|
||||
hasError,
|
||||
isLoading,
|
||||
fetch,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||
import { apiConfig } from '@/config'
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
baseURL: apiConfig.baseURL,
|
||||
}
|
||||
|
||||
const client: AxiosInstance = axios.create(config)
|
||||
|
||||
export default {
|
||||
getClient: () => {
|
||||
return client
|
||||
},
|
||||
}
|
|
@ -1,19 +1,16 @@
|
|||
<script setup lang="ts">
|
||||
import { WishlistItem as WishlistItemType } from '@/types'
|
||||
import { computed, watchEffect } from 'vue'
|
||||
import { computed } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { useTitle } from '@vueuse/core'
|
||||
import { useWishlistStore, useModal } from '@/composables'
|
||||
import Tile from '@/components/Tile.vue'
|
||||
import { IconSpinner, IconError } from '@/components/icons'
|
||||
import WishlistItem from '@/components/WishlistItem.vue'
|
||||
|
||||
const route = useRoute()
|
||||
const modal = useModal()
|
||||
|
||||
const { list, isLoading, hasError, updateItem } = useWishlistStore(
|
||||
route.params.slug as string
|
||||
)
|
||||
const { list, fetch, updateItem } = useWishlistStore()
|
||||
await fetch(route.params.slug as string)
|
||||
|
||||
const notBoughtItems = computed(() => {
|
||||
return list.value.items.filter(
|
||||
|
@ -34,21 +31,7 @@ const bought = async (item: WishlistItemType): Promise<void> => {
|
|||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="isLoading"
|
||||
class="flex flex-row space-x-2 items-center content-center justify-center m-20"
|
||||
>
|
||||
<IconSpinner class="w-4 h-4" />
|
||||
<span> Lade Wunschliste... </span>
|
||||
</div>
|
||||
<div
|
||||
v-else-if="hasError"
|
||||
class="flex flex-row space-x-2 items-center content-center justify-center m-20 text-red-500"
|
||||
>
|
||||
<IconError class="w-4 - h-4" />
|
||||
<span> Es ist ein Fehler aufgetreten... </span>
|
||||
</div>
|
||||
<div v-else>
|
||||
<div v-if="list.id">
|
||||
<div
|
||||
class="flex flex-col md:flex-row space-x-0 md:space-x-6 space-y-2 md:space-y-0 items-center"
|
||||
>
|
||||
|
|
|
@ -1,27 +1,13 @@
|
|||
<script setup lang="ts">
|
||||
import Tile from '@/components/Tile.vue'
|
||||
import { IconSpinner, IconError } from '@/components/icons'
|
||||
import { useWishlistsStore } from '@/composables'
|
||||
const { lists, isLoading, hasError } = useWishlistsStore()
|
||||
const { lists, fetch } = useWishlistsStore()
|
||||
await fetch()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1 class="text-3xl text-center">Wunschlisten</h1>
|
||||
<div
|
||||
v-if="isLoading"
|
||||
class="flex flex-row space-x-2 items-center content-center justify-center m-20"
|
||||
>
|
||||
<IconSpinner class="w-4 h-4" />
|
||||
<span> Lade Wunschlisten... </span>
|
||||
</div>
|
||||
<div
|
||||
v-else-if="hasError"
|
||||
class="flex flex-row space-x-2 items-center content-center justify-center m-20 text-red-500"
|
||||
>
|
||||
<IconError class="w-4 - h-4" />
|
||||
<span> Es ist ein Fehler aufgetreten... </span>
|
||||
</div>
|
||||
<div v-else class="flex flex-row flex-wrap justify-around p-10">
|
||||
<div v-if="lists" class="flex flex-row flex-wrap justify-around p-10">
|
||||
<router-link
|
||||
v-for="(item, index) in lists"
|
||||
:key="index"
|
||||
|
|
Loading…
Add table
Reference in a new issue