refactor: map tree

This commit is contained in:
Net 2024-08-27 16:19:18 +07:00 committed by Methapon Metanipat
parent 0a7ffb646f
commit 7a700b050a

View file

@ -2,6 +2,7 @@ import { ref, toRaw, watch } from 'vue';
import { defineStore } from 'pinia';
import { CustomerBranchCreate, CustomerCreate } from 'stores/customer/types';
import { Employee, EmployeeCreate } from 'stores/employee/types';
import { useI18n } from 'vue-i18n';
import useMyBranch from 'stores/my-branch';
import useCustomerStore from 'stores/customer';
@ -9,6 +10,7 @@ import useEmployeeStore from 'stores/employee';
import useFlowStore from 'stores/flow';
export const useCustomerForm = defineStore('form-customer', () => {
const { t } = useI18n();
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;
const customerStore = useCustomerStore();
@ -46,6 +48,7 @@ export const useCustomerForm = defineStore('form-customer', () => {
editCustomerId?: string;
editCustomerCode?: string;
editCustomerBranchId?: string;
treeFile: { lable: string; file: { lable: string }[] }[];
}>({
dialogType: 'info',
dialogOpen: false,
@ -59,6 +62,7 @@ export const useCustomerForm = defineStore('form-customer', () => {
editCustomerId: '',
editCustomerBranchId: '',
defaultCustomerImageUrl: '',
treeFile: [],
});
watch(
@ -82,6 +86,8 @@ export const useCustomerForm = defineStore('form-customer', () => {
resetFormData = structuredClone(defaultFormData);
resetFormData.registeredBranchId = branchStore.currentMyBranch?.id || '';
state.value.editCustomerId = '';
state.value.treeFile = [];
return;
}
@ -163,6 +169,25 @@ export const useCustomerForm = defineStore('form-customer', () => {
contactName: v.contactName || '',
file: await customerStore.listAttachment(v.id).then(async (r) => {
if (r) {
r.forEach((item) => {
const temp = item.split('-').at(0);
if (
!state.value.treeFile.some(
(x) => x.lable === t(`customer.typeFile.${temp || ''}`),
)
) {
state.value.treeFile.push({
lable: t(`customer.typeFile.${temp || ''}`),
file: r
.filter((x) => x.includes(temp || ''))
.map((x) => ({
lable: x,
})),
});
}
});
return await Promise.all(
r.map(async (item) => ({
url: await customerStore.getAttachment(v.id, item),