From 411b6f40afc51e8c441623afe5603111d1299e05 Mon Sep 17 00:00:00 2001 From: chaem03 Date: Thu, 14 Nov 2024 21:49:35 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]-=EB=88=84=EA=B5=AC=20=EC=88=98=EC=A0=95:?= =?UTF-8?q?=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20=EC=8B=9C=20=EC=84=A0?= =?UTF-8?q?=ED=83=9D=EB=90=9C=20=ED=82=A4=EC=9B=8C=EB=93=9C=20=EB=93=A4?= =?UTF-8?q?=EA=B3=A0=EC=98=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/nuguPatch/NuguPatch.jsx | 6 ----- src/pages/nuguPatch/_hooks/usePatchChip.js | 29 ++++++++++++++++------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/pages/nuguPatch/NuguPatch.jsx b/src/pages/nuguPatch/NuguPatch.jsx index 50fea09..6d81ed7 100644 --- a/src/pages/nuguPatch/NuguPatch.jsx +++ b/src/pages/nuguPatch/NuguPatch.jsx @@ -23,12 +23,6 @@ export const NuguPatch = () => { try { const userData = await getNugu(); setUpdateData(userData); - const initialSelectedChips = [ - userData.keyword1, - userData.keyword2, - userData.keyword3, - ]; - handleClickStatus(initialSelectedChips); } catch (err) { console.error("Patch-user정보 가져오기 실패", err); } diff --git a/src/pages/nuguPatch/_hooks/usePatchChip.js b/src/pages/nuguPatch/_hooks/usePatchChip.js index ef3712d..5d40f00 100644 --- a/src/pages/nuguPatch/_hooks/usePatchChip.js +++ b/src/pages/nuguPatch/_hooks/usePatchChip.js @@ -1,23 +1,38 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useRecoilState } from "recoil"; import { signUpState } from "@atoms/signUpState"; import { CHIP_DATA } from "@constants/chip"; export const usePatchChip = () => { const [updateData, setUpdateData] = useRecoilState(signUpState); const [selectedChip, setSelectedChip] = useState( - CHIP_DATA.map( - (chip) => - updateData.keyword1 === chip || - updateData.keyword2 === chip || - updateData.keyword3 === chip - ) + new Array(CHIP_DATA.length).fill(false) ); const selectedCount = selectedChip.filter((chip) => chip).length; + useEffect(() => { + const initialSelectedChips = [ + updateData.keyword1, + updateData.keyword2, + updateData.keyword3, + ]; + + const updatedChip = [...selectedChip]; + + initialSelectedChips.forEach((keyword) => { + const index = CHIP_DATA.indexOf(keyword); + if (index !== -1) { + updatedChip[index] = true; + } + }); + + setSelectedChip(updatedChip); + }, [updateData]); + const handleClickStatus = (index) => { const updatedChip = [...selectedChip]; console.log(updatedChip); updatedChip[index] = !updatedChip[index]; + if (updatedChip.filter((chip) => chip).length > 3) { return; }