From 11011cc9b657711667e4ebe4dea13429601d480a Mon Sep 17 00:00:00 2001 From: Dami Kim Date: Sun, 14 Nov 2021 21:34:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CartService=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#244)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Cart/CartProduct/CartProduct.tsx | 4 +- .../Cart/CartProduct/CartProductsService.ts | 37 --------------- .../src/components/Cart/CartService.tsx | 46 +++++++++++++++++-- 3 files changed, 45 insertions(+), 42 deletions(-) delete mode 100644 itda-front/src/components/Cart/CartProduct/CartProductsService.ts diff --git a/itda-front/src/components/Cart/CartProduct/CartProduct.tsx b/itda-front/src/components/Cart/CartProduct/CartProduct.tsx index 98ac270..52b247c 100644 --- a/itda-front/src/components/Cart/CartProduct/CartProduct.tsx +++ b/itda-front/src/components/Cart/CartProduct/CartProduct.tsx @@ -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 ); diff --git a/itda-front/src/components/Cart/CartProduct/CartProductsService.ts b/itda-front/src/components/Cart/CartProduct/CartProductsService.ts deleted file mode 100644 index f47cfad..0000000 --- a/itda-front/src/components/Cart/CartProduct/CartProductsService.ts +++ /dev/null @@ -1,37 +0,0 @@ -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 } from "types/CartTypes"; - -const CartService = () => { - 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 UpdateCartService = (cartList: any) => { - const { isError } = useQuery("updateCartList", () => - cartAPI.post.updateCartProductList(cartList) - ); -}; - -const DeleteCartService = (productId: number) => { - const { data } = useQuery("deleteCartList", () => - cartAPI.delete.deleteCartProduct(productId) - ); -}; - -export { CartService, UpdateCartService, DeleteCartService }; diff --git a/itda-front/src/components/Cart/CartService.tsx b/itda-front/src/components/Cart/CartService.tsx index 3130e2c..954bf1d 100644 --- a/itda-front/src/components/Cart/CartService.tsx +++ b/itda-front/src/components/Cart/CartService.tsx @@ -1,5 +1,45 @@ -const CartService = () => { - return
; +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 };