diff --git a/src/components/loadingAnimation/LoadingAnimation.scss b/src/components/loadingAnimation/LoadingAnimation.scss index 13a6238f..2dccf387 100644 --- a/src/components/loadingAnimation/LoadingAnimation.scss +++ b/src/components/loadingAnimation/LoadingAnimation.scss @@ -3,7 +3,6 @@ .loading-container { width: 100vw; height: 100vh; - background-color: #fff; position: fixed; top: 0; left: 0; diff --git a/src/hooks/quries/usePostOrder.ts b/src/hooks/quries/usePostOrder.ts new file mode 100644 index 00000000..0a8ac52c --- /dev/null +++ b/src/hooks/quries/usePostOrder.ts @@ -0,0 +1,41 @@ +import { postOrderApi } from "@/api/postOrderApi"; +import { orderResultState } from "@/states/orderResultState"; +import { useMutation } from "react-query"; +import { useNavigate } from "react-router-dom"; +import { useSetRecoilState } from "recoil"; + +export const usePostOrder = (orderData: OrderData) => { + const setOrderResult = useSetRecoilState(orderResultState); + const navigate = useNavigate(); + const postOrderMutation = useMutation( + () => postOrderApi("/api/reservations", orderData), + { + onSuccess: () => { + navigate("/order/result?=true"); + setOrderResult(true); + }, + onError: () => { + navigate("/order/result?=false"); + setOrderResult(false); + }, + } + ); + + const isLoading = postOrderMutation.isLoading; + + return { + ...postOrderMutation, + isLoading, + }; +}; + +interface OrderData { + roomId: number; + visitorName: string; + visitorPhone: string; + startDate: string; + endDate: string; + couponId?: number; + totalPrice: number; + payMethod: string; +} diff --git a/src/pages/order/Order.tsx b/src/pages/order/Order.tsx index 263e655a..f494ecb3 100644 --- a/src/pages/order/Order.tsx +++ b/src/pages/order/Order.tsx @@ -1,8 +1,6 @@ import { memo, useEffect, useState } from "react"; -import { useRecoilValue, useSetRecoilState } from "recoil"; +import { useRecoilValue } from "recoil"; import { OrderItemTypes, orderState } from "@/states/orderState"; -import { postOrderApi } from "@/api/postOrderApi"; -import { useNavigate } from "react-router-dom"; import _debounce from "lodash/debounce"; import TermsAgreement from "@/components/termsAgreement/TermsAgreement"; @@ -23,11 +21,11 @@ import DiscountBadge from "./discountBadge/DiscountBadge"; import "./order.scss"; import { initialPaymentMethod } from "@/constant/initialPaymentMethod"; import { usedCouponState } from "@/states/usedCouponState"; -import { orderResultState } from "@/states/orderResultState"; +import { usePostOrder } from "@/hooks/quries/usePostOrder"; +import LoadingAnimation from "@/components/loadingAnimation/LoadingAnimation"; const Order = memo(() => { const [userName, setUserName] = useState(""); - const navigate = useNavigate(); const [userPhoneNumber, setUserPhoneNumber] = useState(""); const [selectedMethod, setSelectedMethod] = useState(initialPaymentMethod[0]); const [isAllCheck, setIsAllCheck] = useState(false); @@ -40,46 +38,39 @@ const Order = memo(() => { ? discountAmt : orderData.reduce((total, item) => total + item.price, 0); const usedCoupon = useRecoilValue(usedCouponState); - const setOrderResult = useSetRecoilState(orderResultState); - useEffect(() => { - localStorage.setItem("orderState", JSON.stringify(orderData)); - }, [orderData]); - - const handleClick = () => { - postOrderApiFromAccommodation(); + const requestBody = { + roomId: orderData[0].id, + visitorName: userName, + visitorPhone: userPhoneNumber, + startDate: orderData[0].startDate, + endDate: orderData[0].endDate, + couponId: usedCoupon?.id, + totalPrice: totalOrderPrice, + payMethod: selectedMethod.payMethod, }; - const postOrderApiFromAccommodation = async () => { - const requestBody = { - roomId: orderData[0].id, - visitorName: userName, - visitorPhone: userPhoneNumber, - startDate: orderData[0].startDate, - endDate: orderData[0].endDate, - couponId: usedCoupon?.id, - totalPrice: totalOrderPrice, - payMethod: selectedMethod.payMethod, - }; - try { - await postOrderApi("/api/reservations", requestBody); - navigate("/order/result?=true"); - setOrderResult(true); - } catch (error) { - navigate("/order/result?=false"); - setOrderResult(false); - } + const postOrderMutation = usePostOrder(requestBody); + const totalPrice = orderData.reduce((sum, item) => sum + item.price, 0); + + const handleClick = async () => { + await postOrderMutation.mutateAsync(); }; + useEffect(() => { + localStorage.setItem("orderState", JSON.stringify(orderData)); + }, [orderData]); + useEffect(() => { setIsAllValidationPass(isAllCheck && isBookerValidationPass); }, [isAllCheck, isBookerValidationPass]); - const totalPrice = orderData.reduce((sum, item) => sum + item.price, 0); - return (