'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import { useCart } from '@/lib/cartStore'; interface DesignApproval { id: string; customerEmail: string; customerName: string | null; productId: string; product: { id: string; name: string; basePrice: number; personalisedPrice: number; effectivePrice: number; }; designJson: string; designPreviewUrl: string; placementPreviewUrl: string | null; color: string; size: string | null; status: string; } export default function DesignReviewPage({ params }: { params: { id: string } }) { const [design, setDesign] = useState(null); const [loading, setLoading] = useState(true); const [approving, setApproving] = useState(false); const [changingMessage, setChangingMessage] = useState(''); const [sendingChanges, setSendingChanges] = useState(false); const addItem = useCart((s) => s.addItem); useEffect(() => { fetch(`/api/designs/${params.id}`) .then((r) => r.json()) .then(setDesign) .finally(() => setLoading(false)); }, [params.id]); const handleApprove = async () => { if (!design) return; setApproving(true); try { const res = await fetch(`/api/designs/${params.id}/approve`, { method: 'POST' }); if (res.ok) { const updated = await res.json(); setDesign(updated); // Add to cart addItem({ productId: design.productId, color: design.color, size: design.size, designJson: design.designJson, quantity: 1, }); alert('Design approved! Added to your bag.'); } } finally { setApproving(false); } }; const handleRequestChanges = async () => { if (!changingMessage.trim()) { alert('Please provide feedback for the design changes'); return; } setSendingChanges(true); try { const res = await fetch(`/api/designs/${params.id}/messages`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: changingMessage, role: 'CUSTOMER', messageType: 'CHANGE_REQUEST', }), }); if (res.ok) { // Update status back to PENDING so admin can see it await fetch(`/api/designs/${params.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ status: 'PENDING' }), }); alert('Changes requested! The admin has been notified.'); setChangingMessage(''); // Reload design const updated = await fetch(`/api/designs/${params.id}`).then((r) => r.json()); setDesign(updated); } } finally { setSendingChanges(false); } }; if (loading) return
Loading...
; if (!design) return
Design not found.
; return (
← Back to shop

{design.product.name}

Review your personalized design

{/* Design Preview */}

Design preview

design preview
{design.placementPreviewUrl && (

Design on product

placement preview
)}
{/* Actions */}

Order Summary

Product: {design.product.name}

Color: {design.color}

{design.size && (

Size: {design.size}

)}

Price: £{(design.product.effectivePrice / 100).toFixed(2)}

{/* Request Changes */}

Request Changes