110 lines
4.3 KiB
JavaScript
110 lines
4.3 KiB
JavaScript
'use client'
|
|
|
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'
|
|
import useSWRMutation from 'swr/mutation'
|
|
import { useAxios } from '../useAxios'
|
|
import { unescapeString } from '@/util/common-utils'
|
|
import { moduleSelectionDataState, selectedModuleState } from '@/store/selectedModuleOptions'
|
|
import { compasDegAtom } from '@/store/orientationAtom'
|
|
import { canvasState, currentCanvasPlanState } from '@/store/canvasAtom'
|
|
import { POLYGON_TYPE } from '@/common/common'
|
|
import { useCircuitTrestle } from '../useCirCuitTrestle'
|
|
import { useContext } from 'react'
|
|
import { addedRoofsState } from '@/store/settingAtom'
|
|
import { roofsState } from '@/store/roofAtom'
|
|
import { GlobalDataContext } from '@/app/GlobalDataProvider'
|
|
|
|
/**
|
|
* 캔버스 팝업 상태 관리
|
|
* @param {*} param
|
|
* @returns
|
|
*/
|
|
export function useCanvasPopupStatusController(param = 1) {
|
|
const popupType = parseInt(param)
|
|
|
|
const setCompasDeg = useSetRecoilState(compasDegAtom)
|
|
const [moduleSelectionDataStore, setModuleSelectionDataStore] = useRecoilState(moduleSelectionDataState)
|
|
const setSelectedModules = useSetRecoilState(selectedModuleState)
|
|
const { get, promiseGet, getFetcher, postFetcher } = useAxios()
|
|
const canvas = useRecoilValue(canvasState)
|
|
const currentCanvasPlan = useRecoilValue(currentCanvasPlanState)
|
|
const [addedRoofs, setAddedRoofs] = useRecoilState(addedRoofsState)
|
|
const [roofs, setRoofs] = useRecoilState(roofsState)
|
|
const { managementState, setManagementState } = useContext(GlobalDataContext)
|
|
/**
|
|
* 팝업 상태 조회
|
|
* @param {number} popupTypeParam
|
|
* @returns
|
|
*/
|
|
const getModuleSelection = async (popupTypeParam) => {
|
|
const result = await promiseGet({
|
|
url: `/api/v1/canvas-popup-status?objectNo=${currentCanvasPlan.objectNo}&planNo=${currentCanvasPlan.planNo}&popupType=${popupTypeParam}`,
|
|
})
|
|
.then((res) => {
|
|
return res
|
|
})
|
|
.catch((err) => {
|
|
console.log('🚀 ~ getModuleSelection ~ err:', err)
|
|
return null
|
|
})
|
|
|
|
return result.data
|
|
}
|
|
|
|
/**
|
|
* 전체 팝업 상태 조회
|
|
* 조회 후 전체 데이터 recoil에 저장
|
|
*/
|
|
const handleModuleSelectionTotal = async () => {
|
|
let resultData = {}
|
|
for (let i = 1; i < 3; i++) {
|
|
const result = await getModuleSelection(i)
|
|
if (!result.objectNo) return
|
|
if (i === 1) {
|
|
if (result.popupStatus && unescapeString(result.popupStatus)) {
|
|
const data = JSON.parse(unescapeString(result.popupStatus))
|
|
|
|
if (data?.compasDeg) setCompasDeg(data.compasDeg)
|
|
if (data?.module) setSelectedModules(data.module)
|
|
// setModuleSelectionDataStore(data)
|
|
resultData = { ...data }
|
|
}
|
|
} else if (i === 2) {
|
|
const data = JSON.parse(unescapeString(result.popupStatus))
|
|
const roofSurfaceList = canvas.getObjects().filter((obj) => obj.name === POLYGON_TYPE.MODULE_SETUP_SURFACE)
|
|
const modules = canvas.getObjects().filter((obj) => obj.name === POLYGON_TYPE.MODULE)
|
|
roofSurfaceList.forEach((surface) => {
|
|
surface.modules = modules.filter((module) => module.surfaceId === surface.id)
|
|
})
|
|
if (data.roofConstructions) {
|
|
setRoofs(data.roofConstructions)
|
|
// setManagementState({ ...managementState, roofs: data.roofConstructions.map((roof) => roof.construction.managementState) })
|
|
// setModuleSelectionDataStore({ ...moduleSelectionDataStore, roofConstructions: data.roofConstruction })
|
|
resultData = { ...resultData, roofConstructions: data.roofConstructions }
|
|
}
|
|
// if (data?.module) setManagementState(data.common.managementState)
|
|
}
|
|
}
|
|
setModuleSelectionDataStore(resultData)
|
|
}
|
|
|
|
/**
|
|
* 팝업 상태 저장
|
|
*/
|
|
const { trigger, isMutating } = useSWRMutation(
|
|
`/api/v1/canvas-popup-status?objectNo=${currentCanvasPlan.objectNo}&planNo=${currentCanvasPlan.planNo}&popupType=${popupType}`,
|
|
(url, { arg }) => {
|
|
const params = {
|
|
objectNo: currentCanvasPlan.objectNo,
|
|
planNo: parseInt(currentCanvasPlan.planNo),
|
|
popupType: popupType.toString(),
|
|
// popupStatus: popupType === 1 ? arg : JSON.stringify(arg).replace(/"/g, '\"'),
|
|
popupStatus: JSON.stringify(arg).replace(/"/g, '\"'),
|
|
}
|
|
postFetcher(`/api/v1/canvas-popup-status`, params)
|
|
},
|
|
)
|
|
|
|
return { getModuleSelection, handleModuleSelectionTotal, trigger }
|
|
}
|