Skip to content

Commit

Permalink
feat: api delete 하는 기능 구현 및 필요없는 로직 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
feb-dain committed May 22, 2023
1 parent d69aa12 commit 515e78e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/hooks/useCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const useSetCart = (id: number) => {
const removeItemFromCart = () => {
setCart((prev: CartItem[]) => {
const { cart, cartItemIndex, alreadyHasCartItem } = findCartItemIndex(prev);
api.delete(`${CART_URL}/${id}`, { id });

if (alreadyHasCartItem) return removeProduct(cart, cartItemIndex);

Expand Down
15 changes: 3 additions & 12 deletions src/mock/handlers/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,10 @@ export const cartHandlers = [
}),

// 장바구니 아이템 삭제
rest.delete(`${CART_URL}/:id`, (req, res, ctx) => {
const cartItemId = Number(req.params.id);
rest.delete(`${CART_URL}/:id`, async (req, res, ctx) => {
const { id } = await req.json();
const cart = JSON.parse(getDataFromLocalStorage(KEY_CART));
const isInCart = (id: number) => cart.some((cartItem: CartItem) => cartItem.product.id === id);
const productExists = isInCart(cartItemId);
const updatedCart = cart.filter((cartItem: CartItem) => cartItem.id !== cartItemId);

if (!productExists) {
return res(
ctx.status(404),
ctx.json({ message: '장바구니에 해당 상품이 존재하지 않습니다.' })
);
}
const updatedCart = cart.filter((cartItem: CartItem) => cartItem.id !== id);

updateLocalStorage(updatedCart);

Expand Down

0 comments on commit 515e78e

Please sign in to comment.