refactor: global shared state and function (#79)

* refactor: expose i18n instance

* feat: add global app utility function

* refactor: use global utility function

* refactor: avoid undefined when use outside vue

refactor: avoid undefined when use outside vue

* refactor: remove dup code and use util

* refactor: auto fetch option when use store
This commit is contained in:
Methapon Metanipat 2024-11-21 11:55:44 +07:00 committed by GitHub
parent aa79a4ef7d
commit b0136bba4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 136 additions and 216 deletions

View file

@ -11,7 +11,7 @@ import {
import { ProductTree, quotationProductTree } from './utils';
// NOTE: Import stores
import { setLocale, dateFormat, calculateAge } from 'src/utils/datetime';
import { dateFormat, calculateAge } from 'src/utils/datetime';
import { useEmployeeForm } from 'src/pages/03_customer-management/form';
import { useQuotationStore } from 'src/stores/quotations';
import useProductServiceStore from 'stores/product-service';
@ -85,7 +85,7 @@ import BadgeComponent from 'src/components/BadgeComponent.vue';
import PaymentForm from './PaymentForm.vue';
import { api } from 'src/boot/axios';
import { RouterLink, useRoute } from 'vue-router';
import router from 'src/router';
import { initLang, initTheme } from 'src/utils/ui';
type Node = {
[key: string]: any;
@ -737,33 +737,6 @@ function convertEmployeeToTable() {
);
}
function changeMode(mode: string) {
if (mode === 'light') {
localStorage.setItem('currentTheme', 'light');
$q.dark.set(false);
return;
}
if (mode === 'dark') {
localStorage.setItem('currentTheme', 'dark');
$q.dark.set(true);
return;
}
if (mode === 'baseOnDevice') {
localStorage.setItem('currentTheme', 'baseOnDevice');
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
$q.dark.set(true);
} else {
$q.dark.set(false);
}
return;
}
}
async function triggerDelete(name: string) {
await quotationStore.delAttachment({
parentId: quotationFormData.value.id || '',
@ -827,29 +800,10 @@ async function uploadAttachment(file?: File) {
const sessionData = ref<Record<string, any>>();
onMounted(async () => {
await configStore.getConfig();
// get language
const getCurLang = localStorage.getItem('currentLanguage');
if (getCurLang === 'English') {
locale.value = 'eng';
setLocale('en-gb');
}
if (getCurLang === 'ไทย') {
locale.value = 'tha';
setLocale('th');
}
initTheme();
initLang();
// get theme
const getCurTheme = localStorage.getItem('currentTheme');
if (
getCurTheme === 'light' ||
getCurTheme === 'dark' ||
getCurTheme === 'baseOnDevice'
) {
changeMode(getCurTheme);
} else {
changeMode('light');
}
await configStore.getConfig();
sessionStorage.setItem(
'new-quotation',
@ -874,12 +828,6 @@ onMounted(async () => {
sessionData.value = parsed;
}
// fetch option
const resultOption = await fetch('/option/option.json');
const rawOption = await resultOption.json();
if (locale.value === 'eng') optionStore.globalOption = rawOption.eng;
if (locale.value === 'tha') optionStore.globalOption = rawOption.tha;
await fetchStatus();
if (quotationFormState.value.mode === 'edit') {