import { useState } from 'react'; import Link from 'next/link'; import Head from 'next/head'; import { useRouter } from 'next/router'; const categories = [ { value: 'seeds', label: 'Seeds', icon: '🌰' }, { value: 'seedlings', label: 'Seedlings', icon: '🌱' }, { value: 'mature_plants', label: 'Mature Plants', icon: '🪴' }, { value: 'cuttings', label: 'Cuttings', icon: '✂️' }, { value: 'produce', label: 'Produce', icon: '🥬' }, { value: 'supplies', label: 'Supplies', icon: '🧰' }, ]; export default function CreateListingPage() { const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [formData, setFormData] = useState({ title: '', description: '', price: '', quantity: '1', category: '', tags: '', city: '', region: '', }); const handleChange = ( e: React.ChangeEvent ) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { const response = await fetch('/api/marketplace/listings', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-user-id': 'demo-user', 'x-user-name': 'Demo User', }, body: JSON.stringify({ title: formData.title, description: formData.description, price: parseFloat(formData.price), quantity: parseInt(formData.quantity, 10), category: formData.category, tags: formData.tags.split(',').map((t) => t.trim()).filter(Boolean), location: formData.city || formData.region ? { city: formData.city, region: formData.region, } : undefined, }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to create listing'); } // Redirect to the listing page router.push(`/marketplace/listings/${data.listing.id}`); } catch (err) { setError(err instanceof Error ? err.message : 'Something went wrong'); } finally { setLoading(false); } }; return (
Create Listing - LocalGreenChain Marketplace {/* Header */}
LocalGreenChain
{/* Main Content */}

Create New Listing

Fill in the details below to list your item on the marketplace.

{error && (
{error}
)}
{/* Title */}
{/* Description */}