refactor: view all stat with no fetch

This commit is contained in:
puriphatt 2024-09-05 09:59:19 +07:00
parent 8ffecc9146
commit 18059db8a0

View file

@ -103,6 +103,7 @@ const {
const { workNameItems, recordTreeProductType } = const { workNameItems, recordTreeProductType } =
storeToRefs(productServiceStore); storeToRefs(productServiceStore);
const allStat = ref<{ mode: string; count: number }[]>([]);
const stat = ref< const stat = ref<
{ {
icon: string; icon: string;
@ -759,7 +760,7 @@ async function deleteServiceConfirm(serviceId?: string) {
totalService.value = totalService.value - 1; totalService.value = totalService.value - 1;
} }
flowStore.rotate(); flowStore.rotate();
calculateStats(); calculateStats({ reFetch: true });
}, },
cancel: () => {}, cancel: () => {},
}); });
@ -791,7 +792,7 @@ async function deleteProductConfirm(id?: string) {
totalProduct.value = totalProduct.value - 1; totalProduct.value = totalProduct.value - 1;
} }
flowStore.rotate(); flowStore.rotate();
calculateStats(); calculateStats({ reFetch: true });
}, },
cancel: () => {}, cancel: () => {},
}); });
@ -819,7 +820,7 @@ async function deleteProductById(productId?: string) {
} }
flowStore.rotate(); flowStore.rotate();
calculateStats(); calculateStats({ reFetch: true });
editByTree.value = undefined; editByTree.value = undefined;
drawerInfo.value = false; drawerInfo.value = false;
} else { } else {
@ -834,7 +835,7 @@ async function deleteProductById(productId?: string) {
} }
flowStore.rotate(); flowStore.rotate();
calculateStats(); calculateStats({ reFetch: true });
drawerInfo.value = false; drawerInfo.value = false;
} }
}, },
@ -1070,6 +1071,7 @@ async function submitService() {
const res = await createService(formDataProductService.value); const res = await createService(formDataProductService.value);
if (res) { if (res) {
allStat.value[1].count = allStat.value[1].count + 1;
stat.value[1].count = stat.value[1].count + 1; stat.value[1].count = stat.value[1].count + 1;
} }
totalService.value = totalService.value + 1; totalService.value = totalService.value + 1;
@ -1110,6 +1112,7 @@ async function submitProduct() {
const res = await createProduct(formDataProduct.value); const res = await createProduct(formDataProduct.value);
if (res) { if (res) {
allStat.value[2].count = allStat.value[2].count + 1;
stat.value[2].count = stat.value[2].count + 1; stat.value[2].count = stat.value[2].count + 1;
} }
@ -1140,6 +1143,7 @@ async function submitGroup() {
const res = await createProductService(formDataGroup.value); const res = await createProductService(formDataGroup.value);
if (res) { if (res) {
allStat.value[0].count = allStat.value[0].count + 1;
stat.value[0].count = stat.value[0].count + 1; stat.value[0].count = stat.value[0].count + 1;
} }
} }
@ -1196,8 +1200,11 @@ function openPropertiesDialog(type: 'service' | 'work') {
propertiesDialog.value = true; propertiesDialog.value = true;
} }
async function calculateStats(type?: 'service' | 'product') { async function calculateStats(opt?: {
if (type === 'service' && productMode.value === 'service') { type?: 'service' | 'product';
reFetch?: boolean;
}) {
if (opt && opt.type === 'service' && productMode.value === 'service') {
const resStatsService = await fetchStatsService({ const resStatsService = await fetchStatsService({
productGroupId: currentIdGrop.value, productGroupId: currentIdGrop.value,
}); });
@ -1205,7 +1212,7 @@ async function calculateStats(type?: 'service' | 'product') {
return; return;
} }
if (type === 'product' && productMode.value === 'product') { if (opt && opt.type === 'product' && productMode.value === 'product') {
const resStatsProduct = await fetchStatsProduct({ const resStatsProduct = await fetchStatsProduct({
productGroupId: currentIdGrop.value, productGroupId: currentIdGrop.value,
}); });
@ -1213,13 +1220,18 @@ async function calculateStats(type?: 'service' | 'product') {
return; return;
} }
const resStatsGroup = await fetchStatsProductGroup(); if (allStat.value.length === 0 || (opt && opt.reFetch)) {
const resStatsService = await fetchStatsService(); const resStatsGroup = await fetchStatsProductGroup();
const resStatsProduct = await fetchStatsProduct(); const resStatsService = await fetchStatsService();
const resStatsProduct = await fetchStatsProduct();
stat.value[0].count = resStatsGroup ?? 0; allStat.value.push({ mode: 'group', count: resStatsGroup ?? 0 });
stat.value[1].count = resStatsService ?? 0; allStat.value.push({ mode: 'service', count: resStatsService ?? 0 });
stat.value[2].count = resStatsProduct ?? 0; allStat.value.push({ mode: 'product', count: resStatsProduct ?? 0 });
}
stat.value[0].count = allStat.value[0].count;
stat.value[1].count = allStat.value[1].count;
stat.value[2].count = allStat.value[2].count;
} }
async function fetchStatus() { async function fetchStatus() {
@ -1465,10 +1477,10 @@ watch([currentStatusList, productMode], async () => {
await calculateStats(); await calculateStats();
} }
if (productMode.value === 'service') { if (productMode.value === 'service') {
await calculateStats('service'); await calculateStats({ type: 'service' });
} }
if (productMode.value === 'product') { if (productMode.value === 'product') {
await calculateStats('product'); await calculateStats({ type: 'product' });
} }
}); });
</script> </script>