'use client'; import { Button } from "@/components/ui/Button"; import { useTranslation } from "@/lib/hooks/useTranslation"; 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"; export interface BoxOption { id: string; title: string; price: number; image: string; } interface BuildBoxCustomizeClientWithTranslationProps { boxes: BoxOption[]; } export function BuildBoxCustomizeClientWithTranslation({ boxes }: BuildBoxCustomizeClientWithTranslationProps) { const { t, locale } = useTranslation(); const dispatch = useAppDispatch(); const [selectedBox, setSelectedBox] = useState(null); const boxItems = useAppSelector(selectBoxItems); // Učitaj odabranu kutiju iz postojećih item-a prilikom učitavanja stranice useEffect(() => { const existingBox = boxItems.find(item => item.variantId === 'box-container'); if (existingBox) { setSelectedBox(existingBox.id); } }, [boxItems]); const handleSelectBox = (box: BoxOption) => { // Ako je ista kutija već odabrana, ne radi ništa if (selectedBox === box.id) return; setSelectedBox(box.id); // Ukloni sve postojeće kutije iz košarice const existingBoxes = boxItems.filter(item => item.variantId === 'box-container'); existingBoxes.forEach(existingBox => { dispatch(removeFromBox({ id: existingBox.id, color: existingBox.color })); }); // Add box to the cart dispatch(addToBox({ id: box.id, name: box.title, price: box.price, image: box.image, quantity: 1, variantId: 'box-container', // Special ID to identify this as a box })); }; return (
{boxes.map((box) => { const isSelected = selectedBox === box.id; return (
handleSelectBox(box)} >
{box.title} {/* Oznaka za odabranu kutiju */} {isSelected && (
)} {/* Mobile button - always visible */}
{/* Desktop button - visible on hover */}

{box.title}

${box.price.toFixed(2)}

); })}
); }