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

@ -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;