import { fetchType, getFetchWithCount } from "@/components/lib/api"; import { getUserInfo } from "@common/components/api"; import { getCookie } from "@common/components/lib"; import toast from "react-hot-toast"; export type authState = { auth: { mngAuthrtYn: string; inqAuthrtYn: string; regAuthrtYn: string; mdfcnAuthrtYn: string; delAuthrtYn: string; laeSeCd: string; myUUID?: string; }; }; export type authType = authState & authActions; export const authStateExport: authState = { auth: { mngAuthrtYn: "Y", inqAuthrtYn: "Y", regAuthrtYn: "Y", mdfcnAuthrtYn: "Y", delAuthrtYn: "Y", laeSeCd: "", myUUID:'' }, }; export type authActions = { getMenuAuthrts: (menuCd: number) => void; getLaeSeCdCheck: () => Promise; getMyUUID: () => Promise; }; export const authActionsExport: (set: any, get: any) => authActions = ( set: any, get: any ) => { return { getMenuAuthrts: async (menuCd: number) => { try { const response = await getFetchWithCount( fetchType.CS, `/cms-service/api/v1/menu-authrts/pageinfo/${menuCd}` ); set((state: authState) => { return { ...state, auth: { ...state.auth, mngAuthrtYn: response[0].mngAuthrtYn, inqAuthrtYn: response[0].mngAuthrtYn, regAuthrtYn: response[0].mngAuthrtYn, mdfcnAuthrtYn: response[0].mngAuthrtYn, delAuthrtYn: response[0].mngAuthrtYn, }, }; }); } catch (e) { toast.error( "권한 정보를 가져오는 중 오류가 발생했습니다. 관리자에게 문의하세요." ); console.error("Error fetching menu authorities:", e); } }, getLaeSeCdCheck: async () => { try { const current = get().auth.laeSeCd; if (current && current !== "") { return; } const infoMe = await getUserInfo(); const isLaeSeCd = infoMe.laeSeCd.slice(0, 2) === "G3" ? infoMe.laeSeCd.slice(2) : "0"; set((state: authState) => ({ ...state, auth: { ...state.auth, laeSeCd: isLaeSeCd, }, })); } catch (e) { console.error("laeSeCd check error", e); } }, getMyUUID: async () => { try{ const cached = get().auth.myUUID; if (cached && cached !== "") { return cached; } const tokenId = getCookie("token-id"); set((state: authState) => ({ ...state, auth: { ...state.auth, myUUID: tokenId, }, })); return tokenId; }catch(e){ console.error("getMyUUID error", e); return undefined; } }, }; };