-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. usePostReviews 위치 변경 : 상위 Review에 위치, 리뷰목록 보는 UI에 영향을 줌 => PostReview로 내려옴 2. 불필요한 함수 정의 생략 : 정의하자마자 실행하고 단한번인 경우 정의없이 바로 실행 3. 여기저기 흩어진 검증코드 모으기 : 42 인증 및 글자수 검증은 submit 클릭시 한번만 수행 4. 기타 함수, 변수명 선언형으로 수정함
- Loading branch information
Showing
3 changed files
with
48 additions
and
78 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 |
---|---|---|
@@ -1,41 +1,24 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useApi } from "../../hook/useApi"; | ||
import { useState } from "react"; | ||
|
||
import { useNewDialog } from "../../hook/useNewDialog"; | ||
import axiosPromise from "../../util/axios"; | ||
|
||
type Props = { | ||
bookInfoId: number; | ||
changeTab: (tab: number) => void; | ||
resetTab: () => void; | ||
}; | ||
|
||
export const usePostReview = ({ bookInfoId, changeTab }: Props) => { | ||
const checkLogin = JSON.parse(window.localStorage.getItem("user") || "{}"); | ||
export const usePostReview = ({ bookInfoId, resetTab }: Props) => { | ||
const [content, setContent] = useState(""); | ||
const { request } = useApi("post", "/reviews", { | ||
bookInfoId, | ||
content, | ||
}); | ||
|
||
const refineResponse = () => { | ||
changeTab(0); | ||
}; | ||
const { addDialogWithTitleAndMessage } = useNewDialog(); | ||
|
||
const displayError = () => { | ||
const title = | ||
checkLogin === null | ||
? "로그인 후 입력해주세요." | ||
: "10자 이상 420자 이하로 입력해주세요."; | ||
addDialogWithTitleAndMessage(title, title, ""); | ||
}; | ||
|
||
useEffect(() => { | ||
const req = () => { | ||
if (content !== "") { | ||
request(refineResponse, displayError); | ||
} | ||
}; | ||
req(); | ||
}, [content]); | ||
const { addErrorDialog } = useNewDialog(); | ||
|
||
return { setContent }; | ||
const request = () => | ||
axiosPromise("post", "/reviews", { | ||
bookInfoId, | ||
content, | ||
}) | ||
.then(resetTab) | ||
.catch(addErrorDialog); | ||
return { content, setContent, request }; | ||
}; |
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
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