2024-09-04 17:38:52 +07:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import { ref } from "vue";
|
|
|
|
|
import { useQuasar } from "quasar";
|
|
|
|
|
|
|
|
|
|
import http from "@/plugins/http";
|
|
|
|
|
import config from "@/app.config";
|
|
|
|
|
import { useCounterMixin } from "@/stores/mixin";
|
|
|
|
|
|
2024-09-05 10:54:04 +07:00
|
|
|
import type { DataStructureTree } from "@/interface/main";
|
|
|
|
|
|
2024-09-04 17:38:52 +07:00
|
|
|
const $q = useQuasar();
|
|
|
|
|
const { showLoader, hideLoader, messageError } = useCounterMixin();
|
|
|
|
|
|
|
|
|
|
export const useStructureTree = defineStore("structureTree", () => {
|
|
|
|
|
const activeId = ref<string>("");
|
2024-09-05 10:54:04 +07:00
|
|
|
const dataStore = ref<{ [key: string]: DataStructureTree[] }>({});
|
2024-09-04 17:38:52 +07:00
|
|
|
|
2024-09-05 10:54:04 +07:00
|
|
|
/**
|
|
|
|
|
* fetch ข้อมูลโครงสร้างตามคีย์ของระบบ
|
|
|
|
|
* @param sysKey คีย์ของระบบ
|
|
|
|
|
* @param isLoad สถานะที่ใช้ในการแสดงตัวโหลด true = แสดง, false = ไม่แสดง
|
|
|
|
|
* @returns ข้อมูลโครงสร้าง
|
|
|
|
|
*/
|
|
|
|
|
async function fetchStructureTree(sysKey: string, isLoad: boolean = false) {
|
2024-09-04 17:38:52 +07:00
|
|
|
if (dataStore.value[sysKey]) {
|
|
|
|
|
return dataStore.value[sysKey] || [];
|
|
|
|
|
} else {
|
|
|
|
|
activeId.value === "" && (await fetchActive());
|
2024-09-05 10:54:04 +07:00
|
|
|
const data = await fetchData(sysKey, isLoad);
|
2024-09-04 17:38:52 +07:00
|
|
|
return data || [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 10:54:04 +07:00
|
|
|
/**
|
|
|
|
|
* fetch ข้อมูลโครงสร้างองค์กรปัจจุบัน
|
|
|
|
|
*
|
|
|
|
|
* เช็ต activeId.value เป็นค่า data.activeId ที่ได้จากข้อมูลที่ดึงมา
|
|
|
|
|
*/
|
2024-09-04 17:38:52 +07:00
|
|
|
async function fetchActive() {
|
|
|
|
|
try {
|
|
|
|
|
const res = await http.get(config.API.activeOrganization);
|
|
|
|
|
const data = res.data.result;
|
|
|
|
|
activeId.value = data.activeId;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 10:54:04 +07:00
|
|
|
/**
|
|
|
|
|
* fetch ข้อมูลโครงสร้างจาก API
|
|
|
|
|
* @param sysKey คีย์ของระบบ
|
|
|
|
|
* @param isLoad สถานะที่ใช้ในการแสดงตัวโหลด true = แสดง, false = ไม่แสดง
|
|
|
|
|
* @returns ข้อมูลโครงสร้าง
|
|
|
|
|
*
|
|
|
|
|
* เมื่อ isLoad เป็น true จะทำการแสดงตัวโหลด showLoader
|
|
|
|
|
* และเก็บค่าใน dataStore.value ตามคีย์ของระบบที่รับมา
|
|
|
|
|
*/
|
|
|
|
|
async function fetchData(sysKey: string, isLoad: boolean) {
|
|
|
|
|
isLoad && showLoader();
|
2024-09-04 17:38:52 +07:00
|
|
|
try {
|
|
|
|
|
const res = await http.get(
|
|
|
|
|
config.API.orgByIdSystem(activeId.value, sysKey)
|
|
|
|
|
);
|
|
|
|
|
const data = res.data.result;
|
|
|
|
|
dataStore.value[sysKey] = data;
|
|
|
|
|
return data;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
} finally {
|
|
|
|
|
hideLoader();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
fetchStructureTree,
|
|
|
|
|
};
|
|
|
|
|
});
|