From ab5867b0cbf1f5d87744a90adb6777e2ff8b51e9 Mon Sep 17 00:00:00 2001 From: Mohammed Al-yaseen Date: Thu, 15 Jan 2026 07:22:48 +0300 Subject: [PATCH] implment send amount gift api --- .../checkout/components/CheckoutButton.tsx | 16 +++- src/pages/checkout/hooks/useGidtAmount.ts | 82 +++++++++++++++++++ src/redux/api/others.ts | 9 ++ src/utils/constants.ts | 3 +- 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/pages/checkout/hooks/useGidtAmount.ts diff --git a/src/pages/checkout/components/CheckoutButton.tsx b/src/pages/checkout/components/CheckoutButton.tsx index ff181bd..a1eacfa 100644 --- a/src/pages/checkout/components/CheckoutButton.tsx +++ b/src/pages/checkout/components/CheckoutButton.tsx @@ -10,14 +10,17 @@ import { EqualltyChoiceBottomSheet } from "pages/pay/components/splitBill/Equall import { SplitBillChoiceBottomSheet } from "pages/pay/components/splitBill/SplitBillChoiceBottomSheet"; import { CustomAmountChoiceBottomSheet } from "pages/pay/components/splitBill/CustomAmountChoiceBottomSheet"; import { PayForYourItemsChoiceBottomSheet } from "pages/pay/components/splitBill/PayForYourItemsChoiceBottomSheet"; +import useGidtAmount from "../hooks/useGidtAmount"; +import { GiftType } from "components/CustomBottomSheet/GiftTypeBottomSheet"; type SplitWay = "customAmount" | "equality" | "payForItems" | null; export default function CheckoutButton({ form }: { form: FormInstance }) { const dispatch = useAppDispatch(); const { t } = useTranslation(); - const { orderType } = useAppSelector(selectCart); + const { orderType, giftDetails } = useAppSelector(selectCart); const { handleCreateOrder } = useOrder(); + const { handleCreateGiftAmount } = useGidtAmount(); const [selectedSplitWay, setSelectedSplitWay] = useState(null); const [ isSplitBillChoiceBottomSheetOpen, @@ -47,11 +50,18 @@ export default function CheckoutButton({ form }: { form: FormInstance }) { const handlePlaceOrderClick = useCallback(async () => { try { await form.validateFields(); - handleCreateOrder(); + if ( + orderType === OrderType.Gift && + giftDetails?.giftType === GiftType.Vouchers + ) { + handleCreateGiftAmount(); + } else { + handleCreateOrder(); + } } catch (error) { console.log(error); } - }, [handleCreateOrder, form]); + }, [handleCreateOrder, handleCreateGiftAmount, form, orderType, giftDetails]); const shouldShowSplitBill = useMemo( () => orderType === OrderType.DineIn, diff --git a/src/pages/checkout/hooks/useGidtAmount.ts b/src/pages/checkout/hooks/useGidtAmount.ts new file mode 100644 index 0000000..df76067 --- /dev/null +++ b/src/pages/checkout/hooks/useGidtAmount.ts @@ -0,0 +1,82 @@ +import { message } from "antd"; +import { + clearCart, + selectCart, + selectGrandTotal, +} from "features/order/orderSlice"; +import { useCallback } from "react"; +import { useTranslation } from "react-i18next"; +import { useNavigate, useParams } from "react-router-dom"; +import { useCreateGiftAmountMutation } from "redux/api/others"; +import { useAppDispatch, useAppSelector } from "redux/hooks"; +import { PAYMENT_CONFIRMATION_URL } from "utils/constants"; + +export default function useGidtAmount() { + const dispatch = useAppDispatch(); + const navigate = useNavigate(); + const { t } = useTranslation(); + const { subdomain } = useParams(); + const restaurantID = localStorage.getItem("restaurantID"); + const { giftDetails } = useAppSelector(selectCart); + + const orderPrice = useAppSelector(selectGrandTotal); + + const [createGiftAmount] = useCreateGiftAmountMutation(); + + const handleCreateGiftAmount = useCallback(() => { + const loadingMessageKey = "create-order-loader"; + message.loading({ + content: t("order.creatingOrder", { + defaultValue: "Creating order...", + }), + key: loadingMessageKey, + duration: 0, + }); + + createGiftAmount({ + sender_name: giftDetails?.senderName, + sender_phone: giftDetails?.senderPhone, + sender_email: giftDetails?.senderEmail, + receiver_name: giftDetails?.receiverName, + receiver_phone: giftDetails?.receiverPhone, + special_message: giftDetails?.message, + keep_name_secret: giftDetails?.isSecret, + restaurant_id: restaurantID, + order_amount: orderPrice, + gift_card_id: giftDetails?.cardId, + }).then((res: unknown) => { + message.destroy(loadingMessageKey); + const mutationResult = res as { + data?: { result?: { orderID?: string } }; + error?: { data?: { message?: string } }; + }; + if (mutationResult.error) + message.error( + mutationResult.error.data?.message || t("order.createOrderFailed"), + ); + else { + const redirectMessageKey = "order-redirect-loader"; + + message.loading({ + content: t("order.redirectingToPayment", { + defaultValue: "Redirecting to payment...", + }), + key: redirectMessageKey, + duration: 0, + }); + window.location.href = `${PAYMENT_CONFIRMATION_URL}/${mutationResult.data?.result?.orderID}`; + dispatch(clearCart()); + } + }); + }, [ + createGiftAmount, + orderPrice, + giftDetails, + restaurantID, + t, + navigate, + subdomain, + dispatch, + ]); + return { handleCreateGiftAmount }; +} diff --git a/src/redux/api/others.ts b/src/redux/api/others.ts index df0286f..dea70c5 100644 --- a/src/redux/api/others.ts +++ b/src/redux/api/others.ts @@ -12,6 +12,7 @@ import { EGIFT_CARDS_URL, REDEEM_DETAILS_URL, LOYALTY_HISTORY_URL, + CREATE_GIFT_AMOUNT_URL, } from "utils/constants"; import { OrderDetails } from "pages/checkout/hooks/types"; @@ -202,6 +203,13 @@ export const branchApi = baseApi.injectEndpoints({ return response.result.data.orders; }, }), + createGiftAmount: builder.mutation({ + query: (body: any) => ({ + url: CREATE_GIFT_AMOUNT_URL, + method: "POST", + body, + }), + }), }), }); export const { @@ -218,4 +226,5 @@ export const { useGetEGiftCardsQuery, useGetRedeemDetailsQuery, useGetLoyaltyHistoryQuery, + useCreateGiftAmountMutation, } = branchApi; diff --git a/src/utils/constants.ts b/src/utils/constants.ts index c668b82..f82102b 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -110,4 +110,5 @@ export const PAYMENT_CONFIRMATION_URL = `https://menu.fascano.com/payment/confir export const DISCOUNT_URL = `${BASE_URL}getDiscount`; export const EGIFT_CARDS_URL = `${BASE_URL}gift/cards`; export const REDEEM_DETAILS_URL = `${BASE_URL}gift/getGiftOrderByVoucherCode`; -export const LOYALTY_HISTORY_URL = `${BASE_URL}loyaltyHistory`; \ No newline at end of file +export const LOYALTY_HISTORY_URL = `${BASE_URL}loyaltyHistory`; +export const CREATE_GIFT_AMOUNT_URL = `${BASE_URL}gift/addGiftAmount`; \ No newline at end of file