fix useStructStore
This commit is contained in:
parent
07b33e6bf5
commit
3b6bced57a
2 changed files with 45 additions and 13 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue