import { fetchType, getFetch, postFectch, putFectch, } from "@/components/lib/api"; import type { pubHasuType } from "../../ruseFcltMng/fcltReg/pbnstReg/type"; import type { DemandCompType } from "@common/business_components/ui"; export type comFcltState = { searchFclts: pubHasuType; demandCompList: DemandCompType[]; dmdIsSucees: string; checkDemand: DemandCompType; }; export type comFcltActions = { getSwtrmtFcltNm: (searchNm: string) => void; getDemandCompList: (searchNm: string, cnt: number) => void; regDemandCompany: (data: DemandCompType) => void; updateDemandCompany: (data: DemandCompType) => void; }; export const searchActions: (set: any) => comFcltActions = (set: any) => { return { getSwtrmtFcltNm: async (searchNm: string) => { const fcltNm = await getFetch( "common", `/prcsFclt/info/1/20/${searchNm}` ); const fcltNmCd: pubHasuType[] = fcltNm.map((item: any) => ({ bscFctCd: item.bscFctCd, sido: item.sido, gugun: item.gugun, bscFctNm: item.bscFctNm, addr: item.addr, fctCpc: item.fctCpc, })); set((state: comFcltState) => ({ ...state, searchFclts: fcltNmCd, })); }, getDemandCompList: async (searchNm: string, cnt: number) => { const coList = await getFetch( fetchType.WR, `/reuseFc/request/${searchNm}/${cnt}` ); set((state: comFcltState) => { state.demandCompList = coList; return state; }); }, regDemandCompany: async (data: DemandCompType) => { try { const response = await postFectch( "/reuseFc/request", data, fetchType.WR ); let message = ""; if (response.status === 200) { message = "success"; } else { message = "fail"; } set((state: comFcltState) => { state.dmdIsSucees = message; return state; }); } catch (err) { console.log(err); } }, updateDemandCompany: async (data: DemandCompType) => { try { const response = await putFectch("/reuseFc/request", data); let message = ""; if (response.status === 200) { message = "success"; } else { message = "fail"; } set((state: comFcltState) => { state.dmdIsSucees = message; return state; }); } catch (err) { console.log(err); } } } };