feat: implement planting and harvest details forms with submission handling

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2025-03-13 20:11:24 +07:00
parent aab347c582
commit f7f64562ab
2 changed files with 175 additions and 18 deletions

View File

@ -1,34 +1,130 @@
"use client";
import { SetStateAction, useEffect, useState } from "react";
import PlantingDetailsForm from "./planting-detail-form"; import PlantingDetailsForm from "./planting-detail-form";
import HarvestDetailsForm from "./harvest-detail-form"; import HarvestDetailsForm from "./harvest-detail-form";
import { Separator } from "@/components/ui/separator"; import { Separator } from "@/components/ui/separator";
import GoogleMapWithDrawing from "@/components/google-map-with-drawing"; import GoogleMapWithDrawing from "@/components/google-map-with-drawing";
export default function SetupPage() { export default function SetupPage() {
type PlantingDetails = {
daysToEmerge: number;
plantSpacing: number;
rowSpacing: number;
plantingDepth: number;
averageHeight: number;
isPerennial: boolean;
autoCreateTasks: boolean;
startMethod?: string;
lightProfile?: string;
soilConditions?: string;
plantingDetails?: string;
pruningDetails?: string;
};
const [plantingDetails, setPlantingDetails] =
useState<PlantingDetails | null>(null);
const [harvestDetails, setHarvestDetails] = useState(null);
const [mapData, setMapData] = useState(null);
// handle planting details submission
const handlePlantingDetailsChange = (data: {
daysToEmerge: number;
plantSpacing: number;
rowSpacing: number;
plantingDepth: number;
averageHeight: number;
isPerennial: boolean;
autoCreateTasks: boolean;
startMethod?: string;
lightProfile?: string;
soilConditions?: string;
plantingDetails?: string;
pruningDetails?: string;
}) => {
setPlantingDetails(data);
};
// handle harvest details submission
const handleHarvestDetailsChange = (data: SetStateAction<null>) => {
setHarvestDetails(data);
};
// handle map area selection
const handleMapDataChange = (data: SetStateAction<null>) => {
setMapData(data);
};
useEffect(() => {
console.log(plantingDetails);
}, [plantingDetails]);
const handleSubmit = () => {
if (!plantingDetails || !harvestDetails || !mapData) {
alert("Please complete all sections before submitting.");
return;
}
const formData = {
plantingDetails,
harvestDetails,
mapData,
};
console.log("Form data to be submitted:", formData);
fetch("/api/submit", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
})
.then((response) => response.json())
.then((data) => {
console.log("Response from backend:", data);
})
.catch((error) => {
console.error("Error submitting form:", error);
});
};
return ( return (
<div className="p-5"> <div className="p-5">
{/* Planting Details Section */}
<div className="flex justify-center"> <div className="flex justify-center">
<h1 className="flex text-2xl ">Plating Details</h1> <h1 className="text-2xl">Planting Details</h1>
</div> </div>
<Separator className="mt-3" /> <Separator className="mt-3" />
<div className="mt-10 flex justify-center"> <div className="mt-10 flex justify-center">
<PlantingDetailsForm /> <PlantingDetailsForm onChange={handlePlantingDetailsChange} />
</div> </div>
{/* Harvest Details Section */}
<div className="flex justify-center mt-20"> <div className="flex justify-center mt-20">
<h1 className="flex text-2xl ">Harvest Details</h1> <h1 className="text-2xl">Harvest Details</h1>
</div> </div>
<Separator className="mt-3" /> <Separator className="mt-3" />
<div className="mt-10 flex justify-center"> <div className="mt-10 flex justify-center">
<HarvestDetailsForm /> {/* <HarvestDetailsForm onChange={handleHarvestDetailsChange} /> */}
</div> </div>
{/* Map Section */}
<div className="mt-10"> <div className="mt-10">
<div className="flex justify-center mt-20"> <div className="flex justify-center mt-20">
<h1 className="flex text-2xl ">Map</h1> <h1 className="text-2xl">Map</h1>
</div> </div>
<Separator className="mt-3" /> <Separator className="mt-3" />
<div className="mt-10"> <div className="mt-10">
<GoogleMapWithDrawing /> {/* <GoogleMapWithDrawing onAreaSelected={handleMapDataChange} /> */}
</div> </div>
</div> </div>
{/* Submit Button */}
<div className="mt-10 flex justify-center">
<button onClick={handleSubmit} className="btn btn-primary">
Submit All Data
</button>
</div>
</div> </div>
); );
} }

View File

@ -23,11 +23,30 @@ import {
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { Switch } from "@/components/ui/switch"; import { Switch } from "@/components/ui/switch";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useState } from "react";
type plantingSchema = z.infer<typeof plantingDetailsFormSchema>; type plantingSchema = z.infer<typeof plantingDetailsFormSchema>;
interface FormData {
daysToEmerge: number;
plantSpacing: number;
rowSpacing: number;
plantingDepth: number;
averageHeight: number;
startMethod?: string;
lightProfile?: string;
soilConditions?: string;
plantingDetails?: string;
pruningDetails?: string;
isPerennial: boolean;
autoCreateTasks: boolean;
}
export default function PlantingDetailsForm() { export default function PlantingDetailsForm({
const form = useForm<plantingSchema>({ onChange,
}: {
onChange: (data: plantingSchema) => void;
}) {
const form = useForm({
resolver: zodResolver(plantingDetailsFormSchema), resolver: zodResolver(plantingDetailsFormSchema),
defaultValues: { defaultValues: {
daysToEmerge: 0, daysToEmerge: 0,
@ -44,9 +63,16 @@ export default function PlantingDetailsForm() {
autoCreateTasks: false, autoCreateTasks: false,
}, },
}); });
const onSubmit = (data: FormData) => {
onChange(data);
};
return ( return (
<Form {...form}> <Form {...form}>
<form className="grid grid-cols-3 gap-5"> <form
className="grid grid-cols-3 gap-5"
onSubmit={form.handleSubmit(onSubmit)}
>
<FormField <FormField
control={form.control} control={form.control}
name="daysToEmerge" name="daysToEmerge"
@ -61,6 +87,13 @@ export default function PlantingDetailsForm() {
id="daysToEmerge" id="daysToEmerge"
className="w-96" className="w-96"
{...field} {...field}
onChange={(e) => {
// convert to number
const value = e.target.value
? parseInt(e.target.value, 10)
: "";
field.onChange(value);
}}
/> />
</div> </div>
</div> </div>
@ -83,6 +116,13 @@ export default function PlantingDetailsForm() {
id="plantSpacing" id="plantSpacing"
className="w-96" className="w-96"
{...field} {...field}
onChange={(e) => {
// convert to number
const value = e.target.value
? parseInt(e.target.value, 10)
: "";
field.onChange(value);
}}
/> />
</div> </div>
</div> </div>
@ -105,6 +145,13 @@ export default function PlantingDetailsForm() {
id="rowSpacing" id="rowSpacing"
className="w-96" className="w-96"
{...field} {...field}
onChange={(e) => {
// convert to number
const value = e.target.value
? parseInt(e.target.value, 10)
: "";
field.onChange(value);
}}
/> />
</div> </div>
</div> </div>
@ -129,6 +176,13 @@ export default function PlantingDetailsForm() {
id="plantingDepth" id="plantingDepth"
className="w-96" className="w-96"
{...field} {...field}
onChange={(e) => {
// convert to number
const value = e.target.value
? parseInt(e.target.value, 10)
: "";
field.onChange(value);
}}
/> />
</div> </div>
</div> </div>
@ -153,6 +207,13 @@ export default function PlantingDetailsForm() {
id="averageHeight" id="averageHeight"
className="w-96" className="w-96"
{...field} {...field}
onChange={(e) => {
// convert to number
const value = e.target.value
? parseInt(e.target.value, 10)
: "";
field.onChange(value);
}}
/> />
</div> </div>
</div> </div>
@ -201,9 +262,9 @@ export default function PlantingDetailsForm() {
<SelectValue placeholder="Select light profile" /> <SelectValue placeholder="Select light profile" />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem value="xp">Seed</SelectItem> <SelectItem value="Seed">Seed</SelectItem>
<SelectItem value="xa">Transplant</SelectItem> <SelectItem value="Transplant">Transplant</SelectItem>
<SelectItem value="xz">Cutting</SelectItem> <SelectItem value="Cutting">Cutting</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
</FormControl> </FormControl>
@ -228,9 +289,9 @@ export default function PlantingDetailsForm() {
<SelectValue placeholder="Select a soil condition" /> <SelectValue placeholder="Select a soil condition" />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem value="xp">Seed</SelectItem> <SelectItem value="Seed">Seed</SelectItem>
<SelectItem value="xa">Transplant</SelectItem> <SelectItem value="Transplant">Transplant</SelectItem>
<SelectItem value="xz">Cutting</SelectItem> <SelectItem value="Cutting">Cutting</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
</FormControl> </FormControl>