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;
}
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
</script>
@ -394,11 +400,7 @@ async function editCustomerForm(id: string) {
v-if="currentTab === 'employer'"
id="add-customer-legal-entity"
style="color: white; background-color: hsla(var(--violet-11-hsl))"
@click="
(customerFormState.dialogModal = true),
(customerFormState.dialogType = 'create'),
(customerFormData.customerType = 'CORP')
"
@click="createCustomerForm('CORP')"
padding="xs"
icon="mdi-office-building"
:label="$t('add') + ' ' + $t('customerLegalEntity')"
@ -411,11 +413,7 @@ async function editCustomerForm(id: string) {
:label="$t('add') + ' ' + $t('customerNaturalPerson')"
external-label
label-position="left"
@click="
(customerFormState.dialogModal = true),
(customerFormState.dialogType = 'create'),
(customerFormData.customerType = 'CORP')
"
@click="createCustomerForm('PERS')"
style="color: white; background-color: hsla(var(--teal-10-hsl))"
padding="xs"
icon="mdi-account-plus"
@ -1639,9 +1637,11 @@ async function editCustomerForm(id: string) {
:title="$t('form.title.create', { name: 'Employer' })"
:show="
async () =>
await fetchListOfOptionBranch().then(() =>
customerFormStore.resetForm(),
)
await fetchListOfOptionBranch().then(() => {
customerFormStore.resetForm(
customerFormState.dialogType === 'create',
);
})
"
:submit="
() => {
@ -1701,6 +1701,7 @@ async function editCustomerForm(id: string) {
id="customer-form-content"
style="height: 100%; max-height: 100%; overflow-y: auto"
>
{{ customerFormData.customerType }}
<FormBasicInfo
id="form-basic-info-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 { CustomerCreate } from 'src/stores/customer/types';
import useMyBranch from 'src/stores/my-branch';
@ -46,13 +46,26 @@ export const useCustomerForm = defineStore('form-customer', () => {
editCustomerBranchId: '',
});
watch(
currentFormData,
(v) => (defaultFormData.customerType = v.customerType),
);
function isFormDataDifferent() {
return (
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) {
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
}
@ -60,7 +73,12 @@ export const useCustomerForm = defineStore('form-customer', () => {
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);
if (!data) return;