fix useStructStore

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2025-07-25 10:48:00 +07:00
parent 07b33e6bf5
commit 3b6bced57a
2 changed files with 45 additions and 13 deletions

View file

@ -150,20 +150,21 @@ async function scrollToCenter() {
}
async function fetchAgencyData(id: string) {
if (!id) return; // id return
agencyId.value = "";
await http
.get(config.API.orgByid(id))
.then((res) => {
const data = res.data.result.data;
agencyData.value = data.map((item: any) => ({
id: item.orgTreeId,
name: item.orgName,
}));
agencyOp.value = agencyData.value;
})
.catch((err) => {
messageError($q, err);
});
try {
const data = await storeOrg.fetchDataTree(id);
console.log(data);
agencyData.value = data.data.map((item: any) => ({
id: item.orgTreeId,
name: item.orgName,
}));
agencyOp.value = agencyData.value;
} catch (err) {
messageError($q, err);
}
}
function onSelectAgency(val: string) {

View file

@ -1,9 +1,40 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import http from "@/plugins/http";
import config from "@/app.config";
export const useStructStore = defineStore("StructStore", () => {
const dataSource = ref();
const cachedData = ref<Record<string, any>>({});
/**
*
* @param keyId id
*/
async function fetchDataTree(keyId: string) {
// ตรวจสอบว่ามีข้อมูลใน cache แล้วหรือไม่
if (cachedData.value[keyId]) {
return cachedData.value[keyId];
}
// ถ้าไม่มีข้อมูลใน cache หรือ forceRefresh เป็น true ให้ดึงข้อมูลจาก API
try {
const res = await http.get(config.API.orgByid(keyId));
const result = res.data.result;
// เก็บข้อมูลใน cache
cachedData.value[keyId] = result;
return result;
} catch (error) {
console.error("Error fetching data tree:", error);
return [];
}
}
return {
dataSource,
fetchDataTree,
};
});