-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FE] refactor: 리뷰 작성 페이지 리팩토링 (#219)
* refactor: 각 모달별로 상태와 동작을 관리하는 훅 분리 * refactor: 리뷰 작성 폼의 로직을 훅으로 분리 * refactor: 리뷰 작성 페이지에 분리한 훅 적용 * refactor: URL에서 reviewRequestCode를 추출하는 로직을 훅으로 분리 * chore: 불필요한 테스트용 코드 제거 * chore: ErrorModal을 닫을 때 errorMessage 상태도 초기값으로 변경하도록 수정
- Loading branch information
Showing
6 changed files
with
148 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useState } from 'react'; | ||
|
||
const useAlertModal = () => { | ||
const [isAlertModalOpen, setIsAlertModalOpen] = useState(false); | ||
|
||
const openAlertModal = () => setIsAlertModalOpen(true); | ||
const closeAlertModal = () => setIsAlertModalOpen(false); | ||
|
||
return { | ||
isAlertModalOpen, | ||
openAlertModal, | ||
closeAlertModal, | ||
}; | ||
}; | ||
|
||
export default useAlertModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useState } from 'react'; | ||
|
||
const useConfirmModal = () => { | ||
const [isConfirmModalOpen, setIsConfirmModalOpen] = useState(false); | ||
|
||
const openConfirmModal = () => setIsConfirmModalOpen(true); | ||
const closeConfirmModal = () => setIsConfirmModalOpen(false); | ||
|
||
return { | ||
isConfirmModalOpen, | ||
openConfirmModal, | ||
closeConfirmModal, | ||
}; | ||
}; | ||
|
||
export default useConfirmModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useState } from 'react'; | ||
|
||
const useErrorModal = () => { | ||
const [isErrorModalOpen, setIsErrorModalOpen] = useState(false); | ||
const [errorMessage, setErrorMessage] = useState(''); | ||
|
||
const openErrorModal = (errorMessage: string) => { | ||
setErrorMessage(errorMessage); | ||
setIsErrorModalOpen(true); | ||
}; | ||
|
||
const closeErrorModal = () => { | ||
setErrorMessage(''); | ||
setIsErrorModalOpen(false); | ||
}; | ||
|
||
return { | ||
isErrorModalOpen, | ||
errorMessage, | ||
openErrorModal, | ||
closeErrorModal, | ||
}; | ||
}; | ||
|
||
export default useErrorModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { getDataToWriteReviewApi } from '@/apis/review'; | ||
import { REVIEW } from '@/constants'; | ||
import { Keyword, ReviewContent, WritingReviewInfoData } from '@/types'; | ||
|
||
interface UseReviewFormProps { | ||
reviewRequestCode: string; | ||
openErrorModal: (errorMessage: string) => void; | ||
} | ||
|
||
const useReviewForm = ({ reviewRequestCode, openErrorModal }: UseReviewFormProps) => { | ||
const [dataToWrite, setDataToWrite] = useState<WritingReviewInfoData | null>(null); | ||
const [answers, setAnswers] = useState<ReviewContent[]>([]); | ||
const [selectedKeywords, setSelectedKeywords] = useState<number[]>([]); | ||
|
||
const isValidAnswersLength = !answers.some((id) => id.answer.length < REVIEW.answerMinLength); | ||
const isValidKeywordSelection = | ||
selectedKeywords.length >= REVIEW.keywordMinCount && selectedKeywords.length <= REVIEW.keywordMaxCount; | ||
const isValidForm = isValidAnswersLength && isValidKeywordSelection; | ||
|
||
useEffect(() => { | ||
const getDataToWrite = async () => { | ||
const data = await getDataToWriteReviewApi(reviewRequestCode); | ||
setDataToWrite(data); | ||
setAnswers(data.questions.map((question) => ({ questionId: question.id, answer: '' }))); | ||
}; | ||
|
||
getDataToWrite(); | ||
}, [reviewRequestCode]); | ||
|
||
const handleAnswerChange = (questionId: number, value: string) => { | ||
setAnswers((prev) => | ||
prev.map((answer) => (answer.questionId === questionId ? { ...answer, answer: value } : answer)), | ||
); | ||
}; | ||
|
||
const handleKeywordButtonClick = (keyword: Keyword) => { | ||
if (selectedKeywords.length === REVIEW.keywordMaxCount && !selectedKeywords.includes(keyword.id)) { | ||
openErrorModal('키워드는 최대 5개까지 선택할 수 있어요.'); | ||
return; | ||
} | ||
|
||
setSelectedKeywords((prev) => | ||
prev.includes(keyword.id) ? selectedKeywords.filter((id) => id !== keyword.id) : [...prev, keyword.id], | ||
); | ||
}; | ||
|
||
return { | ||
dataToWrite, | ||
answers, | ||
selectedKeywords, | ||
isValidForm, | ||
handleAnswerChange, | ||
handleKeywordButtonClick, | ||
}; | ||
}; | ||
|
||
export default useReviewForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useLocation } from 'react-router'; | ||
|
||
const useReviewRequestCode = () => { | ||
const location = useLocation(); | ||
const params = location.pathname.split('/'); | ||
const reviewRequestCode = params.slice(-1).toString(); | ||
|
||
return { reviewRequestCode }; | ||
}; | ||
|
||
export default useReviewRequestCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters