refactor: edit search

This commit is contained in:
Net 2024-09-11 15:41:35 +07:00
parent fbe00192c4
commit c04a473f9e
2 changed files with 59 additions and 54 deletions

View file

@ -177,22 +177,28 @@ const { locale } = useI18n();
const { data: branchData } = storeToRefs(branchStore); const { data: branchData } = storeToRefs(branchStore);
const currentSubBranch = ref<Branch[] | undefined>(undefined);
const treeData = computed(() => { const treeData = computed(() => {
const map: Record<string, BranchWithChildren> = {}; const map: Record<string, BranchWithChildren> = {};
const children: Branch[] = [];
branchData.value?.result.forEach((v) => { branchData.value?.result.forEach((v) => {
const name = locale.value === 'eng' ? v.nameEN : v.name; const name = locale.value === 'eng' ? v.nameEN : v.name;
if (v.isHeadOffice) map[v.id] = { ...v, name, branch: [] };
else children.push({ ...v, name }); map[v.id] = { ...v, name, branch: v.branch || [] };
}); });
children.forEach((v) => { // branchData.value?.result.forEach((v) => {
const name = locale.value === 'eng' ? v.nameEN : v.name; // const name = locale.value === 'eng' ? v.nameEN : v.name;
if (v.headOfficeId && map[v.headOfficeId]) { // if (v.isHeadOffice) map[v.id] = { ...v, name, branch: [] };
map[v.headOfficeId].branch.push({ ...v, name }); // else children.push({ ...v, name });
} // });
});
// children.forEach((v) => {
// const name = locale.value === 'eng' ? v.nameEN : v.name;
// if (v.headOfficeId && map[v.headOfficeId]) {
// map[v.headOfficeId].branch.push({ ...v, name });
// }
// });
return Object.values(map); return Object.values(map);
}); });
@ -254,7 +260,7 @@ onMounted(async () => {
// object, we are effectively unselecting any HQ branch. // object, we are effectively unselecting any HQ branch.
]; ];
await branchStore.fetchList({ pageSize: 99999 }); await fetchList({ pageSize: 99999, tree: true });
await calculateStats(); await calculateStats();
modeView.value = $q.screen.lt.md ? true : false; modeView.value = $q.screen.lt.md ? true : false;
@ -361,6 +367,19 @@ function openDialog() {
modal.value = true; modal.value = true;
} }
async function selectedSubBranche(id: string) {
const res = await branchStore.fetchById(id, {
includeSubBranch: true,
includeContact: true,
});
if (res) {
currentSubBranch.value = res.branch;
} else {
currentSubBranch.value = undefined;
}
}
async function fetchBranchById(id: string) { async function fetchBranchById(id: string) {
const res = await branchStore.fetchById(id, { includeContact: true }); const res = await branchStore.fetchById(id, { includeContact: true });
if (res) { if (res) {
@ -556,7 +575,7 @@ function triggerDelete(id: string) {
message: t('dialog.message.confirmDelete'), message: t('dialog.message.confirmDelete'),
action: async () => { action: async () => {
await branchStore.deleteById(id); await branchStore.deleteById(id);
await branchStore.fetchList({ pageSize: 99999 }); await fetchList({ pageSize: 99999 });
modalDrawer.value = false; modalDrawer.value = false;
await calculateStats(); await calculateStats();
@ -612,6 +631,22 @@ function triggerEditQrCodeBank(opts?: { save?: boolean }) {
} }
} }
async function fetchList(opts: {
page?: number;
pageSize?: number;
zipCode?: string;
query?: string;
tree?: boolean;
filter?: 'head' | 'sub';
}) {
await branchStore.fetchList(opts);
}
watch(inputSearch, () => {
fetchList({ tree: true, query: inputSearch.value });
currentSubBranch.value = undefined;
});
async function triggerChangeStatus( async function triggerChangeStatus(
id: string, id: string,
status: string, status: string,
@ -659,7 +694,7 @@ async function onSubmit() {
}, },
); );
await branchStore.fetchList({ pageSize: 99999 }); await fetchList({ pageSize: 99999 });
if (!imageDialog.value) modalDrawer.value = false; if (!imageDialog.value) modalDrawer.value = false;
modal.value = false; modal.value = false;
} }
@ -675,7 +710,7 @@ async function onSubmit() {
formBankBook.value, formBankBook.value,
); );
await branchStore.fetchList({ pageSize: 99999 }); await fetchList({ pageSize: 99999 });
modal.value = false; modal.value = false;
}; };
@ -818,6 +853,8 @@ watch(currentHq, () => {
}); });
} }
selectedSubBranche(currentHq.value.id);
utilsStore.currentTitle.path = tmp; utilsStore.currentTitle.path = tmp;
}); });
</script> </script>
@ -1130,43 +1167,7 @@ watch(currentHq, () => {
bordered bordered
class="full-width" class="full-width"
:rows-per-page-options="[0]" :rows-per-page-options="[0]"
:rows=" :rows="currentSubBranch || treeData"
treeData
.flatMap((v) => [v, ...v.branch])
.filter((v) => {
if (
statusFilter === 'statusACTIVE' &&
v.status === 'INACTIVE'
) {
return false;
}
if (
statusFilter === 'statusINACTIVE' &&
v.status !== 'INACTIVE'
) {
return false;
}
const terms = `${v.code} ${$i18n.locale === 'eng' ? v.nameEN : v.name} ${v.telephoneNo}`;
if (inputSearch && !terms.includes(inputSearch)) {
return false;
}
if (
!!currentHq.id &&
currentHq.id === v.headOfficeId
) {
return true;
}
if (fieldSelectedBranch.value === 'all') return true;
if (fieldSelectedBranch.value === 'branchHQLabel')
return v.isHeadOffice;
if (fieldSelectedBranch.value === 'branchLabel')
return !v.isHeadOffice;
return false;
})
"
:columns="columns" :columns="columns"
:grid="modeView" :grid="modeView"
card-container-class="row q-col-gutter-md" card-container-class="row q-col-gutter-md"
@ -1197,7 +1198,7 @@ watch(currentHq, () => {
<q-tr <q-tr
:class="{ :class="{
'app-text-muted': props.row.status === 'INACTIVE', 'app-text-muted': props.row.status === 'INACTIVE',
'cursor-pointer': props.row._count.branch !== 0, 'cursor-pointer': props.row._count?.branch !== 0,
}" }"
:style=" :style="
props.rowIndex % 2 !== 0 props.rowIndex % 2 !== 0
@ -1317,7 +1318,11 @@ watch(currentHq, () => {
</q-td> </q-td>
<q-td v-if="fieldSelected.includes('branchLabelTel')"> <q-td v-if="fieldSelected.includes('branchLabelTel')">
{{ props.row.contact[0]?.telephoneNo || '-' }} {{
props.row.contact !== undefined
? props.row.contact[0]?.telephoneNo
: '-'
}}
</q-td> </q-td>
<q-td v-if="fieldSelected.includes('contactName')"> <q-td v-if="fieldSelected.includes('contactName')">
@ -1806,7 +1811,7 @@ watch(currentHq, () => {
formType = 'view'; formType = 'view';
} }
await branchStore.fetchList({ pageSize: 99999 }); await fetchList({ pageSize: 99999 });
} }
" "
:readonly="formType === 'view'" :readonly="formType === 'view'"

View file

@ -12,7 +12,7 @@ type BranchId = string;
const useBranchStore = defineStore('api-branch', () => { const useBranchStore = defineStore('api-branch', () => {
const flowStore = useFlowStore(); const flowStore = useFlowStore();
const data = ref<Pagination<Branch[]>>({ const data = ref<Pagination<(Branch & { branch?: Branch[] })[]>>({
result: [], result: [],
page: 0, page: 0,
pageSize: 0, pageSize: 0,