2022-03-24 14:02:30 +00:00
|
|
|
<script setup>
|
2022-12-13 11:21:12 +00:00
|
|
|
import { onMounted } from 'vue';
|
2022-08-03 11:48:45 +00:00
|
|
|
import axios from 'axios';
|
2022-03-24 14:02:30 +00:00
|
|
|
import { useQuasar } from 'quasar';
|
2022-08-03 11:48:45 +00:00
|
|
|
import { useI18n } from 'vue-i18n';
|
|
|
|
import { useRouter } from 'vue-router';
|
|
|
|
import { useSession } from 'src/composables/useSession';
|
|
|
|
|
2022-03-24 14:02:30 +00:00
|
|
|
const quasar = useQuasar();
|
2022-08-03 11:48:45 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const session = useSession();
|
2022-12-13 11:21:12 +00:00
|
|
|
const { t, availableLocales, locale, fallbackLocale } = useI18n();
|
2022-08-03 11:48:45 +00:00
|
|
|
const { isLoggedIn } = session;
|
2022-03-24 14:02:30 +00:00
|
|
|
|
2022-12-13 11:21:12 +00:00
|
|
|
onMounted(() => {
|
|
|
|
let userLang = window.navigator.language;
|
|
|
|
if (userLang.includes('-')) {
|
|
|
|
userLang = userLang.split('-')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (availableLocales.includes(userLang)) {
|
|
|
|
locale.value = userLang;
|
|
|
|
} else {
|
|
|
|
locale.value = fallbackLocale;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-24 14:02:30 +00:00
|
|
|
quasar.iconMapFn = (iconName) => {
|
|
|
|
if (iconName.startsWith('vn:')) {
|
|
|
|
const name = iconName.substring(3);
|
|
|
|
|
|
|
|
return {
|
2023-02-13 13:08:18 +00:00
|
|
|
cls: `icon-${name} notranslate`,
|
2022-03-24 14:02:30 +00:00
|
|
|
};
|
|
|
|
}
|
2022-11-29 13:45:48 +00:00
|
|
|
|
|
|
|
return {
|
2023-02-13 13:08:18 +00:00
|
|
|
cls: 'material-symbols-outlined notranslate',
|
2022-11-29 13:45:48 +00:00
|
|
|
content: iconName,
|
|
|
|
};
|
2022-03-24 14:02:30 +00:00
|
|
|
};
|
2022-08-03 11:48:45 +00:00
|
|
|
|
|
|
|
function responseError(error) {
|
|
|
|
let message = error.message;
|
|
|
|
let logOut = false;
|
|
|
|
|
2023-03-09 13:59:10 +00:00
|
|
|
const response = error.response;
|
|
|
|
if (response && response.data.error) {
|
|
|
|
message = response.data.error.message;
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:48:45 +00:00
|
|
|
switch (error.response?.status) {
|
|
|
|
case 401:
|
|
|
|
message = 'login.loginError';
|
|
|
|
|
|
|
|
if (isLoggedIn()) {
|
|
|
|
message = 'errors.statusUnauthorized';
|
|
|
|
logOut = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
message = 'errors.statusUnauthorized';
|
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
message = 'errors.statusInternalServerError';
|
|
|
|
break;
|
|
|
|
case 502:
|
|
|
|
message = 'errors.statusBadGateway';
|
|
|
|
break;
|
|
|
|
case 504:
|
|
|
|
message = 'errors.statusGatewayTimeout';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let translatedMessage = t(message);
|
|
|
|
if (!translatedMessage) translatedMessage = message;
|
|
|
|
|
|
|
|
quasar.notify({
|
|
|
|
message: translatedMessage,
|
|
|
|
type: 'negative',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (logOut) {
|
|
|
|
session.destroy();
|
|
|
|
router.push({ path: '/login' });
|
|
|
|
}
|
|
|
|
|
2022-10-31 08:34:01 +00:00
|
|
|
return Promise.reject(error);
|
2022-08-03 11:48:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:58:41 +00:00
|
|
|
axios.interceptors.response.use((response) => {
|
|
|
|
const { method } = response.config;
|
|
|
|
|
2022-10-25 12:23:59 +00:00
|
|
|
const isSaveRequest = method === 'patch';
|
2022-10-18 12:58:41 +00:00
|
|
|
if (isSaveRequest) {
|
|
|
|
quasar.notify({
|
|
|
|
message: t('globals.dataSaved'),
|
|
|
|
type: 'positive',
|
|
|
|
icon: 'check',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}, responseError);
|
2022-03-24 14:02:30 +00:00
|
|
|
</script>
|
|
|
|
|
2022-03-24 12:33:14 +00:00
|
|
|
<template>
|
2022-03-24 13:57:11 +00:00
|
|
|
<router-view />
|
2022-03-24 12:33:14 +00:00
|
|
|
</template>
|
|
|
|
|
2022-03-24 14:02:30 +00:00
|
|
|
<style lang="scss">
|
|
|
|
.body--light {
|
|
|
|
background-color: #eee;
|
|
|
|
}
|
|
|
|
</style>
|