131 lines
4.9 KiB
TypeScript
131 lines
4.9 KiB
TypeScript
import { message } from "antd";
|
|
import {
|
|
clearCart,
|
|
selectCart,
|
|
selectGrandTotal,
|
|
selectHighestPricedLoyaltyItem,
|
|
} from "features/order/orderSlice";
|
|
import { OrderType } from "pages/checkout/hooks/types.ts";
|
|
import { useCallback } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useNavigate, useParams } from "react-router-dom";
|
|
import { useCreateOrderMutation } from "redux/api/others";
|
|
import { useAppDispatch, useAppSelector } from "redux/hooks";
|
|
import { PAYMENT_CONFIRMATION_URL } from "utils/constants";
|
|
import { Customer } from "../../otp/types";
|
|
|
|
export default function useOrder() {
|
|
const dispatch = useAppDispatch();
|
|
const navigate = useNavigate();
|
|
const { t } = useTranslation();
|
|
const { subdomain } = useParams();
|
|
const restaurantID = localStorage.getItem("restaurantID");
|
|
const { mobilenumber, user_uuid } = JSON.parse(
|
|
localStorage.getItem("customer") || "{}",
|
|
) as Customer;
|
|
const {
|
|
items,
|
|
coupon,
|
|
tip,
|
|
tables,
|
|
specialRequest,
|
|
phone,
|
|
estimateTime,
|
|
officeDetails,
|
|
orderType,
|
|
giftDetails,
|
|
location,
|
|
discount,
|
|
} = useAppSelector(selectCart);
|
|
const highestLoyaltyItem = useAppSelector(selectHighestPricedLoyaltyItem);
|
|
const { useLoyaltyPoints } = useAppSelector(selectCart);
|
|
|
|
const orderPrice = useAppSelector(selectGrandTotal);
|
|
|
|
const [createOrder] = useCreateOrderMutation();
|
|
|
|
const getDeliveryMethod = useCallback(() => {
|
|
if (orderType === OrderType.Delivery) return 1;
|
|
if (orderType === OrderType.Pickup) return 2;
|
|
if (orderType === OrderType.DineIn) return 3;
|
|
if (orderType === OrderType.Gift) return 10;
|
|
if (orderType === OrderType.ScheduledOrder) return 9;
|
|
if (orderType === OrderType.ToRoom) return 5;
|
|
if (orderType === OrderType.ToOffice) return 4;
|
|
return undefined;
|
|
}, [orderType]);
|
|
|
|
const handleCreateOrder = useCallback(() => {
|
|
createOrder({
|
|
phone: mobilenumber || phone || giftDetails?.senderPhone,
|
|
comment: specialRequest,
|
|
delivery_method: getDeliveryMethod(),
|
|
timeslot: "",
|
|
table_id: tables[0],
|
|
deliveryType: orderType,
|
|
type: "table-pickup",
|
|
// user_id: id,
|
|
restorant_id: restaurantID,
|
|
items: items.map((i) => ({
|
|
...i,
|
|
qty: i.quantity,
|
|
extras: i.extras?.map((e) => e.id) || [],
|
|
extrasgroup: i.extrasgroup || [],
|
|
order_item_comment: i.comment || "",
|
|
})),
|
|
office_no: officeDetails?.officeNo || "",
|
|
vatvalue: 0,
|
|
...(discount.isDiscount ? { couponID: coupon } : {}),
|
|
...(discount.isGift ? { discountGiftCode: coupon } : {}),
|
|
discountAmount: discount.value || 0,
|
|
paymentType: "cod",
|
|
uuid: user_uuid,
|
|
pickup_comments: "",
|
|
pickup_time: estimateTime,
|
|
delivery_pickup_interval: "",
|
|
orderPrice: orderPrice,
|
|
use_loylaty: useLoyaltyPoints && highestLoyaltyItem ? 1 : 0,
|
|
useWallet: 0,
|
|
tip,
|
|
userCoordinates:
|
|
location?.lat && location?.lng
|
|
? `${location?.lat},${location?.lng}`
|
|
: "",
|
|
delivery_address: location?.address,
|
|
...(orderType === OrderType.Gift
|
|
? {
|
|
receiverName: giftDetails?.receiverName,
|
|
receiverPhone: giftDetails?.receiverPhone,
|
|
specialMessage: giftDetails?.message,
|
|
keepNameSecret: giftDetails?.isSecret,
|
|
senderEmail: giftDetails?.senderEmail,
|
|
senderPhone: giftDetails?.senderPhone,
|
|
senderName: giftDetails?.senderName,
|
|
dineType: orderType,
|
|
}
|
|
: {}),
|
|
})
|
|
.then((res: any) => {
|
|
if (res.error)
|
|
message.error(res.error.data.message || t("order.createOrderFailed"));
|
|
else {
|
|
if (orderType === "gift")
|
|
// navigate(`/${PAYMENT_CONFIRMATION_URL}/${res.data.result.orderID}`, {
|
|
// replace: false,
|
|
// });
|
|
window.location.href = `${PAYMENT_CONFIRMATION_URL}/${res.data.result.orderID}`;
|
|
// window.open(
|
|
// `${PAYMENT_CONFIRMATION_URL}/${res.data.result.orderID}`,
|
|
// );
|
|
else navigate(`/${subdomain}/order/${res.data.result.orderID}`);
|
|
dispatch(clearCart());
|
|
localStorage.setItem("orderID", res.data.result.orderID);
|
|
}
|
|
})
|
|
.catch((error: any) => {
|
|
console.error("Create Order failed:", error);
|
|
});
|
|
}, [createOrder, mobilenumber, phone, giftDetails?.senderPhone, giftDetails?.receiverName, giftDetails?.receiverPhone, giftDetails?.message, giftDetails?.isSecret, giftDetails?.senderEmail, giftDetails?.senderName, specialRequest, getDeliveryMethod, tables, orderType, restaurantID, items, officeDetails?.officeNo, discount.isDiscount, discount.isGift, discount.value, coupon, user_uuid, estimateTime, orderPrice, useLoyaltyPoints, highestLoyaltyItem, tip, location?.lat, location?.lng, location?.address, t, navigate, subdomain, dispatch]);
|
|
return { handleCreateOrder };
|
|
}
|