From 8fdc9169558d81597389f667ac85c9ca500d4a6b Mon Sep 17 00:00:00 2001 From: puriphatt Date: Mon, 17 Jun 2024 04:00:22 +0000 Subject: [PATCH] fix: alert i18n dialog --- src/boot/axios.ts | 16 +++++++++++----- src/components/GlobalDialog.vue | 2 +- src/i18n/en-US/alert-dialog.ts | 2 ++ src/i18n/th-th/alert-dialog.ts | 2 ++ src/stores/utils/index.ts | 1 + 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/boot/axios.ts b/src/boot/axios.ts index 0fe978c3..b6a869ff 100644 --- a/src/boot/axios.ts +++ b/src/boot/axios.ts @@ -21,10 +21,10 @@ const api = axios.create({ baseURL: import.meta.env.VITE_API_BASE_URL }); function parseError( status: number, - body?: { status: number; message: string }, + body?: { status: number; message: string; code: string }, ) { if (status === 422) return 'ข้อมูลไม่ถูกต้อง กรุณาตรวจสอบใหม่อีกครั้ง'; - if (body && body.message) return body.message; + if (body && body.code) return body.code; return 'เกิดข้อผิดพลาดทำให้ระบบไม่สามารถทำงานได้ กรุณาลองใหม่ในภายหลัง'; } @@ -43,11 +43,17 @@ api.interceptors.response.use( (err) => { useLoader().hide(); dialog({ - color: 'negative', + color: + err.response.status >= 400 && err.response.status < 500 + ? 'warning' + : 'negative', icon: 'mdi-alert', - title: 'เกิดข้อผิดพลาด', - actionText: 'ตกลง', + title: + err.response.status >= 400 && err.response.status < 500 + ? 'warning' + : 'errorOccurred', persistent: true, + enablei18n: true, message: parseError(err.response.status, err.response.data), action: () => {}, }); diff --git a/src/components/GlobalDialog.vue b/src/components/GlobalDialog.vue index d235f3ab..db1a58ad 100644 --- a/src/components/GlobalDialog.vue +++ b/src/components/GlobalDialog.vue @@ -81,7 +81,7 @@ defineProps<{ :class="{ 'full-width': !cancel }" v-if="action" @click="action" - :label="actionText || $t('defaultDialog')" + :label="actionText || $t('agree')" :color="color || 'primary'" v-close-popup /> diff --git a/src/i18n/en-US/alert-dialog.ts b/src/i18n/en-US/alert-dialog.ts index 2b897e18..782e481f 100644 --- a/src/i18n/en-US/alert-dialog.ts +++ b/src/i18n/en-US/alert-dialog.ts @@ -2,6 +2,8 @@ export default { warning: 'Warning', warningForgetInput: 'You have incomplete information.', + errorOccurred: 'An error occurred.', + // Backend productGroupNotFound: 'Product group cannot be found.', productGroupInUsed: 'Product group is in used.', diff --git a/src/i18n/th-th/alert-dialog.ts b/src/i18n/th-th/alert-dialog.ts index 4c2affd4..c43cf6f3 100644 --- a/src/i18n/th-th/alert-dialog.ts +++ b/src/i18n/th-th/alert-dialog.ts @@ -2,6 +2,8 @@ export default { warning: 'เตือน', warningForgetInput: 'คุณกรอกข้อมูลไม่ครบ', + errorOccurred: 'เกิดข้อผิดพลาด.', + // Backend productGroupNotFound: 'ไม่พบกลุ่มสินค้า', productGroupInUsed: 'กลุ่มสินค้าที่ใช้งานอยู่', diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index 181391a3..e379134d 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -10,6 +10,7 @@ export function dialog(opts: { persistent?: boolean; actionText?: string; cancelText?: string; + enablei18n?: boolean; action?: (...args: unknown[]) => unknown; cancel?: (...args: unknown[]) => unknown; }) {