Created
May 19, 2023 06:22
-
-
Save thomasJang/9d07ce30d130095dbaa4e3c3846b9d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { create } from "zustand"; | |
import { subscribeWithSelector } from "zustand/middleware"; | |
import { StoreActions } from "@core/stores/types"; | |
import { | |
BizService, | |
BizSurvey, | |
BizTitle, | |
GetBizSurveyRequest, | |
GetBizTitleViewRequest, | |
PutBizSurveyRequest, | |
} from "../../services"; | |
import { AXFDGDataItem, AXFDGDataItemStatus } from "@axframe/datagrid"; | |
import { fileDownload } from "../../utils"; | |
import { CustomError } from "../../@core/services/CustomError"; | |
interface States { | |
bizTitle?: BizTitle; | |
listRequestValue: GetBizSurveyRequest; | |
bizSurveyListData: AXFDGDataItem<BizSurvey>[]; | |
listColWidths: number[]; | |
bizTitleSpinning: boolean; | |
bizSurveyListSpinning: boolean; | |
listCheckedRowIds: string[]; | |
} | |
interface Actions { | |
init: (id: string) => void; | |
reset: () => void; | |
callBizTitle: (apiParams: GetBizTitleViewRequest) => Promise<void>; | |
callBizSurvey: () => Promise<void>; | |
setListRequestValue: (requestValue: GetBizSurveyRequest) => void; | |
setListColWidths: (colWidths: number[]) => void; | |
setListCheckedRowIds: (checkedRowIds: string[]) => void; | |
callSaveBizSurvey: () => Promise<void>; | |
setBizSurveyListData: (index: number, value: BizSurvey) => void; | |
callBizSurveyReportDownLoad: () => Promise<void>; | |
callBizSurveyDuplicate: () => Promise<void>; | |
callBizSurveyDelete: () => Promise<void>; | |
callBizSurveyListDownLoad: () => Promise<void>; | |
} | |
// create states | |
const createState: States = { | |
bizTitle: { | |
bizCd: "", | |
bizNm: "", | |
}, | |
bizSurveyListData: [], | |
listRequestValue: {}, | |
listColWidths: [], | |
bizTitleSpinning: false, | |
bizSurveyListSpinning: false, | |
listCheckedRowIds: [], | |
}; | |
// create actions | |
const createActions: StoreActions<States & Actions, Actions> = (set, get) => ({ | |
async init(id): Promise<void> { | |
set(createState); | |
await get().callBizTitle({ id }); | |
await get().callBizSurvey(); | |
}, | |
reset(): void {}, | |
callBizTitle: async (apiParams) => { | |
set({ bizTitleSpinning: true }); | |
const { rs } = await BizService.getBizTitleView(apiParams); | |
set({ | |
bizTitle: rs, | |
listRequestValue: { | |
bizCd: rs.bizCd, | |
}, | |
}); | |
set({ bizTitleSpinning: false }); | |
}, | |
callBizSurvey: async () => { | |
set({ bizSurveyListSpinning: true, listCheckedRowIds: [] }); | |
try { | |
const apiPrams: GetBizSurveyRequest = { ...get().listRequestValue }; | |
const { ds } = await BizService.getBizSurvey(apiPrams); | |
set({ | |
bizSurveyListData: ds.map((values) => ({ | |
values: { | |
...values, | |
rowId: values.id + "@" + values.bizCd, | |
}, | |
})), | |
}); | |
} catch (e) { | |
set({ bizSurveyListData: [] }); | |
throw e; | |
} finally { | |
set({ bizSurveyListSpinning: false }); | |
} | |
}, | |
setListRequestValue: (requestValues) => { | |
set({ listRequestValue: requestValues }); | |
}, | |
setListColWidths: (colWidths) => set({ listColWidths: colWidths }), | |
setListCheckedRowIds: (checkedRowIds) => set({ listCheckedRowIds: checkedRowIds }), | |
callSaveBizSurvey: async () => { | |
set({ bizSurveyListSpinning: true, listCheckedRowIds: [] }); | |
try { | |
const apiPrams: PutBizSurveyRequest[] = | |
get() | |
.bizSurveyListData?.filter( | |
(item) => | |
item.status === AXFDGDataItemStatus.new || | |
item.status === AXFDGDataItemStatus.edit || | |
item.status === AXFDGDataItemStatus.remove | |
) | |
.map((values) => ({ ...values.values })) ?? []; | |
await BizService.putBizSurvey(apiPrams); | |
await get().callBizSurvey(); | |
} catch (e) { | |
throw e; | |
} finally { | |
set({ bizSurveyListSpinning: false }); | |
} | |
}, | |
setBizSurveyListData: (index, value) => { | |
const list = [...get().bizSurveyListData]; | |
list[index] = { | |
values: { ...value }, | |
}; | |
set({ bizSurveyListData: list }); | |
}, | |
callBizSurveyReportDownLoad: async () => { | |
const { bizCd, areaCd } = get().listRequestValue; | |
if (!bizCd || !areaCd) throw new CustomError("다운로드를 위해서는 권역정보가 필요합니다.\n권역을 선택해주세요."); | |
await fileDownload(`/v1/bizSurvey/download?bizCd=${bizCd}&areaCd=${areaCd}`, `${bizCd}_REPORT.zip`); | |
}, | |
callBizSurveyDuplicate: async () => { | |
const listCheckedRowIds = get().listCheckedRowIds; | |
if (listCheckedRowIds.length === 0) { | |
throw new CustomError("복사할 목록을 선택해주세요."); | |
} | |
if (listCheckedRowIds.length > 1) { | |
throw new CustomError("복사는 한개의 아이템씩 해주세요."); | |
} | |
set({ bizSurveyListSpinning: true }); | |
try { | |
const bizSurveyListDataMap: Record<string, BizSurvey> = get().bizSurveyListData.reduce((newObj, obj) => { | |
newObj[obj.values.rowId ?? ""] = obj.values; | |
return newObj; | |
}, {}); | |
for (let i = 0; i < listCheckedRowIds.length; i++) { | |
const bizSurvey = bizSurveyListDataMap[listCheckedRowIds[i]]; | |
if (bizSurvey) { | |
await BizService.postBizSurveyCopy(bizSurvey); | |
} | |
} | |
} catch (e) { | |
throw e; | |
} finally { | |
set({ bizSurveyListSpinning: false }); | |
} | |
}, | |
callBizSurveyDelete: async () => { | |
const listCheckedRowIds = get().listCheckedRowIds; | |
if (listCheckedRowIds.length === 0) { | |
throw new CustomError("삭제할 목록을 선택해주세요."); | |
} | |
set({ bizSurveyListSpinning: true }); | |
try { | |
const apiParam = get() | |
.bizSurveyListData.filter((item) => listCheckedRowIds.includes(item.values.rowId ?? "")) | |
.map((item) => item.values); | |
await BizService.putBizSurveyRemove(apiParam); | |
} catch (e) { | |
throw e; | |
} finally { | |
set({ bizSurveyListSpinning: false }); | |
} | |
}, | |
callBizSurveyListDownLoad: async () => { | |
const { bizCd, areaCd = "", filter = "" } = get().listRequestValue; | |
await fileDownload(`/v1/bizSurvey/excel?bizCd=${bizCd}&areaCd=${areaCd}&filter=${filter}`, `${bizCd}_LIST.xlsx`); | |
}, | |
}); | |
// ---------------- exports | |
export interface surveyViewStore extends States, Actions {} | |
export const useSurveyViewStore = create( | |
subscribeWithSelector<surveyViewStore>((set, get) => ({ | |
...createState, | |
...createActions(set, get), | |
})) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment