refactor: create / edit function

This commit is contained in:
Methapon2001 2024-08-05 15:29:11 +07:00
parent 6bb4256dec
commit 542e48cd1d
2 changed files with 35 additions and 16 deletions

View file

@ -384,6 +384,12 @@ async function editCustomerForm(id: string) {
customerFormState.value.dialogModal = true; customerFormState.value.dialogModal = true;
} }
function createCustomerForm(customerType: 'CORP' | 'PERS') {
customerFormState.value.dialogModal = true;
customerFormState.value.dialogType = 'create';
customerFormData.value.customerType = customerType;
}
// TODO: When in employee form, if select address same as customer then auto fill // TODO: When in employee form, if select address same as customer then auto fill
</script> </script>
@ -394,11 +400,7 @@ async function editCustomerForm(id: string) {
v-if="currentTab === 'employer'" v-if="currentTab === 'employer'"
id="add-customer-legal-entity" id="add-customer-legal-entity"
style="color: white; background-color: hsla(var(--violet-11-hsl))" style="color: white; background-color: hsla(var(--violet-11-hsl))"
@click=" @click="createCustomerForm('CORP')"
(customerFormState.dialogModal = true),
(customerFormState.dialogType = 'create'),
(customerFormData.customerType = 'CORP')
"
padding="xs" padding="xs"
icon="mdi-office-building" icon="mdi-office-building"
:label="$t('add') + ' ' + $t('customerLegalEntity')" :label="$t('add') + ' ' + $t('customerLegalEntity')"
@ -411,11 +413,7 @@ async function editCustomerForm(id: string) {
:label="$t('add') + ' ' + $t('customerNaturalPerson')" :label="$t('add') + ' ' + $t('customerNaturalPerson')"
external-label external-label
label-position="left" label-position="left"
@click=" @click="createCustomerForm('PERS')"
(customerFormState.dialogModal = true),
(customerFormState.dialogType = 'create'),
(customerFormData.customerType = 'CORP')
"
style="color: white; background-color: hsla(var(--teal-10-hsl))" style="color: white; background-color: hsla(var(--teal-10-hsl))"
padding="xs" padding="xs"
icon="mdi-account-plus" icon="mdi-account-plus"
@ -1639,9 +1637,11 @@ async function editCustomerForm(id: string) {
:title="$t('form.title.create', { name: 'Employer' })" :title="$t('form.title.create', { name: 'Employer' })"
:show=" :show="
async () => async () =>
await fetchListOfOptionBranch().then(() => await fetchListOfOptionBranch().then(() => {
customerFormStore.resetForm(), customerFormStore.resetForm(
) customerFormState.dialogType === 'create',
);
})
" "
:submit=" :submit="
() => { () => {
@ -1701,6 +1701,7 @@ async function editCustomerForm(id: string) {
id="customer-form-content" id="customer-form-content"
style="height: 100%; max-height: 100%; overflow-y: auto" style="height: 100%; max-height: 100%; overflow-y: auto"
> >
{{ customerFormData.customerType }}
<FormBasicInfo <FormBasicInfo
id="form-basic-info-customer" id="form-basic-info-customer"
@save="customerFormState.saveMode = 'customer'" @save="customerFormState.saveMode = 'customer'"

View file

@ -1,4 +1,4 @@
import { ref } from 'vue'; import { ref, watch } from 'vue';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { CustomerCreate } from 'src/stores/customer/types'; import { CustomerCreate } from 'src/stores/customer/types';
import useMyBranch from 'src/stores/my-branch'; import useMyBranch from 'src/stores/my-branch';
@ -46,13 +46,26 @@ export const useCustomerForm = defineStore('form-customer', () => {
editCustomerBranchId: '', editCustomerBranchId: '',
}); });
watch(
currentFormData,
(v) => (defaultFormData.customerType = v.customerType),
);
function isFormDataDifferent() { function isFormDataDifferent() {
return ( return (
JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value) JSON.stringify(resetFormData) !== JSON.stringify(currentFormData.value)
); );
} }
function resetForm() { function resetForm(clean = false) {
if (clean) {
defaultFormData.customerType = currentFormData.value.customerType;
currentFormData.value = structuredClone(defaultFormData);
currentFormData.value.registeredBranchId =
branchStore.currentMyBranch?.id || '';
return;
}
if (!resetFormData.registeredBranchId) { if (!resetFormData.registeredBranchId) {
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || ''; resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
} }
@ -60,7 +73,12 @@ export const useCustomerForm = defineStore('form-customer', () => {
currentFormData.value = structuredClone(resetFormData); currentFormData.value = structuredClone(resetFormData);
} }
async function assignFormData(id: string) { async function assignFormData(id?: string) {
if (!id) {
resetFormData = structuredClone(defaultFormData);
return;
}
const data = await customerStore.fetchById(id); const data = await customerStore.fetchById(id);
if (!data) return; if (!data) return;