refactor: current plan 이동 시 useEffect 호출 제거, 초기 plan 저장 시 팝업 미표출하도록 수정

This commit is contained in:
Daseul Kim 2025-01-23 17:54:02 +09:00
parent b75b155646
commit 90b52abb7f
2 changed files with 25 additions and 28 deletions

View File

@ -24,15 +24,15 @@ export default function CanvasFrame() {
const currentMenu = useRecoilValue(currentMenuState) const currentMenu = useRecoilValue(currentMenuState)
const { floorPlanState } = useContext(FloorPlanContext) const { floorPlanState } = useContext(FloorPlanContext)
const { contextMenu, handleClick } = useContextMenu() const { contextMenu, handleClick } = useContextMenu()
const { selectedPlan } = usePlan() const { currentCanvasPlan } = usePlan()
const totalDisplay = useRecoilValue(totalDisplaySelector) // const totalDisplay = useRecoilValue(totalDisplaySelector) //
const { setIsGlobalLoading } = useContext(QcastContext) const { setIsGlobalLoading } = useContext(QcastContext)
const loadCanvas = () => { const loadCanvas = () => {
if (canvas) { if (canvas) {
canvas?.clear() // . canvas?.clear() // .
if (selectedPlan?.canvasStatus && floorPlanState.objectNo === selectedPlan.objectNo) { if (currentCanvasPlan?.canvasStatus && floorPlanState.objectNo === currentCanvasPlan.objectNo) {
canvas?.loadFromJSON(JSON.parse(selectedPlan.canvasStatus), function () { canvas?.loadFromJSON(JSON.parse(currentCanvasPlan.canvasStatus), function () {
canvasLoadInit() //config canvasLoadInit() //config
canvas?.renderAll() // . canvas?.renderAll() // .
}) })
@ -43,7 +43,7 @@ export default function CanvasFrame() {
useEffect(() => { useEffect(() => {
loadCanvas() loadCanvas()
}, [selectedPlan, canvas]) }, [currentCanvasPlan, canvas])
useEffect(() => { useEffect(() => {
setIsGlobalLoading(false) setIsGlobalLoading(false)

View File

@ -128,7 +128,7 @@ export function usePlan(params = {}) {
/** /**
* objectNo에 해당하는 canvas 목록을 조회 * objectNo에 해당하는 canvas 목록을 조회
*/ */
const getCanvasByObjectNo = async (userId, objectNo) => { const getCanvasByObjectNo = async (userId, objectNo, planNo) => {
return await get({ url: `/api/canvas-management/canvas-statuses/by-object/${objectNo}/${userId}` }).then((res) => return await get({ url: `/api/canvas-management/canvas-statuses/by-object/${objectNo}/${userId}` }).then((res) =>
res.map((item) => ({ res.map((item) => ({
id: item.id, id: item.id,
@ -136,7 +136,7 @@ export function usePlan(params = {}) {
planNo: item.planNo, planNo: item.planNo,
userId: item.userId, userId: item.userId,
canvasStatus: dbToCanvasFormat(item.canvasStatus), canvasStatus: dbToCanvasFormat(item.canvasStatus),
isCurrent: false, isCurrent: planNo === item.planNo,
bgImageName: item.bgImageName, bgImageName: item.bgImageName,
mapPositionAddress: item.mapPositionAddress, mapPositionAddress: item.mapPositionAddress,
})), })),
@ -174,14 +174,15 @@ export function usePlan(params = {}) {
} }
await promisePost({ url: '/api/canvas-management/canvas-statuses', data: planData }) await promisePost({ url: '/api/canvas-management/canvas-statuses', data: planData })
.then((res) => { .then((res) => {
const newPlan = { id: res.data, objectNo: objectNo, planNo: planNo, userId: userId, canvasStatus: canvasStatus, isCurrent: true }
setCurrentCanvasPlan(newPlan)
if (isInitPlan) { if (isInitPlan) {
// 초기 플랜 생성인 경우 플랜 목록 초기화 // 초기 플랜 생성인 경우 플랜 목록 초기화
setPlans([{ id: res.data, objectNo: objectNo, planNo: planNo, userId: userId, canvasStatus: canvasStatus }]) setPlans([newPlan])
} else { } else {
setPlans((plans) => [...plans, { id: res.data, objectNo: objectNo, planNo: planNo, userId: userId, canvasStatus: canvasStatus }]) setPlans((plans) => [...plans.map((plan) => ({ ...plan, isCurrent: false })), newPlan])
}
updateCurrentPlan(res.data)
swalFire({ text: getMessage('plan.message.save') }) swalFire({ text: getMessage('plan.message.save') })
}
}) })
.catch((error) => { .catch((error) => {
swalFire({ text: error.message, icon: 'error' }) swalFire({ text: error.message, icon: 'error' })
@ -255,14 +256,16 @@ export function usePlan(params = {}) {
} }
setEstimateContextState(res.data) setEstimateContextState(res.data)
// 클릭한 플랜 탭으로 이동 // 클릭한 플랜 탭으로 이동
updateCurrentPlan(newCurrentId) setCurrentCanvasPlan(plans.find((plan) => plan.id === newCurrentId))
setPlans((plans) => plans.map((plan) => ({ ...plan, isCurrent: plan.id === newCurrentId })))
} else { } else {
swalFire({ text: getMessage('estimate.menu.move.valid1') }) swalFire({ text: getMessage('estimate.menu.move.valid1') })
} }
} else { } else {
// 발전시뮬레이션 // 발전시뮬레이션
if (estimateDetail.tempFlg === '0') { if (estimateDetail.tempFlg === '0') {
updateCurrentPlan(newCurrentId) setCurrentCanvasPlan(plans.find((plan) => plan.id === newCurrentId))
setPlans((plans) => plans.map((plan) => ({ ...plan, isCurrent: plan.id === newCurrentId })))
} else { } else {
swalFire({ text: getMessage('simulator.menu.move.valid1') }) swalFire({ text: getMessage('simulator.menu.move.valid1') })
} }
@ -278,26 +281,18 @@ export function usePlan(params = {}) {
}) })
} else { } else {
if (!currentCanvasPlan || currentCanvasPlan.id !== newCurrentId) { if (!currentCanvasPlan || currentCanvasPlan.id !== newCurrentId) {
saveCanvas() await saveCanvas()
} }
updateCurrentPlan(newCurrentId) setCurrentCanvasPlan(plans.find((plan) => plan.id === newCurrentId))
setPlans((plans) => plans.map((plan) => ({ ...plan, isCurrent: plan.id === newCurrentId })))
} }
} }
const updateCurrentPlan = (newCurrentId) => {
setPlans((plans) =>
plans.map((plan) => {
return { ...plan, isCurrent: plan.id === newCurrentId }
}),
)
}
useEffect(() => { useEffect(() => {
setCurrentCanvasPlan(plans.find((plan) => plan.isCurrent) || null) setSelectedPlan(currentCanvasPlan)
setSelectedPlan(plans.find((plan) => plan.isCurrent))
handleCurrentPlanUrl() handleCurrentPlanUrl()
// setBgImage() // setBgImage()
}, [plans]) }, [currentCanvasPlan])
const handleCurrentPlanUrl = () => { const handleCurrentPlanUrl = () => {
const currentPlan = plans.find((plan) => plan.isCurrent) const currentPlan = plans.find((plan) => plan.isCurrent)
@ -368,7 +363,8 @@ export function usePlan(params = {}) {
if (!lastPlan) { if (!lastPlan) {
setCurrentCanvasPlan(null) setCurrentCanvasPlan(null)
} else if (targetPlan.id !== lastPlan.id) { } else if (targetPlan.id !== lastPlan.id) {
updateCurrentPlan(lastPlan.id) setCurrentCanvasPlan(lastPlan)
setPlans((plans) => plans.map((plan) => ({ ...plan, isCurrent: plan.id === lastPlan.id })))
} }
} }
@ -377,10 +373,10 @@ export function usePlan(params = {}) {
*/ */
const loadCanvasPlanData = async (userId, objectNo, planNo) => { const loadCanvasPlanData = async (userId, objectNo, planNo) => {
console.log('🚀 ~ loadCanvasPlanData ~ userId, objectNo, planNo:', userId, objectNo, planNo) console.log('🚀 ~ loadCanvasPlanData ~ userId, objectNo, planNo:', userId, objectNo, planNo)
await getCanvasByObjectNo(userId, objectNo).then((res) => { await getCanvasByObjectNo(userId, objectNo, planNo).then((res) => {
if (res.length > 0) { if (res.length > 0) {
setCurrentCanvasPlan(res.find((plan) => plan.planNo === planNo))
setPlans(res) setPlans(res)
updateCurrentPlan(res.find((plan) => plan.planNo === planNo).id)
} else { } else {
postCanvasStatus(userId, objectNo, '', true) postCanvasStatus(userId, objectNo, '', true)
} }
@ -397,6 +393,7 @@ export function usePlan(params = {}) {
return { return {
canvas, canvas,
plans, plans,
currentCanvasPlan,
selectedPlan, selectedPlan,
saveCanvas, saveCanvas,
handleCurrentPlan, handleCurrentPlan,