-
Notifications
You must be signed in to change notification settings - Fork 0
[week6][박민서] 과제 제출 #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: mindong
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { getCookie } from "../utils/cookie"; | |
| function VotePage() { | ||
| const navigate = useNavigate(); | ||
| const userId = getCookie("userId"); | ||
| const [images, setImages] = useState(""); | ||
|
|
||
| const [thumbsUpImage, setThumbsUpImage] = useState( | ||
| require("../assets/images/thumbs-up-icon.png") | ||
|
|
@@ -20,22 +21,64 @@ function VotePage() { | |
|
|
||
| const getImage = async () => { | ||
| try { | ||
| // ### TO DO ### | ||
| // ############# | ||
| const response = await axios.get( | ||
| "https://api.thecatapi.com/v1/images/search?limit=1&size=full", | ||
| { | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "x-api-key": | ||
| "live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg", | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| const data = response.data; | ||
| setImages(data[0]); | ||
| } catch (err) { | ||
| console.log(err); | ||
| } | ||
| }; | ||
|
|
||
| const vote = async (val) => { | ||
| // val: up or down | ||
| try { | ||
| // ### TO DO ### | ||
| // ############# | ||
| const response = await axios.post( | ||
| "https://api.thecatapi.com/v1/votes", | ||
| { | ||
| image_id: images.id, | ||
| sub_id: userId, | ||
| value: val, | ||
| }, | ||
| { | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "x-api-key": | ||
| "live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg", | ||
| }, | ||
| } | ||
| ); | ||
| getImage(); | ||
| } catch (err) { | ||
| console.log(err); | ||
| } | ||
| }; | ||
|
|
||
| const handleThumbsUpHover = () => { | ||
| setThumbsUpImage(require("../assets/images/thumbs-up-click.png")); | ||
| }; | ||
|
|
||
| const handleThumbsUpLeave = () => { | ||
| setThumbsUpImage(require("../assets/images/thumbs-up-icon.png")); | ||
| }; | ||
|
|
||
| const handleThumbsDownHover = () => { | ||
| setThumbsDownImage(require("../assets/images/thumbs-down-click.png")); | ||
| }; | ||
|
|
||
| const handleThumbsDownLeave = () => { | ||
| setThumbsDownImage(require("../assets/images/thumbs-down-icon.png")); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="w-full h-screen flex flex-col justify-center items-center gap-10"> | ||
| <div className="absolute top-[5%] right-[16%] flex gap-5"> | ||
|
|
@@ -61,20 +104,30 @@ function VotePage() { | |
| /> | ||
| <div className="w-2/3 h-2/3 py-2 border-4 rounded-2xl border-[#FF6841] flex justify-center items-center"> | ||
| <div className="w-full h-[90%] flex justify-evenly items-center"> | ||
| <img | ||
| // ### ONE CAT IMAGE ### | ||
| className="w-3/5 h-full border-[3px] rounded-xl border-[#FF6841]" | ||
| /> | ||
| {images ? ( | ||
| <img | ||
| src={images.url} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 삼항 연산자를 src 안에서 하면 더 간단하게 할 수도 있을 것 같아요! |
||
| className="w-3/5 h-full border-[3px] rounded-xl border-[#FF6841]" | ||
| /> | ||
| ) : ( | ||
| <div></div> | ||
| )} | ||
| <div className="w-1/3 flex gap-12 justify-center"> | ||
| <img | ||
| src={thumbsUpImage} | ||
| className="w-20 h-20 cursor-pointer" | ||
| // ### thumbsUpImage Event ### | ||
| onMouseOver={handleThumbsUpHover} | ||
| onMouseOut={handleThumbsUpLeave} | ||
| onClick={() => vote(1)} | ||
| /> | ||
| <img | ||
| src={thumbsDownImage} | ||
| className="w-20 h-20 cursor-pointer" | ||
| // ### thumbsDownImage Event ### | ||
| onMouseOver={handleThumbsDownHover} | ||
| onMouseOut={handleThumbsDownLeave} | ||
| onClick={() => vote(-1)} | ||
|
Comment on lines
+122
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 잘 하셨는데, 이 부분에서 조금 더 가독성을 높일 수 있는 방법을 알려드릴게요! 을 위에 선언해주고, vote(1) 대신 vote(VoteValue.UP), vote(-1) 대신 vote(VoteValue.DOWN) 로 값을 입력해줄 수 있어요! 내가 짠 코드도 일주일만 지나면 이게 뭐였지 하는 경우가 많고, 타인과 협업을 할 때는 다른 사람의 코드를 읽을 때 숫자나 문자가 그대로 하드코딩 되어있으면 이해하는데 시간이 필요한 경우가 많아서 이렇게 원하는 값을 좀 더 직관적으로 랩핑해주면 훨씬 더 좋은 코드를 작성할 수 있습니다~!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 구체적이고 친절한 코멘트 감사드립니다! 참고해서 다음 번에는 더 좋은 코드 짤 수 있게 하겠습니다 ㅎㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 짱이에요 수고하셨습니다~! |
||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 다시 data.id 랑 data.url 을 새로운 변수에 저장해서 그걸 setImages() 로 pass 했는데 그럴 필요가 없었네요!