feat: add basic form customer
This commit is contained in:
parent
0a745885ee
commit
6bb4256dec
8 changed files with 408 additions and 80 deletions
|
|
@ -16,40 +16,10 @@ export const useCustomerForm = defineStore('form-customer', () => {
|
|||
customerNameEN: '',
|
||||
taxNo: '',
|
||||
registeredBranchId: branchStore.currentMyBranch?.id || '',
|
||||
customerBranch: [
|
||||
// {
|
||||
// code: '',
|
||||
// branchNo: 1,
|
||||
// address: '',
|
||||
// addressEN: '',
|
||||
// provinceId: '',
|
||||
// districtId: '',
|
||||
// subDistrictId: '',
|
||||
// zipCode: '',
|
||||
// email: '',
|
||||
// telephoneNo: '',
|
||||
// name: '',
|
||||
// status: 'CREATED',
|
||||
// taxNo: '',
|
||||
// nameEN: '',
|
||||
// legalPersonNo: '',
|
||||
// registerName: '',
|
||||
// registerDate: new Date(),
|
||||
// authorizedCapital: '',
|
||||
// employmentOffice: '',
|
||||
// bussinessType: '',
|
||||
// bussinessTypeEN: '',
|
||||
// jobPosition: '',
|
||||
// jobPositionEN: '',
|
||||
// jobDescription: '',
|
||||
// saleEmployee: '',
|
||||
// payDate: new Date(),
|
||||
// wageRate: 0,
|
||||
// },
|
||||
],
|
||||
customerBranch: [],
|
||||
image: null,
|
||||
};
|
||||
let resetCustomerData = structuredClone(defaultFormData);
|
||||
let resetFormData = structuredClone(defaultFormData);
|
||||
|
||||
const currentFormData = ref<CustomerCreate>(structuredClone(defaultFormData));
|
||||
const state = ref<{
|
||||
|
|
@ -57,31 +27,37 @@ export const useCustomerForm = defineStore('form-customer', () => {
|
|||
dialogOpen: boolean;
|
||||
dialogModal: boolean;
|
||||
branchIndex: number;
|
||||
customerType: 'CORP' | 'PERS';
|
||||
saveMode: 'customer' | 'branch';
|
||||
customerImageUrl: string;
|
||||
imageDialog: boolean;
|
||||
imageEdit: boolean;
|
||||
editCustomerId?: string;
|
||||
editCustomerBranchId?: string;
|
||||
}>({
|
||||
dialogType: 'info',
|
||||
dialogOpen: false,
|
||||
dialogModal: false,
|
||||
imageDialog: false,
|
||||
branchIndex: 0,
|
||||
customerType: 'CORP',
|
||||
imageEdit: false,
|
||||
saveMode: 'customer',
|
||||
customerImageUrl: '',
|
||||
editCustomerId: '',
|
||||
editCustomerBranchId: '',
|
||||
});
|
||||
|
||||
function isFormDataDifferent() {
|
||||
return (
|
||||
JSON.stringify(resetCustomerData) !==
|
||||
JSON.stringify(currentFormData.value)
|
||||
JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value)
|
||||
);
|
||||
}
|
||||
|
||||
function resetFormData(cb?: (...args: any[]) => unknown) {
|
||||
currentFormData.value = structuredClone(resetCustomerData);
|
||||
cb?.();
|
||||
function resetForm() {
|
||||
if (!resetFormData.registeredBranchId) {
|
||||
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
|
||||
}
|
||||
|
||||
currentFormData.value = structuredClone(resetFormData);
|
||||
}
|
||||
|
||||
async function assignFormData(id: string) {
|
||||
|
|
@ -89,15 +65,15 @@ export const useCustomerForm = defineStore('form-customer', () => {
|
|||
|
||||
if (!data) return;
|
||||
|
||||
resetCustomerData.registeredBranchId = data.registeredBranchId;
|
||||
resetCustomerData.status = data.status;
|
||||
resetCustomerData.customerType = data.customerType;
|
||||
resetCustomerData.customerName = data.customerName;
|
||||
resetCustomerData.customerNameEN = data.customerNameEN;
|
||||
resetCustomerData.personName = data.personName;
|
||||
resetCustomerData.taxNo = data.taxNo;
|
||||
resetCustomerData.image = null;
|
||||
resetCustomerData.customerBranch = data.branch.map((v) => ({
|
||||
resetFormData.registeredBranchId = data.registeredBranchId;
|
||||
resetFormData.status = data.status;
|
||||
resetFormData.customerType = data.customerType;
|
||||
resetFormData.customerName = data.customerName;
|
||||
resetFormData.customerNameEN = data.customerNameEN;
|
||||
resetFormData.personName = data.personName;
|
||||
resetFormData.taxNo = data.taxNo;
|
||||
resetFormData.image = null;
|
||||
resetFormData.customerBranch = data.branch.map((v) => ({
|
||||
id: v.id,
|
||||
code: v.code,
|
||||
branchNo: v.branchNo,
|
||||
|
|
@ -128,33 +104,75 @@ export const useCustomerForm = defineStore('form-customer', () => {
|
|||
wageRate: v.wageRate,
|
||||
}));
|
||||
|
||||
currentFormData.value = structuredClone(resetCustomerData);
|
||||
currentFormData.value = structuredClone(resetFormData);
|
||||
}
|
||||
|
||||
function addCurrentCustomerBranch() {
|
||||
currentFormData.value.customerBranch?.push({
|
||||
id: '',
|
||||
code: '',
|
||||
branchNo: 1,
|
||||
address: '',
|
||||
addressEN: '',
|
||||
provinceId: '',
|
||||
districtId: '',
|
||||
subDistrictId: '',
|
||||
zipCode: '',
|
||||
email: '',
|
||||
telephoneNo: '',
|
||||
name: '',
|
||||
status: 'CREATED',
|
||||
taxNo: '',
|
||||
nameEN: '',
|
||||
legalPersonNo: '',
|
||||
registerName: '',
|
||||
registerDate: new Date(),
|
||||
authorizedCapital: '',
|
||||
employmentOffice: '',
|
||||
bussinessType: '',
|
||||
bussinessTypeEN: '',
|
||||
jobPosition: '',
|
||||
jobPositionEN: '',
|
||||
jobDescription: '',
|
||||
saleEmployee: '',
|
||||
payDate: new Date(),
|
||||
wageRate: 0,
|
||||
});
|
||||
}
|
||||
|
||||
async function submitForm() {
|
||||
console.log(state.value.saveMode);
|
||||
if (state.value.dialogType === 'edit' && !state.value.editCustomerId) {
|
||||
throw new Error('Form mode is set to edit but no ID is provided.');
|
||||
}
|
||||
if (state.value.dialogType === 'edit') {
|
||||
return submitEdit();
|
||||
}
|
||||
return submitCreate();
|
||||
if (state.value.saveMode === 'customer') await submitFormCustomer();
|
||||
}
|
||||
|
||||
async function submitCreate() {
|
||||
// customerStore.create(currentFormData.value);
|
||||
}
|
||||
async function submitFormCustomer() {
|
||||
if (state.value.dialogType === 'info') return;
|
||||
|
||||
async function submitEdit() {}
|
||||
if (state.value.dialogType === 'create') {
|
||||
return await customerStore.create(currentFormData.value);
|
||||
}
|
||||
|
||||
if (!state.value.editCustomerId) {
|
||||
throw new Error(
|
||||
'Form mode is set to edit but no ID is provided. Make sure to set customer ID.',
|
||||
);
|
||||
}
|
||||
|
||||
await customerStore.editById(state.value.editCustomerId, {
|
||||
...currentFormData.value,
|
||||
image: currentFormData.value.image || undefined,
|
||||
customerBranch: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
state,
|
||||
resetFormData,
|
||||
currentFormData,
|
||||
isFormDataDifferent,
|
||||
resetFormData,
|
||||
resetForm,
|
||||
assignFormData,
|
||||
submitForm,
|
||||
addCurrentCustomerBranch,
|
||||
};
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue