From 7b5c0b615c9e71e8fa0f99b4f222b8691725a6ad Mon Sep 17 00:00:00 2001 From: Daseul Kim Date: Fri, 7 Feb 2025 15:36:18 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=94=8C=EB=9E=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존 add-plan, canvas-statuses 두 개 api를 사용하던 내용을 api 변경에 따라 add-plan api 한 개만 사용하도록 변경 --- src/hooks/usePlan.js | 78 ++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/src/hooks/usePlan.js b/src/hooks/usePlan.js index 04bb983f..f49c31fd 100644 --- a/src/hooks/usePlan.js +++ b/src/hooks/usePlan.js @@ -150,48 +150,56 @@ export function usePlan(params = {}) { } /** - * 물건번호(object) plan 추가 (canvas 생성 전 planNo 할당) + * 신규 plan 추가 + * + * case 1) 초기 플랜 생성 : isInitPlan = true, isCopy = false + * case 2) 빈 플랜 생성 : isInitPlan = false, isCopy = false + * case 3) 복제 플랜 생성 : isInitPlan = false, isCopy = true */ - const postObjectPlan = async (userId, objectNo) => { - return await promisePost({ url: '/api/object/add-plan', data: { userId: userId, objectNo: objectNo } }) + const postObjectPlanTemp = async (userId, objectNo, isCopy = false, isInitPlan = false) => { + const planData = isCopy + ? { + userId: userId, + objectNo: objectNo, + copyFlg: '1', + planNo: currentCanvasPlan?.planNo, + } + : { + userId: userId, + objectNo: objectNo, + copyFlg: '0', + } + await promisePost({ url: '/api/object/add-plan', data: planData }) .then((res) => { - return res.data.planNo - }) - .catch((error) => { - swalFire({ text: error.response.data.message, icon: 'error' }) - return null - }) - } + let newPlan = { + id: res.data.canvasId, + objectNo: objectNo, + planNo: res.data.planNo, + userId: userId, + canvasStatus: '', + isCurrent: true, + bgImageName: null, + mapPositionAddress: null, + } - /** - * 신규 canvas 데이터를 저장 - */ - const postCanvasStatus = async (userId, objectNo, canvasStatus, isInitPlan = false) => { - const planNo = await postObjectPlan(userId, objectNo) - if (!planNo) return - - const planData = { - userId: userId, - objectNo: objectNo, - planNo: planNo, - bgImageName: currentCanvasPlan?.bgImageName ?? null, - mapPositionAddress: currentCanvasPlan?.mapPositionAddress ?? null, - canvasStatus: canvasToDbFormat(canvasStatus), - } - await promisePost({ url: '/api/canvas-management/canvas-statuses', data: planData }) - .then((res) => { - const newPlan = { id: res.data, objectNo: objectNo, planNo: planNo, userId: userId, canvasStatus: canvasStatus, isCurrent: true } - setCurrentCanvasPlan(newPlan) if (isInitPlan) { // 초기 플랜 생성인 경우 플랜 목록 초기화 + setCurrentCanvasPlan(newPlan) setPlans([newPlan]) } else { + if (isCopy) { + // 복제 플랜 생성인 경우 현재 캔버스 데이터를 복제 + newPlan.canvasStatus = currentCanvasData() + newPlan.bgImageName = currentCanvasPlan?.bgImageName ?? null + newPlan.mapPositionAddress = currentCanvasPlan?.mapPositionAddress ?? null + } + setCurrentCanvasPlan(newPlan) setPlans((plans) => [...plans.map((plan) => ({ ...plan, isCurrent: false })), newPlan]) swalFire({ text: getMessage('plan.message.save') }) } }) .catch((error) => { - swalFire({ text: error.message, icon: 'error' }) + swalFire({ text: error.response.data.message, icon: 'error' }) }) } @@ -337,13 +345,13 @@ export function usePlan(params = {}) { text: `Plan ${currentCanvasPlan.planNo} ` + getMessage('plan.message.confirm.copy'), type: 'confirm', confirmFn: async () => { - await postCanvasStatus(userId, objectNo, currentCanvasData(), false) + await postObjectPlanTemp(userId, objectNo, true, false) }, denyFn: async () => { - await postCanvasStatus(userId, objectNo, '', false) + await postObjectPlanTemp(userId, objectNo, false, false) }, }) - : await postCanvasStatus(userId, objectNo, '', false) + : await postObjectPlanTemp(userId, objectNo, false, false) } /** @@ -394,12 +402,12 @@ export function usePlan(params = {}) { */ const loadCanvasPlanData = async (userId, objectNo, planNo) => { console.log('🚀 ~ loadCanvasPlanData ~ userId, objectNo, planNo:', userId, objectNo, planNo) - await getCanvasByObjectNo(userId, objectNo, planNo).then((res) => { + await getCanvasByObjectNo(userId, objectNo, planNo).then(async (res) => { if (res.length > 0) { setCurrentCanvasPlan(res.find((plan) => plan.planNo === planNo)) setPlans(res) } else { - postCanvasStatus(userId, objectNo, '', true) + await postObjectPlanTemp(userId, objectNo, false, true) } }) }