Skip to content

Commit

Permalink
feat: CartService 로직 구현 (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
damilog committed Nov 14, 2021
1 parent ee1cb19 commit 11011cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
4 changes: 2 additions & 2 deletions itda-front/src/components/Cart/CartProduct/CartProduct.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useState } from "react";
import { useRecoilState } from "recoil";
import S from "../CartStyles";
import { CartService } from "components/Cart/CartProduct/CartProductsService";
import { GetCart } from "components/Cart/CartService";
import CarProductCard from "./CartProductCard";
import CheckButton from "components/common/Atoms/CheckButton";
import { selectedProduct, cartProductData } from "stores/CartAtoms";

const CartProduct = () => {
CartService();
GetCart();
const [cartProductState, setCartProductState] = useRecoilState(
cartProductData
);
Expand Down
37 changes: 0 additions & 37 deletions itda-front/src/components/Cart/CartProduct/CartProductsService.ts

This file was deleted.

46 changes: 43 additions & 3 deletions itda-front/src/components/Cart/CartService.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
const CartService = () => {
return <div></div>;
import { useQuery } from "react-query";
import { useSetRecoilState, useRecoilValue } from "recoil";
import cartAPI from "util/API/cartAPI";
import { cartProductData } from "stores/CartAtoms";
import { isSideDrawerClicked } from "stores/SideDrawerAtoms";
import { ICart, ICartSelectedProduct } from "types/CartTypes";

const GetCart = () => {
const setCartProductState = useSetRecoilState(cartProductData);
const sideDrawerToggleState = useRecoilValue(isSideDrawerClicked);

const { isLoading } = useQuery(
["cartProducts", sideDrawerToggleState],
cartAPI.get.getCartProductList,
{
onSuccess: data => {
const tmpProducts: ICart[] = Object.values(data?.data.products);
setCartProductState(tmpProducts);
},
}
);
return { isLoading };
};

const PostCart = (cartList: ICartSelectedProduct) => {
const { isError } = useQuery("updateCartList", () =>
cartAPI.post.updateCartProduct(cartList)
);
};

const PostCartAll = (newCartList: ICart[]) => {
const requestData = newCartList.map(({ productId, productCount }) => {
return {
id: productId,
count: productCount,
};
});
cartAPI.post.updateAllCartProduct(requestData);
};

const DeleteCart = (productId: number) => {
useQuery("deleteCartList", () => cartAPI.delete.deleteCartProduct(productId));
};

export default CartService;
export { GetCart, PostCart, PostCartAll, DeleteCart };

0 comments on commit 11011cc

Please # to comment.