'use client'; import { Button } from "@/components/ui/Button"; import { useAppDispatch, useAppSelector } from "@/lib/redux/hooks"; import { addToBox, removeFromBox, selectBoxItems } from "@/lib/redux/slices/boxSlice"; import Image from "next/image"; import { useEffect, useState } from "react"; // Box option interface export interface BoxOption { id: string; title: string; price: number; image: string; } interface BoxCustomizeProps { boxes: BoxOption[]; } export function BuildBoxCustomizeClient({ boxes }: BoxCustomizeProps) { const [selectedBoxId, setSelectedBoxId] = useState(null); const dispatch = useAppDispatch(); const items = useAppSelector(selectBoxItems); // Check if there's already a box in the items and set it as selected on load useEffect(() => { const existingBox = items.find(item => item.variantId === 'box-container'); if (existingBox) { setSelectedBoxId(existingBox.id); } }, [items]); // When a box is selected, replace any existing box with the new one const handleSelectBox = (box: BoxOption) => { // If the same box is already selected, do nothing if (selectedBoxId === box.id) return; // Set this box as selected setSelectedBoxId(box.id); // Remove any existing box containers first const existingBoxes = items.filter(item => item.variantId === 'box-container'); existingBoxes.forEach(existingBox => { dispatch(removeFromBox({ id: existingBox.id, color: existingBox.color })); }); // Add the new box dispatch(addToBox({ id: box.id, name: box.title, price: box.price, image: box.image, quantity: 1, variantId: 'box-container', // Mark this as a box container, not a product })); }; return (
{boxes.map((box) => ( handleSelectBox(box)} /> ))}
); } // Box option component function BoxOption({ box, isSelected, onSelect }: { box: BoxOption; isSelected: boolean; onSelect: () => void; }) { return (
{box.title} {/* Overlay with "Dodaj u box" button - always visible for selected, only on hover for others */}
{isSelected && (
)}

{box.title}

${box.price.toFixed(2)}
); }