diff --git a/frontend/src/components/ChecklistList/CompareBanner.tsx b/frontend/src/components/ChecklistList/CompareBanner.tsx index 4cb45d83d..7be71d6ea 100644 --- a/frontend/src/components/ChecklistList/CompareBanner.tsx +++ b/frontend/src/components/ChecklistList/CompareBanner.tsx @@ -37,7 +37,7 @@ const S = { &:hover, &:active { - background-color: ${({ theme }) => theme.palette.yellow500}; + background-color: ${({ theme }) => theme.palette.yellow300}; } `, Box: styled.div` diff --git a/frontend/src/components/NewChecklist/ChecklistCategory.tsx b/frontend/src/components/NewChecklist/ChecklistCategory.tsx index 6c1d19e66..5eda6d8bf 100644 --- a/frontend/src/components/NewChecklist/ChecklistCategory.tsx +++ b/frontend/src/components/NewChecklist/ChecklistCategory.tsx @@ -33,7 +33,7 @@ const ChecklistCategory = (props: ChecklistType) => { isPreview ? ( <> - {index !== category.questions.length - 1 && } + {index !== category.questions.length - 1 && } ) : ( <> @@ -44,7 +44,7 @@ const ChecklistCategory = (props: ChecklistType) => { deleteAnswer={props.deleteAnswer} questionSelectedAnswer={props.questionSelectedAnswer} /> - {index !== category.questions.length - 1 && } + {index !== category.questions.length - 1 && } ), )} diff --git a/frontend/src/components/NewChecklist/ChecklistQuestion/ChecklistQuestion.tsx b/frontend/src/components/NewChecklist/ChecklistQuestion/ChecklistQuestion.tsx index 8f920dd72..e68ce42e9 100644 --- a/frontend/src/components/NewChecklist/ChecklistQuestion/ChecklistQuestion.tsx +++ b/frontend/src/components/NewChecklist/ChecklistQuestion/ChecklistQuestion.tsx @@ -19,14 +19,10 @@ const emotionPhrase: Record = { }; const ChecklistQuestion = ({ question, addAnswer, deleteAnswer, questionSelectedAnswer }: Props) => { - // const [answer, setAnswer] = useState(null); - const handleClick = (newAnswer: number) => { if (questionSelectedAnswer(question.questionId) === newAnswer) { - // setAnswer(null); deleteAnswer(question.questionId); } else { - // setAnswer(newAnswer); addAnswer({ questionId: question.questionId, newAnswer }); } }; diff --git a/frontend/src/hooks/useChecklistAnswer.ts b/frontend/src/hooks/useChecklistAnswer.ts new file mode 100644 index 000000000..c3d090a5d --- /dev/null +++ b/frontend/src/hooks/useChecklistAnswer.ts @@ -0,0 +1,39 @@ +import { useCallback, useState } from 'react'; + +import { addAnswerProps } from '@/pages/ChecklistPreviewPage'; +import { ChecklistFormAnswer } from '@/types/checklist'; + +const useChecklistAnswer = () => { + const [checklistAnswers, setChecklistAnswers] = useState([]); + + const questionSelectedAnswer = (targetId: number) => { + const targetQuestion = checklistAnswers.filter(e => e.questionId === targetId); + if (!targetQuestion[0]) return; + return targetQuestion[0]?.answer; + }; + + const addAnswer = useCallback( + ({ questionId, newAnswer }: addAnswerProps) => { + const target = [...checklistAnswers].find(answer => answer.questionId === questionId); + if (target) { + const newAnswers = [...checklistAnswers].map(answer => + answer.questionId === questionId ? { questionId, answer: newAnswer } : answer, + ); + setChecklistAnswers(newAnswers); + return; + } + setChecklistAnswers(prev => [...prev, { questionId, answer: newAnswer }]); + }, + [checklistAnswers], + ); + + const deleteAnswer = (questionId: number) => { + setChecklistAnswers(prevAnswers => { + return prevAnswers.filter(answer => answer.questionId !== questionId); + }); + }; + + return { addAnswer, deleteAnswer, questionSelectedAnswer }; +}; + +export default useChecklistAnswer; diff --git a/frontend/src/pages/NewChecklistPage/NewChecklistPage.tsx b/frontend/src/pages/NewChecklistPage/NewChecklistPage.tsx index a05caf5c3..3f4edae8e 100644 --- a/frontend/src/pages/NewChecklistPage/NewChecklistPage.tsx +++ b/frontend/src/pages/NewChecklistPage/NewChecklistPage.tsx @@ -5,6 +5,7 @@ import { postChecklist } from '@/apis/checklist'; import Button from '@/components/common/Button/Button'; import Header from '@/components/common/Header/Header'; import Tabs, { Menu } from '@/components/common/Tabs/Tabs'; +import useChecklistAnswer from '@/hooks/useChecklistAnswer'; import useInputs from '@/hooks/useInput'; import NewChecklistInfoTemplate from '@/pages/NewChecklistPage/NewChecklistInfoTemplate'; import NewChecklistTemplate from '@/pages/NewChecklistPage/NewChecklistTemplate'; @@ -53,6 +54,8 @@ const NewChecklistPage = () => { /*체크리스트 답변*/ const [checklistAnswers, setChecklistAnswers] = useState([]); + const { addAnswer, deleteAnswer, questionSelectedAnswer } = useChecklistAnswer(); + //TODO: 프롭스 드릴링 등 나중에 리팩토링 필요 가능성 const onSubmitChecklist = () => { const emotionAnswers: ChecklistFormAfterAnswer[] = checklistAnswers.map(question => { @@ -72,12 +75,6 @@ const NewChecklistPage = () => { fetchNewChecklist(); }; - const questionSelectedAnswer = (targetId: number) => { - const targetQuestion = checklistAnswers.filter(e => e.questionId === targetId); - if (!targetQuestion[0]) return; - return targetQuestion[0]?.answer; - }; - return (
{ )} diff --git a/frontend/src/pages/NewChecklistPage/NewChecklistTemplate.tsx b/frontend/src/pages/NewChecklistPage/NewChecklistTemplate.tsx index cd488f4ef..7843b89d8 100644 --- a/frontend/src/pages/NewChecklistPage/NewChecklistTemplate.tsx +++ b/frontend/src/pages/NewChecklistPage/NewChecklistTemplate.tsx @@ -1,5 +1,5 @@ import styled from '@emotion/styled'; -import { useCallback, useEffect, useState } from 'react'; +import { useEffect, useState } from 'react'; import { getChecklistQuestions } from '@/apis/checklist'; import Accordion from '@/components/common/Accordion/Accordion'; @@ -12,12 +12,16 @@ export interface addAnswerProps { } interface Props { + addAnswer: ({ questionId, newAnswer }: addAnswerProps) => void; + deleteAnswer: (questionId: number) => void; answers: ChecklistFormAnswer[]; setAnswers: React.Dispatch>; questionSelectedAnswer: (questionId: number) => number | undefined; } -const NewChecklistTemplate = ({ answers, setAnswers, questionSelectedAnswer }: Props) => { +const NewChecklistTemplate = (props: Props) => { + const { questionSelectedAnswer, addAnswer, deleteAnswer } = props; + const [checklistQuestions, setChecklistQuestions] = useState([]); useEffect(() => { @@ -28,27 +32,6 @@ const NewChecklistTemplate = ({ answers, setAnswers, questionSelectedAnswer }: P fetchChecklist(); }, []); - const addAnswer = useCallback( - ({ questionId, newAnswer }: addAnswerProps) => { - const target = [...answers].find(answer => answer.questionId === questionId); - if (target) { - const newAnswers = [...answers].map(answer => - answer.questionId === questionId ? { questionId, answer: newAnswer } : answer, - ); - setAnswers(newAnswers); - return; - } - setAnswers(prev => [...prev, { questionId, answer: newAnswer }]); - }, - [answers], - ); - - const deleteAnswer = (questionId: number) => { - setAnswers(prevAnswers => { - return prevAnswers.filter(answer => answer.questionId !== questionId); - }); - }; - return (