hrms-mgt/src/stores/structureTree.ts

127 lines
3.8 KiB
TypeScript
Raw Normal View History

import { defineStore } from "pinia";
import { ref } from "vue";
import http from "@/plugins/http";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
import type {
DataStructureTree,
DataStrategy,
DataActing,
} from "@/interface/main";
2025-06-05 15:11:23 +07:00
const { showLoader, hideLoader } = useCounterMixin();
export const useStructureTree = defineStore("structureTree", () => {
const activeId = ref<string>("");
const dataStore = ref<{ [key: string]: DataStructureTree[] }>({});
const dataStrategy = ref<DataStrategy[]>([]);
const dataActing = ref<DataActing[]>([]);
/**
* fetch
* @param sysKey
* @param isLoad true = , false =
* @returns
*/
async function fetchStructureTree(sysKey: string, isLoad: boolean = false) {
if (dataStore.value[sysKey]) {
return dataStore.value[sysKey] || [];
} else {
activeId.value === "" && (await fetchActive());
const data = await fetchData(sysKey, isLoad);
return data || [];
}
}
/**
* fetch
*
* activeId.value data.activeId
*/
async function fetchActive() {
try {
const res = await http.get(config.API.activeOrganization);
const data = res.data.result;
activeId.value = data.activeId;
} catch (err) {
2025-06-05 15:11:23 +07:00
console.log(err);
}
}
/**
* fetch API
* @param sysKey
* @param isLoad true = , false =
* @returns
*
* isLoad true showLoader
* dataStore.value
*/
async function fetchData(sysKey: string, isLoad: boolean) {
isLoad && showLoader();
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) {
2025-06-05 15:11:23 +07:00
console.log(err);
return [];
} finally {
2024-09-26 12:04:46 +07:00
isLoad && hideLoader();
}
}
/** function fetchTree ยุทธศาสตร์ / แผน*/
async function fetchTreeStrategy() {
if (dataStrategy.value.length > 0) {
return dataStrategy.value;
} else {
try {
showLoader();
const res = await http.get(config.API.devStrategy + `/edit/indicator`);
const data = res.data.result;
dataStrategy.value = data;
return data;
} catch (err) {
2025-06-05 15:11:23 +07:00
console.log(err);
} finally {
hideLoader();
}
}
}
/**
* function fetchTree
*/
async function fetchTreeActing(id: string) {
if (dataActing.value.length > 0) {
return dataActing.value;
} else {
try {
showLoader();
const res = await http.get(config.API.orgAct + `/${id}`);
const data = res.data.result;
dataActing.value = data;
return data;
} catch (err) {
2025-06-05 15:11:23 +07:00
console.log(err);
} finally {
hideLoader();
}
}
}
return {
activeId,
fetchStructureTree,
fetchTreeStrategy,
fetchTreeActing,
dataActing,
};
});