Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/routes/VoteHistoryPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ function VoteHistoryPage() {

const getImages = async () => {
try {
let response;
// ### TO DO ###
// #############
const response = await axios.get(
"https://api.thecatapi.com/v1/votes",
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_xEi0AeOWPilSMxy9Gfogg39wSAoPQAEafUTjX90ifRBVjaBvXPEQ4a9l51p5fuU3",
},
}
);
const data = response.data;
const imageSet = [];

Expand Down Expand Up @@ -64,8 +71,8 @@ function VoteHistoryPage() {
<img
key={img.url}
src={img.url}
className={`object-cover w-full h-full border-[3px] border-[#FF6841] rounded-xl
### FILL ME ###
className={`object-cover w-full h-full border-[3px] rounded-xl
${img.value < 0 ? "border-red-500" : "border-blue-500"}
`}
/>
</div>
Expand Down
64 changes: 57 additions & 7 deletions src/routes/VotePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,56 @@ function VotePage() {
const [thumbsDownImage, setThumbsDownImage] = useState(
require("../assets/images/thumbs-down-icon.png")
);

const [image, setImage] = useState({});

useEffect(() => {
getImage();
}, []);

const getImage = async () => {
try {
// ### TO DO ###
// #############
const response = await axios.get(
"https://api.thecatapi.com/v1/images/search",
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_xEi0AeOWPilSMxy9Gfogg39wSAoPQAEafUTjX90ifRBVjaBvXPEQ4a9l51p5fuU3",
},
}
);

const data = response.data;

const voteImage = {
id: data[0].id,
url: data[0].url,
};
setImage(voteImage);

} catch (err) {
console.log(err);
}
};

const vote = async (val) => {
try {
// ### TO DO ###
// #############
const response = await axios.post(
"https://api.thecatapi.com/v1/votes",
{
image_id: image.id,
sub_id: userId,
value: val
},
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_xEi0AeOWPilSMxy9Gfogg39wSAoPQAEafUTjX90ifRBVjaBvXPEQ4a9l51p5fuU3",
},
}
);
} catch (err) {
console.log(err);
}
Expand Down Expand Up @@ -62,19 +94,37 @@ 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 ###
src={image.url}
className="w-3/5 h-full border-[3px] rounded-xl border-[#FF6841]"
/>
<div className="w-1/3 flex gap-12 justify-center">
<img
src={thumbsUpImage}
className="w-20 h-20 cursor-pointer"
// ### thumbsUpImage Event ###
onMouseOver = {() => setThumbsUpImage(
require("../assets/images/thumbs-up-click.png")
)}
onMouseOut = {() => setThumbsUpImage(
require("../assets/images/thumbs-up-icon.png")
)}
onClick = {() => {
vote(1);
getImage();
}}
/>
<img
src={thumbsDownImage}
className="w-20 h-20 cursor-pointer"
// ### thumbsDownImage Event ###
onMouseOver = {() => setThumbsDownImage(
require("../assets/images/thumbs-down-click.png")
)}
onMouseOut = {() => setThumbsDownImage(
require("../assets/images/thumbs-down-icon.png")
)}
onClick = {() => {
vote(-1);
getImage();
}}
Comment on lines -72 to +127

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 하셨는데, 이 부분에서 조금 더 가독성을 높일 수 있는 방법을 알려드릴게요!

const VoteValue = {
UP: 1,
DOWN: -1,
};
을 위에 선언해주고, vote(1) 대신 vote(VoteValue.UP), vote(-1) 대신 vote(VoteValue.DOWN) 로 값을 입력해줄 수 있어요!

내가 짠 코드도 일주일만 지나면 이게 뭐였지 하는 경우가 많고, 타인과 협업을 할 때는 다른 사람의 코드를 읽을 때 숫자나 문자가 그대로 하드코딩 되어있으면 이해하는데 시간이 필요한 경우가 많아서 이렇게 원하는 값을 좀 더 직관적으로 랩핑해주면 훨씬 더 좋은 코드를 작성할 수 있습니다~!

/>
</div>
</div>
Expand Down