'use client'; import { useState } from 'react'; import { saveShippingRates, deleteShippingRate } from '@/app/admin/shipping-rates/actions'; interface ShippingRateType { id: string; service: string; basePrice: number; pricePerKg: number; estimatedDays: number; isInternational: boolean; } interface Props { existingRates: ShippingRateType[]; } export default function ShippingRatesForm({ existingRates }: Props) { const [rates, setRates] = useState(existingRates); const [newRate, setNewRate] = useState({ service: '', basePrice: 0, pricePerKg: 0, estimatedDays: 1, isInternational: false, }); const handleAddRate = () => { if (!newRate.service.trim()) { alert('Please enter a service name'); return; } const rate: ShippingRateType = { id: `new-${Date.now()}`, ...newRate, }; setRates([...rates, rate]); setNewRate({ service: '', basePrice: 0, pricePerKg: 0, estimatedDays: 1, isInternational: false, }); }; const handleUpdateRate = (id: string, field: string, value: string | number | boolean) => { setRates( rates.map((rate) => rate.id === id ? { ...rate, [field]: value } : rate ) ); }; const handleRemoveRate = (id: string) => { if (id.startsWith('new-')) { setRates(rates.filter((r) => r.id !== id)); } else { // Delete from database deleteShippingRate(id); } }; return (

Current Rates

{rates.length === 0 ? (

No shipping rates configured yet.

) : (
{rates.map((rate) => ( ))}
Service Base Price Price/kg Est. Days Type Action
handleUpdateRate(rate.id, 'service', e.target.value)} className="w-full border border-line bg-paper px-2 py-1 text-sm" />
£ handleUpdateRate( rate.id, 'basePrice', Math.round(parseFloat(e.target.value) * 100) ) } className="w-24 border border-line bg-paper px-2 py-1 text-sm" />
£ handleUpdateRate( rate.id, 'pricePerKg', Math.round(parseFloat(e.target.value) * 100) ) } className="w-24 border border-line bg-paper px-2 py-1 text-sm" />
handleUpdateRate(rate.id, 'estimatedDays', parseInt(e.target.value))} className="w-20 border border-line bg-paper px-2 py-1 text-sm" />
)}

Add New Rate

setNewRate({ ...newRate, service: e.target.value })} placeholder="e.g. Special Delivery" className="w-full border border-line bg-paper px-2 py-1 text-sm" />
setNewRate({ ...newRate, basePrice: Math.round(parseFloat(e.target.value) * 100) }) } className="w-full border border-line bg-paper px-2 py-1 text-sm" />
setNewRate({ ...newRate, pricePerKg: Math.round(parseFloat(e.target.value) * 100) }) } className="w-full border border-line bg-paper px-2 py-1 text-sm" />
setNewRate({ ...newRate, estimatedDays: parseInt(e.target.value) })} className="w-full border border-line bg-paper px-2 py-1 text-sm" />
); }