jws-frontend/src/pages/05_quotation/form.ts

112 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-10-01 15:02:18 +07:00
import { defineStore } from 'pinia';
2024-10-02 14:05:36 +07:00
import { ref } from 'vue';
2024-10-03 09:47:43 +07:00
// NOTE: Import types
2024-10-02 14:05:36 +07:00
import { QuotationPayload, EmployeeWorker } from 'src/stores/quotations/types';
2024-10-01 15:02:18 +07:00
2024-10-03 09:47:43 +07:00
// NOTE: Import stores
2024-10-03 09:41:06 +07:00
import { useQuotationStore } from 'stores/quotations';
2024-10-03 14:43:41 +07:00
const DEFAULT_DATA: QuotationPayload & { id?: string } = {
2024-10-03 09:56:50 +07:00
productServiceList: [],
urgent: false,
customerBranchId: '',
worker: [],
workerCount: 0,
payBillDate: new Date(),
paySplit: [],
paySplitCount: 0,
payCondition: 'Full',
dueDate: new Date(),
documentReceivePoint: '',
contactTel: '',
contactName: '',
workName: '',
actorName: '',
status: 'CREATED',
};
2024-10-01 15:02:18 +07:00
export const useQuotationForm = defineStore('form-quotation', () => {
const quotationStore = useQuotationStore();
2024-10-03 09:56:50 +07:00
let resetFormData = structuredClone(DEFAULT_DATA);
2024-10-02 14:05:36 +07:00
const currentFormData = ref<QuotationPayload>(structuredClone(resetFormData));
2024-10-03 09:51:58 +07:00
const currentFormState = ref<{
mode: null | 'info' | 'create' | 'edit';
}>({
mode: null,
});
2024-10-02 14:05:36 +07:00
function isFormDataDifferent() {
const { ...resetData } = resetFormData;
const { ...currData } = currentFormData.value;
return JSON.stringify(resetData) !== JSON.stringify(currData);
}
2024-10-03 09:41:06 +07:00
function resetForm(clean = false) {
if (clean) {
2024-10-03 09:56:50 +07:00
currentFormData.value = structuredClone(DEFAULT_DATA);
resetFormData = structuredClone(DEFAULT_DATA);
2024-10-03 09:41:06 +07:00
return;
}
currentFormData.value = structuredClone(resetFormData);
}
2024-10-03 14:43:41 +07:00
async function assignFormData(id: string, mode: 'info' | 'edit' = 'info') {
2024-10-03 09:47:43 +07:00
const data = await quotationStore.getQuotation(id);
if (!data) return; // NOTE: Error should be handled globally by axios instance
2024-10-03 09:51:58 +07:00
2024-10-03 14:43:41 +07:00
resetFormData = Object.assign(data, {
dueDate: new Date(data.dueDate),
payBillDate: data.payBillDate ? new Date(data.payBillDate) : undefined,
worker: data.worker.map((v) =>
Object.assign(v.employee, {
dateOfBirth: new Date(v.employee.dateOfBirth),
}),
),
});
currentFormData.value = structuredClone(resetFormData);
currentFormState.value.mode = mode;
2024-10-03 09:47:43 +07:00
}
2024-10-03 09:41:06 +07:00
2024-10-03 09:51:58 +07:00
function submiQuotationt() {
currentFormState.value.mode = 'info';
}
2024-10-03 09:41:06 +07:00
2024-10-03 09:28:47 +07:00
function injectNewEmployee(data: EmployeeWorker) {
2024-10-02 14:05:36 +07:00
currentFormData.value.worker.push({
alienReferencNumber: data.alienReferencNumber,
documentExpireDate: data.documentExpireDate,
lastNameEN: data.lastNameEN,
lastName: data.lastName,
middleNameEN: data.middleNameEN,
middleName: data.middleName,
firstNameEN: data.firstNameEN,
firstName: data.firstName,
namePrefix: data.namePrefix,
nationality: data.nationality,
gender: data.gender,
dateOfBirth: data.dateOfBirth,
});
}
return {
currentFormData,
2024-10-03 14:43:41 +07:00
currentFormState,
2024-10-02 14:05:36 +07:00
2024-10-03 09:28:47 +07:00
injectNewEmployee,
2024-10-03 09:41:06 +07:00
isFormDataDifferent,
resetForm,
assignFormData,
submiQuotationt,
2024-10-02 14:05:36 +07:00
};
2024-10-01 15:02:18 +07:00
});