import { useState } from "react"; const initialCart = [ { id: 1, name: "DAP Fertilizer", variant: "50kg Bag", price: 3850, qty: 2, unit: "bag", emoji: "๐ŸŒฑ" }, { id: 2, name: "CAN Fertilizer", variant: "50kg Bag", price: 3200, qty: 1, unit: "bag", emoji: "๐Ÿงช" }, { id: 3, name: "H614D Maize Seeds", variant: "10kg Packet", price: 1450, qty: 3, unit: "packet", emoji: "๐ŸŒฝ" }, { id: 4, name: "Roundup Herbicide", variant: "1 Litre", price: 890, qty: 1, unit: "bottle", emoji: "๐Ÿชด" }, ]; const NANDI_SUBCOUNTIES = [ "Aldai", "Chesumei", "Emgwen", "Mosop", "Nandi Hills", "Tindiret" ]; const formatKES = (n) => `KES ${Number(n).toLocaleString("en-KE", { minimumFractionDigits: 0 })}`; const DELIVERY_FEE = 350; export default function FarmCart() { const [page, setPage] = useState("cart"); const [cart, setCart] = useState(initialCart); const [form, setForm] = useState({ fullName: "", phone: "", location: "", delivery: "pickup", instructions: "" }); const [errors, setErrors] = useState({}); const [submitted, setSubmitted] = useState(false); const updateQty = (id, delta) => { setCart(c => c.map(item => item.id === id ? { ...item, qty: Math.max(1, item.qty + delta) } : item )); }; const removeItem = (id) => setCart(c => c.filter(item => item.id !== id)); const subtotal = cart.reduce((s, i) => s + i.price * i.qty, 0); const deliveryFee = form.delivery === "deliver" ? DELIVERY_FEE : 0; const total = subtotal + deliveryFee; const validate = () => { const e = {}; if (!form.fullName.trim()) e.fullName = "Full name is required"; if (!form.phone.trim()) e.phone = "Phone number is required"; else if (!/^(\+254|0)[17]\d{8}$/.test(form.phone.replace(/\s/g, ""))) e.phone = "Enter a valid Kenyan phone number (e.g. 0712345678)"; if (!form.location) e.location = "Please select a delivery location"; return e; }; const handleSubmit = () => { const e = validate(); setErrors(e); if (Object.keys(e).length === 0) setSubmitted(true); }; const setField = (k, v) => { setForm(f => ({ ...f, [k]: v })); if (errors[k]) setErrors(e => ({ ...e, [k]: undefined })); }; if (submitted) { return (
โœ“

Order Placed!

Thank you, {form.fullName}. We'll send payment instructions to{" "} {form.phone} via M-Pesa shortly.

Order Total{formatKES(total)}
Delivery{form.delivery === "pickup" ? "Pick Up" : `Deliver to ${form.location}`}
); } if (page === "checkout") { return (
๐ŸŒพ AgroNandi Inputs

Checkout

{/* LEFT: Form */}

Delivery Details

setField("fullName", e.target.value)} /> {errors.fullName &&

{errors.fullName}

}
setField("phone", e.target.value)} type="tel" />

Used to send M-Pesa payment prompt

{errors.phone &&

{errors.phone}

}
{errors.location &&

{errors.location}

}
{["pickup", "deliver"].map(opt => ( ))}