import { useState } from "react"; import { useNavigate } from "@remix-run/react"; import { X, Plus, Check, Loader2, Layers, ChevronLeft } from "lucide-react"; import { Drawer, DrawerContent, DrawerHeader, DrawerTitle, } from "~/components/ui/drawer"; import { Button } from "~/components/ui/button"; import { Skeleton } from "~/components/ui/skeleton"; import { useUserCollections, useAddProductsToCollection, } from "../../requestHandler/use-user-collection-hooks"; import { CreateSetModal } from "./CreateSetModal"; import { SetSuccessModal } from "./SetSuccessModal"; import { UserCollection } from "src/api/types"; interface AddToSetDrawerProps { isOpen: boolean; onOpenChange: (open: boolean) => void; productId: string; } export const AddToSetDrawer = ({ isOpen, onOpenChange, productId, }: AddToSetDrawerProps) => { const navigate = useNavigate(); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [isSuccessModalOpen, setIsSuccessModalOpen] = useState(false); const [selectedSet, setSelectedSet] = useState(null); const [addingToSetId, setAddingToSetId] = useState(null); const { data: collections, isLoading } = useUserCollections(); const handleGoToSetDetail = (e: React.MouseEvent, collectionId: string) => { e.stopPropagation(); onOpenChange(false); navigate(`/profile/sets/${collectionId}`); }; const { mutate: addToCollection, isPending: isAddingToCollection } = useAddProductsToCollection(); const handleAddToSet = (collectionId: string) => { setAddingToSetId(collectionId); // Find the collection to show in success modal const collection = collections?.find((c) => c.id === collectionId); addToCollection( { collectionId, productIds: [productId] }, { onSuccess: () => { setAddingToSetId(null); onOpenChange(false); // Show success modal with the collection if (collection) { setSelectedSet(collection); setIsSuccessModalOpen(true); } }, onError: () => { setAddingToSetId(null); }, } ); }; const handleCreateSetSuccess = (newSet: UserCollection) => { setSelectedSet(newSet); setIsCreateModalOpen(false); onOpenChange(false); setIsSuccessModalOpen(true); }; const isProductInCollection = (collection: UserCollection) => { return collection.products?.includes(productId) || false; }; return ( <>
onOpenChange(false)} />
افزودن به ست

این محصول را به یکی از ست‌های خود اضافه کنید

{isLoading ? (
{[1, 2, 3].map((i) => ( ))}
) : collections && collections.length > 0 ? (
{collections.map((collection) => { const isInCollection = isProductInCollection(collection); const isAdding = addingToSetId === collection.id; return ( )} ); })}
) : (

هنوز ستی ایجاد نکرده‌اید

اولین ست خود را بسازید

)} {/* Create new set button */}
); };