refactor: use session storage instead

This commit is contained in:
Methapon Metanipat 2024-10-21 11:13:34 +07:00
parent d165d3bb81
commit 425fd71d2c
2 changed files with 66 additions and 49 deletions

View file

@ -187,21 +187,18 @@ function triggerQuotationDialog(opts: {
branchId?: string; branchId?: string;
}) { }) {
const url = new URL('/quotation/add-quotation', window.location.origin); const url = new URL('/quotation/add-quotation', window.location.origin);
url.searchParams.set(
'branchId',
opts.branchId !== undefined ? opts.branchId : branchId.value,
);
url.searchParams.set(
'customerBranchId',
quotationFormData.value.customerBranchId,
);
url.searchParams.set('agentPrice', agentPrice.value.toString());
url.searchParams.set('special', special.value.toString());
url.searchParams.set('statusDialog', opts.statusDialog);
if (opts.quotationId !== undefined) { localStorage.setItem(
url.searchParams.set('quotationId', opts.quotationId); 'new-quotation',
} JSON.stringify({
customerBranchId: quotationFormData.value.customerBranchId,
branchId: opts.branchId ?? branchId.value,
agentPrice: agentPrice.value,
special: special.value,
statusDialog: opts.statusDialog,
quotationId: opts.quotationId,
}),
);
window.open(url.toString(), '_blank'); window.open(url.toString(), '_blank');
} }

View file

@ -511,6 +511,8 @@ function changeMode(mode: string) {
} }
} }
const sessionData = ref<Record<string, any>>();
onMounted(async () => { onMounted(async () => {
await configStore.getConfig(); await configStore.getConfig();
// get language // get language
@ -536,34 +538,27 @@ onMounted(async () => {
changeMode('light'); changeMode('light');
} }
// get query string sessionStorage.setItem(
const urlParams = new URLSearchParams(window.location.search); 'new-quotation',
localStorage.getItem('new-quotation') ||
sessionStorage.getItem('new-quotation') ||
'',
);
const price = urlParams.get('agentPrice'); localStorage.removeItem('new-quotation');
agentPrice.value = price === 'true' ? true : false; const payload = sessionStorage.getItem('new-quotation');
if (!!payload) {
const parsed = JSON.parse(payload);
date.value = Date.now(); date.value = Date.now();
quotationFormState.value.mode = parsed.statusDialog;
currentQuotationId.value = urlParams.get('quotationId') || undefined; quotationFormData.value.registeredBranchId = parsed.branchId;
quotationFormData.value.customerBranchId = parsed.customerBranchId;
quotationFormData.value.registeredBranchId = urlParams.get('branchId') || ''; currentQuotationId.value = parsed.quotationId;
agentPrice.value = parsed.agentPrice;
quotationFormData.value.customerBranchId =
urlParams.get('customerBranchId') || '';
quotationFormState.value.mode = urlParams.get('statusDialog') as
| 'info'
| 'edit'
| 'create';
// 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;
if ( if (
currentQuotationId.value !== undefined && currentQuotationId.value !== undefined &&
quotationFormState.value.mode &&
quotationFormState.value.mode !== 'create' quotationFormState.value.mode !== 'create'
) { ) {
await quotationForm.assignFormData( await quotationForm.assignFormData(
@ -572,6 +567,35 @@ onMounted(async () => {
); );
await assignWorkerToSelectedWorker(); await assignWorkerToSelectedWorker();
} }
sessionData.value = parsed;
}
// get query string
// const urlParams = new URLSearchParams(window.location.search);
//
// const price = urlParams.get('agentPrice');
// agentPrice.value = price === 'true' ? true : false;
//
// date.value = Date.now();
//
// currentQuotationId.value = urlParams.get('quotationId') || undefined;
//
// quotationFormData.value.registeredBranchId = urlParams.get('branchId') || '';
//
// quotationFormData.value.customerBranchId =
// urlParams.get('customerBranchId') || '';
//
// quotationFormState.value.mode = urlParams.get('statusDialog') as
// | 'info'
// | 'edit'
// | 'create';
// 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 assignToProductServiceList(); await assignToProductServiceList();
pageState.isLoaded = true; pageState.isLoaded = true;
@ -580,17 +604,13 @@ onMounted(async () => {
watch( watch(
() => quotationFormData.value.customerBranchId, () => quotationFormData.value.customerBranchId,
async (v) => { async (v) => {
const url = new URL(window.location.href);
url.searchParams.set('customerBranchId', v);
history.pushState({}, '', url.toString());
if (!v) return; if (!v) return;
const retEmp = await customerStore.fetchBranchEmployee(v, { const retEmp = await customerStore.fetchBranchEmployee(v, {
passport: true, passport: true,
}); });
if (retEmp) {
workerList.value = retEmp.data.result; if (retEmp) workerList.value = retEmp.data.result;
}
}, },
); );