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 39c9219 commit 8563b26
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/hooks/useRemoveCheckedItemsFromCart.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { useSetRecoilState } from 'recoil';
import { CART_URL } from '../constants/url';
import { cartState } from '../recoil';
import { CartItem } from '../types';
import { useFetchData } from './useFetchData';

export const useRemoveCheckedItemsFromCart = (checkedItemIdList: number[]) => {
const setCart = useSetRecoilState(cartState);

const { api } = useFetchData();

const removeAllCheckedItemsFromCart = (prev: CartItem[]) => {
const cart = [...prev];
const updatedCart = cart.filter((item) => !checkedItemIdList.includes(item.id));
Expand All @@ -13,6 +17,7 @@ export const useRemoveCheckedItemsFromCart = (checkedItemIdList: number[]) => {
};

const removeCheckedItemsFromCart = () => {
checkedItemIdList.forEach((id) => api.delete(`${CART_URL}/${id}`, { id }));
setCart((prev: CartItem[]) => removeAllCheckedItemsFromCart(prev));
};

Expand Down

0 comments on commit 8563b26

Please sign in to comment.