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
26 changes: 24 additions & 2 deletions src/routes/FavouriteHistoryPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,30 @@ function FavouriteHistoryPage() {

const getImages = async () => {
try {
// ### TO DO ###
// #############
const response = await axios.get(
`https://api.thecatapi.com/v1/favourites?sub_id=${userId}`,
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg",
},
}
);

const data = response.data;
const imageSet = [];

data.map((e) => {
imageSet.push({
id: e.image.id,
url: e.image.url,
isFavourite: true,
favouriteId: e.id,
});
});

setImages(imageSet);
} catch (err) {
console.log(err);
}
Expand Down
68 changes: 62 additions & 6 deletions src/routes/MainPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,82 @@ function HomePage() {

const getImages = async () => {
try {
// ### TO DO ###
// #############
const response = await axios.get(
"https://api.thecatapi.com/v1/images/search?limit=8&size=small",
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg",
},
}
);

const data = response.data;
const imageSet = [];

data.map((e) => {
imageSet.push({
id: e.id,
url: e.url,
isFavourite: false,
favouriteId: null,
});
});

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

const favouritingImage = async (imgId) => {
try {
// ### TO DO ###
// #############
const response = await axios.post(
"https://api.thecatapi.com/v1/favourites",
{
image_id: imgId,
sub_id: userId,
},
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg",
},
}
);

const newImages = [...images];
const idx = newImages.findIndex((e) => e.id === imgId);
newImages[idx].isFavourite = true;
newImages[idx].favouriteId = response.data.id;

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

const unFavouritingImage = async (favouriteId) => {
try {
// ### TO DO ###
// #############
const newImages = [...images];
const idx = newImages.findIndex((e) => e.favouriteId === favouriteId);
newImages[idx].isFavourite = false;
newImages[idx].favouriteId = null;

setImages(newImages);

const response = await axios.delete(
`https://api.thecatapi.com/v1/favourites/${favouriteId}`,
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg",
},
}
);
} catch (err) {
console.log(err);
}
Expand Down
15 changes: 13 additions & 2 deletions src/routes/VoteHistoryPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ function VoteHistoryPage() {
const getImages = async () => {
try {
let response;
response = await axios.get(
`https://api.thecatapi.com/v1/votes?sub_id=${userId}`,
{
headers: {
"Content-Type": "application/json",
"x-api-key":
"live_kQOoJ0Uf7sfgdZjO0kywH1mSu52d8kGfCFpy2uraLk4RC8LzWS3D2Jx6GLBB88Rg",
},
}
);
// ### TO DO ###
// #############
const data = response.data;
Expand Down Expand Up @@ -64,8 +74,9 @@ 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] ${
img.value > 0 ? "border-blue-500" : "border-red-500"
} rounded-xl
`}
/>
</div>
Expand Down
69 changes: 61 additions & 8 deletions src/routes/VotePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]);

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 했는데 그럴 필요가 없었네요!

} 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">
Expand All @@ -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}
Copy link

@yebin1926 yebin1926 May 6, 2024

Choose a reason for hiding this comment

The 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

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) 로 값을 입력해줄 수 있어요!

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

Copy link
Author

Choose a reason for hiding this comment

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

구체적이고 친절한 코멘트 감사드립니다! 참고해서 다음 번에는 더 좋은 코드 짤 수 있게 하겠습니다 ㅎㅎㅎ

Choose a reason for hiding this comment

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

짱이에요 수고하셨습니다~!

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