From 64694d88722df428730aed0cdc3e571bb4910f85 Mon Sep 17 00:00:00 2001 From: herirudini Date: Thu, 26 Feb 2026 15:52:53 +0700 Subject: [PATCH] feat: fix loader interceptor parameter, implement toast error interceptor --- src/App.vue | 12 ++++++------ src/services/api.ts | 9 +++++++-- src/services/product.service.ts | 4 ++-- src/stores/toast.store.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/stores/toast.store.ts diff --git a/src/App.vue b/src/App.vue index 70fcf90..8edb018 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,19 +1,19 @@ diff --git a/src/services/api.ts b/src/services/api.ts index 0399b96..5014d63 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -1,6 +1,7 @@ import axios from 'axios' import { useLoadingStore } from '@/stores/loading.store' import router from '@/router' +import { useToastStore } from '@/stores/toast.store' const api = axios.create({ baseURL: import.meta.env.VITE_API_URL, @@ -10,8 +11,8 @@ const api = axios.create({ // Request Interceptor api.interceptors.request.use((config) => { console.log('BASE URL:', import.meta.env.VITE_API_URL) - const loader = config.params['loader'] ? JSON.parse(config.params['loader']) : true; - if (loader) { + const loader = config.params?.loader; + if (loader !== false) { const loadingStore = useLoadingStore() loadingStore.start() } @@ -33,9 +34,13 @@ api.interceptors.response.use( return response }, (error) => { + const toast = useToastStore() + // Extract meaningful message + const message = error.response?.data?.message || error.message || 'Something went wrong' if (error.response?.status === 401) { router.push('/login') } + toast.show(message, 'danger') const loadingStore = useLoadingStore() loadingStore.stop() return Promise.reject(error) diff --git a/src/services/product.service.ts b/src/services/product.service.ts index 4046bfb..353bddd 100644 --- a/src/services/product.service.ts +++ b/src/services/product.service.ts @@ -2,9 +2,9 @@ import { IPagination } from '@/types/product' import api from './api' export const getProducts = (params?: IPagination) => { - return api.get('/products', { params }) + return api.get('/products', { params: { ...params, loader: true }, }) } export const getProductById = (id: string, params?: IPagination) => { - return api.get(`/products/${id}`, { params }) + return api.get(`/products/${id}`, { params: { ...params, loader: false }, }) } \ No newline at end of file diff --git a/src/stores/toast.store.ts b/src/stores/toast.store.ts new file mode 100644 index 0000000..d5613f4 --- /dev/null +++ b/src/stores/toast.store.ts @@ -0,0 +1,27 @@ +import { defineStore } from 'pinia' + +interface ToastState { + isOpen: boolean + message: string + color: 'success' | 'danger' | 'warning' | 'primary' +} + +export const useToastStore = defineStore('toast', { + state: (): ToastState => ({ + isOpen: false, + message: '', + color: 'danger' + }), + + actions: { + show(message: string, color: ToastState['color'] = 'danger') { + this.message = message + this.color = color + this.isOpen = true + }, + + hide() { + this.isOpen = false + } + } +}) \ No newline at end of file