import Link from 'next/link'; import { redirect } from 'next/navigation'; import { getCurrentCustomer } from '@/lib/auth'; import { prisma } from '@/lib/prisma'; import { logoutCustomer } from './actions'; import AddressFormSection from '@/components/AddressFormSection'; import OlderOrders from '@/components/OlderOrders'; function formatPrice(cents: number) { return `£${(cents / 100).toFixed(2)}`; } function statusLabel(status: string, trackingNumber?: string | null, collectedByCustomer?: boolean) { if (status === 'DELIVERED') return collectedByCustomer ? 'Collected' : 'Delivered'; if (status === 'PAID') return trackingNumber ? 'Dispatched' : 'Awaiting dispatch'; if (status === 'FAILED') return 'Failed'; if (status === 'PENDING') return 'Waiting payment'; return 'Processing'; } function statusClasses(status: string, trackingNumber?: string | null) { if (status === 'DELIVERED') return 'bg-green-500/10 text-green-600'; if (status === 'PAID') return trackingNumber ? 'bg-splash-blue/10 text-splash-blue' : 'bg-splash-orange/10 text-splash-orange'; if (status === 'FAILED') return 'bg-splash-pink/10 text-splash-pink'; return 'bg-splash-orange/10 text-splash-orange'; } const SUCCESS_MESSAGES: Record = { password_changed: 'Password updated successfully.', }; export default async function AccountPage({ searchParams }: { searchParams: { success?: string } }) { const customer = await getCurrentCustomer(); if (!customer) redirect('/account/login'); const success = searchParams.success ? SUCCESS_MESSAGES[searchParams.success] : null; const orders = await prisma.order.findMany({ where: { customerId: customer.id }, orderBy: { createdAt: 'desc' }, include: { items: true }, }); const designReviews = await prisma.designApproval.findMany({ where: { customerEmail: customer.email, status: 'IN_REVIEW' }, include: { product: true }, orderBy: { createdAt: 'desc' }, }); const approvedDesigns = await prisma.designApproval.findMany({ where: { customerEmail: customer.email, status: 'APPROVED' }, include: { product: true }, orderBy: { createdAt: 'desc' }, }); // Separate recent (< 7 days) and older (>= 7 days) orders const now = new Date(); const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); const recentOrders = orders.filter(order => new Date(order.createdAt) > sevenDaysAgo); const olderOrders = orders.filter(order => new Date(order.createdAt) <= sevenDaysAgo); return (
✓ Welcome back{customer.name ? `, ${customer.name}` : ''}!
{success && (
✓ {success}
)}

My account

{customer.name ? `${customer.name} · ` : ''} {customer.email}

Change password

Design reviews

{designReviews.length === 0 ? (

No designs awaiting review.

) : (
{designReviews.map((design) => ( design preview

{design.product.name}

{new Date(design.createdAt).toLocaleDateString(undefined, { dateStyle: 'medium' })}

Review → ))}
)}

Ready to order

{approvedDesigns.length === 0 ? (

No approved designs ready to order.

) : (
{approvedDesigns.map((design) => ( design preview

{design.product.name}

Approved {new Date(design.createdAt).toLocaleDateString(undefined, { dateStyle: 'medium' })}

{design.expectedDeliveryDate && (

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

)}
Order → ))}
)}

Order history

{orders.length === 0 ? (

No orders yet.

) : ( <> {recentOrders.length > 0 && (
{recentOrders.map((order) => (

{new Date(order.createdAt).toLocaleDateString(undefined, { dateStyle: 'medium' })} ·{' '} {order.items.length} item{order.items.length === 1 ? '' : 's'}

{statusLabel(order.status, order.trackingNumber, order.collectedByCustomer)}
{formatPrice(order.total)} {/* Collection Ready Notification */} {order.isCollectionPickup && order.readyForCollection && (

✓ Your order is ready for collection

Please come collect your order at your earliest convenience

)} {/* Tracking Information */} {order.carrier && order.trackingNumber && (

Tracking: {order.carrier} · {order.trackingNumber}

{order.estimatedDeliveryDate && (

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

)}
)}
))}
)} {olderOrders.length > 0 && ( )} )}
); }