91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
'use client'
|
|
|
|
import { useEffect } from 'react'
|
|
import { useRecoilState, useRecoilValue } from 'recoil'
|
|
import useSWR from 'swr'
|
|
import useSWRMutation from 'swr/mutation'
|
|
import { useAxios } from '../useAxios'
|
|
import { unescapeString } from '@/util/common-utils'
|
|
import { moduleSelectionDataState } from '@/store/selectedModuleOptions'
|
|
import { compasDegAtom } from '@/store/orientationAtom'
|
|
import { currentCanvasPlanState } from '@/store/canvasAtom'
|
|
|
|
export function useCanvasPopupStatusController(param = 1) {
|
|
const popupType = parseInt(param)
|
|
|
|
const [compasDeg, setCompasDeg] = useRecoilState(compasDegAtom)
|
|
const [moduleSelectionDataStore, setModuleSelectionDataStore] = useRecoilState(moduleSelectionDataState)
|
|
const { getFetcher, postFetcher } = useAxios()
|
|
|
|
const currentCanvasPlan = useRecoilValue(currentCanvasPlanState)
|
|
console.log('🚀 ~ Orientation ~ currentCanvasPlan:', currentCanvasPlan)
|
|
|
|
const {
|
|
data: popupStatus,
|
|
error,
|
|
isLoading,
|
|
} = useSWR(
|
|
popupType ? `/api/v1/canvas-popup-status?objectNo=${currentCanvasPlan.objectNo}&planNo=${currentCanvasPlan.planNo}&popupType=${popupType}` : null,
|
|
getFetcher,
|
|
)
|
|
|
|
useEffect(() => {
|
|
console.log('🚀 ~ useEffect ~ popupStatus:', popupStatus)
|
|
if (popupStatus) {
|
|
switch (parseInt(popupStatus?.popupType)) {
|
|
case 1:
|
|
setCompasDeg(popupStatus.popupStatus)
|
|
break
|
|
case 2:
|
|
setModuleSelectionDataStore(JSON.parse(unescapeString(popupStatus.popupStatus)))
|
|
break
|
|
case 3:
|
|
break
|
|
case 4:
|
|
break
|
|
case 5:
|
|
break
|
|
case 6:
|
|
break
|
|
default:
|
|
}
|
|
} else {
|
|
switch (popupType) {
|
|
case 1:
|
|
setCompasDeg(0)
|
|
break
|
|
case 2:
|
|
setModuleSelectionDataStore({
|
|
common: {},
|
|
roofConstructions: [],
|
|
})
|
|
break
|
|
case 3:
|
|
break
|
|
case 4:
|
|
break
|
|
case 5:
|
|
break
|
|
case 6:
|
|
break
|
|
default:
|
|
}
|
|
}
|
|
}, [popupStatus])
|
|
|
|
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, '\"'),
|
|
}
|
|
postFetcher(`/api/v1/canvas-popup-status`, params)
|
|
},
|
|
)
|
|
|
|
return { trigger }
|
|
}
|