import { useState } from 'react'; import Link from 'next/link'; import Head from 'next/head'; import { useRouter } from 'next/router'; export default function RegisterPlant() { const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); const [formData, setFormData] = useState({ id: `plant-${Date.now()}`, commonName: '', scientificName: '', species: '', genus: '', family: '', propagationType: 'original' as const, plantedDate: new Date().toISOString().split('T')[0], status: 'sprouted' as const, latitude: '', longitude: '', address: '', city: '', country: '', ownerName: '', ownerEmail: '', notes: '', }); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { // Prepare plant data const plantData = { id: formData.id, commonName: formData.commonName, scientificName: formData.scientificName || undefined, species: formData.species || undefined, genus: formData.genus || undefined, family: formData.family || undefined, propagationType: formData.propagationType, generation: 0, plantedDate: formData.plantedDate, status: formData.status, location: { latitude: parseFloat(formData.latitude), longitude: parseFloat(formData.longitude), address: formData.address || undefined, city: formData.city || undefined, country: formData.country || undefined, }, owner: { id: `user-${Date.now()}`, name: formData.ownerName, email: formData.ownerEmail, }, childPlants: [], notes: formData.notes || undefined, registeredAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }; const response = await fetch('/api/plants/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(plantData), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to register plant'); } setSuccess(true); setTimeout(() => { router.push(`/plants/${data.plant.id}`); }, 2000); } catch (err: any) { setError(err.message || 'An error occurred'); } finally { setLoading(false); } }; const handleChange = ( e: React.ChangeEvent< HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement > ) => { setFormData({ ...formData, [e.target.name]: e.target.value, }); }; const getCurrentLocation = () => { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( (position) => { setFormData({ ...formData, latitude: position.coords.latitude.toString(), longitude: position.coords.longitude.toString(), }); }, (error) => { setError('Unable to get your location: ' + error.message); } ); } else { setError('Geolocation is not supported by your browser'); } }; return (
Register Plant - LocalGreenChain {/* Header */}
🌱 LocalGreenChain
{/* Main Content */}

Register a New Plant

Add your plant to the blockchain and start tracking its lineage.

{error && (
{error}
)} {success && (
Plant registered successfully! Redirecting to plant page...
)}
{/* Plant Information */}

Plant Information

{/* Location */}

Location

{/* Owner Information */}

Your Information

{/* Notes */}