'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 | null; effectivePrice: number; }; designJson: string; designPreviewUrl: string; placementPreviewUrl: string | null; color: string; size: string | null; status: string; expectedDeliveryDate: string | null; } 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', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}), }); if (res.ok) { const updated = await res.json(); setDesign(updated); // Add to cart addItem({ cartItemId: crypto.randomUUID?.() || Math.random().toString(36).substr(2, 9), productId: design.productId, slug: design.product.id, // Using product ID as slug for now name: design.product.name, mockup: design.product.id, color: design.color, size: design.size, quantity: 1, unitPrice: design.product.personalisedPrice ?? design.product.effectivePrice, designJson: design.designJson, designPreviewUrl: design.designPreviewUrl, designPreviewUrlBack: design.designPreviewUrlBack, placementPreviewUrl: design.placementPreviewUrl, placementPreviewUrlBack: design.placementPreviewUrlBack, designWidthCm: null, designHeightCm: null, }); alert('Design approved! Added to your bag.'); } else { alert('Failed to approve design. Please try again.'); } } catch (error) { console.error('Error approving design:', error); alert('An error occurred. Please try again.'); } 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.personalisedPrice ?? design.product.effectivePrice) / 100).toFixed(2)}

{design.expectedDeliveryDate && (

Est. delivery: {new Date(design.expectedDeliveryDate).toLocaleDateString(undefined, { dateStyle: 'medium' })}

)}
{/* Request Changes */}

Request Changes