Skip to content
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

[Feat/#165] 스토어 찜 취소 / 디자인 찜 취소 API 연결 #167

Merged
merged 8 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 9 additions & 2 deletions src/apis/likes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { usePostStoreLikes } from './usePostStoreLikes';
import { useDeleteCakeLikes } from './useDeleteCakeLikes';
import { useDeleteStoreLikes } from './useDeleteStoreLikes';
import { usePostCakeLikes } from './usePostCakeLikes';
import { usePostStoreLikes } from './usePostStoreLikes';

export { usePostStoreLikes, usePostCakeLikes };
export {
usePostStoreLikes,
usePostCakeLikes,
useDeleteCakeLikes,
useDeleteStoreLikes,
};
23 changes: 23 additions & 0 deletions src/apis/likes/useDeleteCakeLikes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useMutation } from '@tanstack/react-query';

import { instance } from '@apis/instance';

import { END_POINT } from '@constants';

import { MutateResposneType } from '@types';

const deleteCakeLikes = async (cakeId: number): Promise<MutateResposneType> => {
try {
const response = await instance.delete(END_POINT.DELETE_CAKE_LIKES(cakeId));
return response.data;
} catch (error) {
console.log(error);
throw error;
}
};

export const useDeleteCakeLikes = () => {
return useMutation({
mutationFn: (cakeId: number) => deleteCakeLikes(cakeId),
});
};
27 changes: 27 additions & 0 deletions src/apis/likes/useDeleteStoreLikes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useMutation } from '@tanstack/react-query';

import { instance } from '@apis/instance';

import { END_POINT } from '@constants';

import { MutateResposneType } from '@types';

const deleteStoreLikes = async (
storeId: number
): Promise<MutateResposneType> => {
try {
const response = await instance.delete(
END_POINT.DELETE_STORE_LIKES(storeId)
);
return response.data;
} catch (error) {
console.log(error);
throw error;
}
};

export const useDeleteStoreLikes = () => {
return useMutation({
mutationFn: (storeId: number) => deleteStoreLikes(storeId),
});
};
54 changes: 43 additions & 11 deletions src/components/common/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { ButtonHTMLAttributes } from 'react';
import React, { ButtonHTMLAttributes, useState } from 'react';

import { usePostCakeLikes, usePostStoreLikes } from '@apis/likes';
import {
useDeleteCakeLikes,
useDeleteStoreLikes,
usePostCakeLikes,
usePostStoreLikes,
} from '@apis/likes';

import {
IcFillLikeOff36,
Expand Down Expand Up @@ -46,27 +51,54 @@ const IconButton = ({
count,
itemId,
}: IconButtonProps) => {
const { mutate: postCakeLikes } = usePostCakeLikes();
const [localActive, setLocalActive] = useState(isActive);
const [localCount, setLocalCount] = useState<number | undefined>(count);

const { mutate: postStoreLikes } = usePostStoreLikes();
const { mutate: postCakeLikes } = usePostCakeLikes();
const { mutate: deleteStoreLikes } = useDeleteStoreLikes();
const { mutate: deleteCakeLikes } = useDeleteCakeLikes();

const handleButtonClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
if (itemId === undefined) return;

const isStore = buttonType === 'save24' || buttonType === 'save28';

if (itemId !== undefined) {
if (buttonType === 'save24' || buttonType === 'save28') {
postStoreLikes(itemId);
} else {
postCakeLikes(itemId);
const optimisticUpdate = () => {
setLocalActive((prev) => !prev);
if (localCount !== undefined) {
setLocalCount(localCount + (localActive ? -1 : 1));
}
};

const rollbackUpdate = () => {
setLocalActive((prev) => !prev);
if (localCount !== undefined) {
setLocalCount(count);
}
};

optimisticUpdate();

if (localActive) {
(isStore ? deleteStoreLikes : deleteCakeLikes)(itemId, {
onError: () => rollbackUpdate(),
});
} else {
(isStore ? postStoreLikes : postCakeLikes)(itemId, {
onError: () => rollbackUpdate(),
});
}
};

return (
<button className={buttonStyle({ buttonType })} onClick={handleButtonClick}>
{isActive
{localActive
? buttonIcon[buttonType]?.active
: buttonIcon[buttonType]?.inactive}
{count !== undefined && (
<span className={countStyle({ buttonType })}>{count}</span>
{localCount !== undefined && (
<span className={countStyle({ buttonType })}>{localCount}</span>
)}
</button>
);
Expand Down
2 changes: 2 additions & 0 deletions src/constants/apis/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export const END_POINT = {
FETCH_CAKE_RANK: '/api/v1/cake/rank',
POST_CAKE_LIKES: (cakeId: number) => `/api/v1/cake/likes/${cakeId}`,
POST_STORE_LIKES: (storeId: number) => `/api/v1/store/likes/${storeId}`,
DELETE_CAKE_LIKES: (cakeId: number) => `/api/v1/cake/likes/${cakeId}`,
DELETE_STORE_LIKES: (storeId: number) => `/api/v1/store/likes/${storeId}`
Copy link
Collaborator

Choose a reason for hiding this comment

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

혹시 URL에서 cake와 store만 달라지는 것 같은데 혹시 번거롭지 않다면 찜 API 하나, 찜 삭제 API 하나로 통합해보는 건 어떨까요..?

POST_LIKE = (type: 'cake' | 'store', id: number) => `/api/v1/${type}/likes/${id}`;
DELETE_LIKE = (type: 'cake' | 'store', id: number) => `/api/v1/${type}/likes/${id}`;

간단한 예시긴 하지만 이런 식으로 통합해볼 수.. 있을 것 같아요! 물론 리팩토링 때 고려해봐도 좋긴 합니다:)

} as const;
Loading