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>
|
<template>
|
||||||
<div class="app max-w-[900px] mx-auto p-10">
|
<div class="app max-w-[900px] mx-auto p-10">
|
||||||
<main>
|
<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>
|
</main>
|
||||||
<modal-overlay />
|
<modal-overlay />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</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 { Wishlist, WishlistItem } from '@/types'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
const apiClient = apiService.getClient()
|
const { client } = useAxios()
|
||||||
|
|
||||||
const refState = ref<Wishlist | any>({})
|
const refState = ref<Wishlist | any>({})
|
||||||
const isLoading = ref(false)
|
|
||||||
const hasError = ref(false)
|
|
||||||
|
|
||||||
const fetchBySlugUrl = async (slugText: string): Promise<void> => {
|
const fetch = async (slugText: string): Promise<void> => {
|
||||||
isLoading.value = true
|
const { data } = await client.get(`/wishlist/${slugText}`)
|
||||||
try {
|
|
||||||
const { data } = await apiClient.get(`/wishlist/${slugText}`)
|
|
||||||
refState.value = data
|
refState.value = data
|
||||||
} catch (error: any) {
|
|
||||||
console.error(error)
|
|
||||||
hasError.value = true
|
|
||||||
} finally {
|
|
||||||
isLoading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateItem = async (item: WishlistItem): Promise<void> => {
|
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) => {
|
export const useWishlistStore = () => {
|
||||||
fetchBySlugUrl(slugText)
|
|
||||||
return {
|
return {
|
||||||
list: refState,
|
list: refState,
|
||||||
isLoading,
|
fetch,
|
||||||
hasError,
|
|
||||||
updateItem,
|
updateItem,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,19 @@
|
||||||
import apiService from '@/services/apiService'
|
import useAxios from '@/composables/useAxios'
|
||||||
import { Wishlist } from '@/types'
|
import { Wishlist } from '@/types'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
const apiClient = apiService.getClient()
|
const { client } = useAxios()
|
||||||
const prefix = '/wishlist'
|
const prefix = '/wishlist'
|
||||||
|
|
||||||
const refState = ref<Wishlist[]>([])
|
const refState = ref<Wishlist[]>([])
|
||||||
const isLoading = ref(false)
|
|
||||||
const hasError = ref(false)
|
|
||||||
|
|
||||||
export const loadAll = async (): Promise<void> => {
|
export const fetch = async (): Promise<void> => {
|
||||||
isLoading.value = true
|
const { data } = await client.get(prefix)
|
||||||
try {
|
|
||||||
const { data } = await apiClient.get(prefix)
|
|
||||||
refState.value = data
|
refState.value = data
|
||||||
} catch (error: any) {
|
|
||||||
hasError.value = true
|
|
||||||
} finally {
|
|
||||||
isLoading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useWishlistsStore = () => {
|
export const useWishlistsStore = () => {
|
||||||
loadAll()
|
|
||||||
return {
|
return {
|
||||||
lists: refState,
|
lists: refState,
|
||||||
hasError,
|
fetch,
|
||||||
isLoading,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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">
|
<script setup lang="ts">
|
||||||
import { WishlistItem as WishlistItemType } from '@/types'
|
import { WishlistItem as WishlistItemType } from '@/types'
|
||||||
import { computed, watchEffect } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { useRoute } from 'vue-router'
|
import { useRoute } from 'vue-router'
|
||||||
import { useTitle } from '@vueuse/core'
|
|
||||||
import { useWishlistStore, useModal } from '@/composables'
|
import { useWishlistStore, useModal } from '@/composables'
|
||||||
import Tile from '@/components/Tile.vue'
|
import Tile from '@/components/Tile.vue'
|
||||||
import { IconSpinner, IconError } from '@/components/icons'
|
|
||||||
import WishlistItem from '@/components/WishlistItem.vue'
|
import WishlistItem from '@/components/WishlistItem.vue'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const modal = useModal()
|
const modal = useModal()
|
||||||
|
|
||||||
const { list, isLoading, hasError, updateItem } = useWishlistStore(
|
const { list, fetch, updateItem } = useWishlistStore()
|
||||||
route.params.slug as string
|
await fetch(route.params.slug as string)
|
||||||
)
|
|
||||||
|
|
||||||
const notBoughtItems = computed(() => {
|
const notBoughtItems = computed(() => {
|
||||||
return list.value.items.filter(
|
return list.value.items.filter(
|
||||||
|
@ -34,21 +31,7 @@ const bought = async (item: WishlistItemType): Promise<void> => {
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div
|
<div v-if="list.id">
|
||||||
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
|
<div
|
||||||
class="flex flex-col md:flex-row space-x-0 md:space-x-6 space-y-2 md:space-y-0 items-center"
|
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">
|
<script setup lang="ts">
|
||||||
import Tile from '@/components/Tile.vue'
|
import Tile from '@/components/Tile.vue'
|
||||||
import { IconSpinner, IconError } from '@/components/icons'
|
|
||||||
import { useWishlistsStore } from '@/composables'
|
import { useWishlistsStore } from '@/composables'
|
||||||
const { lists, isLoading, hasError } = useWishlistsStore()
|
const { lists, fetch } = useWishlistsStore()
|
||||||
|
await fetch()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<h1 class="text-3xl text-center">Wunschlisten</h1>
|
<h1 class="text-3xl text-center">Wunschlisten</h1>
|
||||||
<div
|
<div v-if="lists" class="flex flex-row flex-wrap justify-around p-10">
|
||||||
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">
|
|
||||||
<router-link
|
<router-link
|
||||||
v-for="(item, index) in lists"
|
v-for="(item, index) in lists"
|
||||||
:key="index"
|
:key="index"
|
||||||
|
|
Loading…
Add table
Reference in a new issue