diff --git a/src/modules/02_organization/components/StructureOrgMain.vue b/src/modules/02_organization/components/StructureOrgMain.vue index 4a6ae377f..ea8cdecf0 100644 --- a/src/modules/02_organization/components/StructureOrgMain.vue +++ b/src/modules/02_organization/components/StructureOrgMain.vue @@ -154,8 +154,7 @@ async function fetchAgencyData(id: string) { agencyId.value = ""; try { - const data = await storeOrg.fetchDataTree(id); - console.log(data); + const data = await store.fetchDataTree(id); agencyData.value = data.data.map((item: any) => ({ id: item.orgTreeId, diff --git a/src/modules/02_organization/store/chart.ts b/src/modules/02_organization/store/chart.ts index 23dff2503..678225335 100644 --- a/src/modules/02_organization/store/chart.ts +++ b/src/modules/02_organization/store/chart.ts @@ -1,40 +1,10 @@ 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>({}); - - /** - * ฟังก์ชันดึงข้อมูลโครงสร้าง - * @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, }; }); diff --git a/src/modules/02_organization/store/organizational.ts b/src/modules/02_organization/store/organizational.ts index faecc64a9..07022d5ac 100644 --- a/src/modules/02_organization/store/organizational.ts +++ b/src/modules/02_organization/store/organizational.ts @@ -1,6 +1,9 @@ import { defineStore } from "pinia"; import { reactive, ref } from "vue"; +import http from "@/plugins/http"; +import config from "@/app.config"; + /** importType*/ import type { DataActive, @@ -96,7 +99,7 @@ export const useOrganizational = defineStore("organizationalStore", () => { posMasterNo: e.orgShortname + (e.posMasterNoPrefix ? e.posMasterNoPrefix : "") + - (e.posMasterNo ? ` ${e.posMasterNo }`: "") + + (e.posMasterNo ? ` ${e.posMasterNo}` : "") + (e.posMasterNoSuffix ? e.posMasterNoSuffix : ""), positionName: e.isSit ? e.profilePosition : e.positionName, posTypeName: e.isSit ? e.profilePostype : e.posTypeName, @@ -166,6 +169,33 @@ export const useOrganizational = defineStore("organizationalStore", () => { } } + const cachedData = ref>({}); + + /** + * ฟังก์ชันดึงข้อมูลโครงสร้าง + * @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 { typeOrganizational, statusView, @@ -190,5 +220,6 @@ export const useOrganizational = defineStore("organizationalStore", () => { isLosck, remark, convertStatus, + fetchDataTree, }; });