import { Button, Card, Divider, Flex, Image, Progress, Tooltip } from "antd"; import Ads2 from "components/Ads/Ads2"; import { CancelOrderBottomSheet } from "components/CustomBottomSheet/CancelOrderBottomSheet"; import LocationIcon from "components/Icons/LocationIcon"; import InvoiceIcon from "components/Icons/order/InvoiceIcon"; import TimeIcon from "components/Icons/order/TimeIcon"; import OrderDishIcon from "components/Icons/OrderDishIcon"; import PaymentDetails from "components/PaymentDetails/PaymentDetails"; import ProHeader from "components/ProHeader/ProHeader"; import ProInputCard from "components/ProInputCard/ProInputCard"; import ProText from "components/ProText"; import ProTitle from "components/ProTitle"; import dayjs from "dayjs"; import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate, useParams } from "react-router-dom"; import { useGetOrderDetailsQuery, useGetRestaurantDetailsQuery, } from "redux/api/others"; import { useAppSelector } from "redux/hooks"; import Stepper from "./components/Stepper"; import styles from "./order.module.css"; import BackIcon from "components/Icons/BackIcon"; import NextIcon from "components/Icons/NextIcon"; import { RateBottomSheet } from "components/CustomBottomSheet/RateBottomSheet"; export default function OrderPage() { const { t } = useTranslation(); const { orderId } = useParams(); const { isRTL } = useAppSelector((state) => state.locale); const { restaurant } = useAppSelector((state) => state.order); const navigate = useNavigate(); const hasRefetchedRef = useRef(false); const { data: orderDetails } = useGetOrderDetailsQuery( { orderID: orderId || "", restaurantID: localStorage.getItem("restaurantID") || "", }, { skip: !orderId, // return it t0 60000 after finish testing pollingInterval: 10000, refetchOnMountOrArgChange: true, }, ); // Get restaurant subdomain for refetching const restaurantSubdomain = restaurant?.subdomain; const { refetch: refetchRestaurantDetails } = useGetRestaurantDetailsQuery( restaurantSubdomain || "", { skip: !restaurantSubdomain, }, ); // Reset refetch flag when orderId changes useEffect(() => { hasRefetchedRef.current = false; }, [orderId]); // Refetch restaurant details when order status has alias "closed" useEffect(() => { if (orderDetails?.status && !hasRefetchedRef.current) { const hasClosedStatus = orderDetails.status.some( (status) => status?.alias === "closed", ); if (hasClosedStatus && restaurantSubdomain) { refetchRestaurantDetails(); hasRefetchedRef.current = true; } } }, [orderDetails?.status, restaurantSubdomain, refetchRestaurantDetails]); // Check if order is in progress (check last status alias) const lastStatus = orderDetails?.status?.[orderDetails.status.length - 1]; const isInProgress = lastStatus?.alias === "accepted_by_restaurant"; // Calculate timer and progress const [remainingSeconds, setRemainingSeconds] = useState(0); const [progressPercent, setProgressPercent] = useState(0); useEffect(() => { if ( !isInProgress || !orderDetails?.order?.time_to_prepare || !orderDetails?.order?.created_at ) { return; } const updateTimer = () => { const orderCreatedAt = dayjs(orderDetails.order.created_at); const now = dayjs(); const elapsedSeconds = now.diff(orderCreatedAt, "second"); // time_to_prepare is in minutes, convert to seconds const totalSeconds = (Number(orderDetails.order.time_to_prepare) || 0) * 60; const remaining = Math.max(0, totalSeconds - elapsedSeconds); setRemainingSeconds(remaining); const percent = totalSeconds > 0 ? Math.min(100, Math.max(0, (elapsedSeconds / totalSeconds) * 100)) : 0; setProgressPercent(percent); }; updateTimer(); const interval = setInterval(updateTimer, 1000); return () => clearInterval(interval); }, [ isInProgress, orderDetails?.order?.time_to_prepare, orderDetails?.status, ]); // Format remaining time as MM:SS const formatTimer = (totalSeconds: number): string => { const mins = Math.floor(totalSeconds / 60); const secs = Math.floor(totalSeconds % 60); return `${mins.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`; }; return ( <> {t("order.title")}
{t("order.yourOrderFromFascanoRestaurant")}
{" "} {isRTL ? orderDetails?.restaurantAR : orderDetails?.restaurant}
{isInProgress ? ( formatTimer(remainingSeconds)} // success={{ percent: progressPercent }} strokeColor="#FFB700" size={120} type="dashboard" /> ) : ( )}
{t("order.inProgressOrder")} (1)
#{orderDetails?.order.id} ordered :- Today -{" "} {dayjs(orderDetails?.status[0]?.pivot?.created_at).format( "h:mm A", )}
{t("order.yourOrderFrom")}
{" "} {t("order.muscat")}
} >
{orderDetails?.orderItems.map((item, index) => (
{item.name}
))}
navigate(`/${restaurant?.subdomain}`)} >
{isRTL ? restaurant?.nameAR : restaurant?.restautantName} {isRTL ? ( ) : ( )}
); }