Skip to content

Commit

Permalink
Add toast error
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed Dec 3, 2024
1 parent bdbae69 commit f4a699e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { $Fetch } from 'ofetch'

declare global {
var $fetch: $Fetch
}
15 changes: 15 additions & 0 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,28 @@
</template>

<script setup lang="ts">
const toast = useToast()
const { appTheme } = useAppTheme()
useHead({
htmlAttrs: {
class: [appTheme],
},
title: 'DistApp',
})
globalThis.$fetch = globalThis.$fetch.create({
onResponseError(response) {
if (!response.response.ok) {
toast.add({
severity: 'error',
summary: 'Error',
detail: normalizeError(response),
life: 3000,
})
}
}
})
</script>

<style lang="scss">
Expand Down
4 changes: 2 additions & 2 deletions components/BottomMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const cookie = useCookie(cookieAuthKey)
const cookiePayload = cookie.value ? decodeJwt(cookie.value) : undefined
const userEmail = cookiePayload?.email?.toString()
const signout = () => {
if (process.client) {
if (import.meta.client) {
const userTokens = useLocalStorage<UserTokenInfo[]>(userTokensKey, [])
try {
if (cookie.value) {
Expand All @@ -30,7 +30,7 @@ const signout = () => {
} catch (e) {
console.error('Error decode on signout', e)
}
cookie.value = userTokens.value.find(e => new Boolean(e))?.token
cookie.value = userTokens.value.find(e => e ? true : false)?.token
navigateTo({
name: 'index',
})
Expand Down
5 changes: 4 additions & 1 deletion utils/showErrorAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export function normalizeError(error: FetchContext | FetchError): string {
return undefined
}
}
const message = error.response?._data?.message || error.response?.statusText || getFetchError()?.data?.message || 'Unexpected happen'
const message: string = error.response?._data?.message || error.response?.statusText || getFetchError()?.data?.message || 'Unexpected happen'
if (message.includes(`undefined (reading 'userId')`)) {
return 'Unauthorized. Try login again'
}
return message
}

Expand Down

0 comments on commit f4a699e

Please sign in to comment.