import { ShoppingCart, X, Check } from "lucide-react";
import { Button } from "~/components/ui/button";
import discountRed from "~/assets/icons/product/discount-red.svg";
import { useState } from "react";
import { useToast } from "~/hooks/use-toast";
import {
Drawer,
DrawerContent,
DrawerHeader,
DrawerTitle,
} from "~/components/ui/drawer";
import { Link } from "@remix-run/react";
import { useAddToCart } from "../../requestHandler/use-cart-hooks";
import { findVariantId, formatPrice } from "../../utils/helpers";
import { ProductDetail } from "src/api/types";
import { useRootData } from "~/hooks/use-root-data";
import { LoginRequiredDialog } from "../LoginRequiredDialog";
interface BottomBarProps {
product: ProductDetail;
selectedColor: string | null;
selectedSize: string | null;
currentPrice: string;
currentDiscountPrice: string | null;
currentStock: number | null;
}
interface PriceDisplayProps {
currentPrice: string;
currentDiscountPrice: string | null;
}
const PriceDisplay = ({
currentPrice,
currentDiscountPrice,
}: PriceDisplayProps) => {
return (
{currentDiscountPrice && (
{formatPrice(currentPrice)}
)}
{formatPrice(currentDiscountPrice || currentPrice)}
تومان
);
};
interface AddToCartButtonProps {
isOutOfStock: boolean;
onClick: () => void;
}
const AddToCartButton = ({ isOutOfStock, onClick }: AddToCartButtonProps) => (
);
interface CartDrawerProps {
isOpen: boolean;
onOpenChange: (open: boolean) => void;
}
const CartDrawer = ({ isOpen, onOpenChange }: CartDrawerProps) => (
onOpenChange(false)}
/>
سبد خرید
محصول به سبد خرید اضافه شد!
);
export const BottomBar = ({
product,
selectedColor,
selectedSize,
currentPrice,
currentDiscountPrice,
currentStock,
}: BottomBarProps) => {
const { toast } = useToast();
const { user } = useRootData();
const [isCartDrawerOpen, setIsCartDrawerOpen] = useState(false);
const [isLoginModalOpen, setIsLoginModalOpen] = useState(false);
// Use the addToCart mutation hook
const addToCartMutation = useAddToCart();
const isOutOfStock = currentStock === null || currentStock <= 0;
const handleAddToCart = async () => {
// Check if user is logged in
if (!user?.access_token) {
setIsLoginModalOpen(true);
return;
}
if (currentStock !== null && currentStock > 0) {
// Find the variant ID based on selected color and size
const variantId = findVariantId(product, selectedColor, selectedSize);
if (!variantId) {
toast({
title: "خطا",
description: "محصول با ویژگیهای انتخابی شما یافت نشد",
variant: "destructive",
});
return;
}
try {
// Add to cart using React Query mutation
await addToCartMutation.mutateAsync({
variantId,
quantity: 1,
});
toast({
title: "سبد خرید",
description: "محصول با موفقیت به سبد خرید اضافه شد",
variant: "default",
});
setIsCartDrawerOpen(true);
} catch (error) {
console.error("Error adding to cart:", error);
toast({
title: "خطا",
description: "افزودن محصول به سبد خرید با مشکل مواجه شد",
variant: "destructive",
});
}
}
};
return (
<>
>
);
};