mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-18 21:44:08 +01:00
feat: streamline setup page with step navigation and enhance map area selection
This commit is contained in:
parent
3a27d48f21
commit
e7c8b64e24
@ -1,123 +1,118 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { SetStateAction, useEffect, useState } from "react";
|
import { 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 GoogleMapWithDrawing from "@/components/google-map-with-drawing";
|
import GoogleMapWithDrawing from "@/components/google-map-with-drawing";
|
||||||
|
import { Separator } from "@/components/ui/separator";
|
||||||
import {
|
import {
|
||||||
plantingDetailsFormSchema,
|
plantingDetailsFormSchema,
|
||||||
harvestDetailsFormSchema,
|
harvestDetailsFormSchema,
|
||||||
} from "@/schemas/application.schema";
|
} from "@/schemas/application.schema";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
type plantingSchema = z.infer<typeof plantingDetailsFormSchema>;
|
type PlantingSchema = z.infer<typeof plantingDetailsFormSchema>;
|
||||||
type harvestSchema = z.infer<typeof harvestDetailsFormSchema>;
|
type HarvestSchema = z.infer<typeof harvestDetailsFormSchema>;
|
||||||
|
|
||||||
|
const steps = [
|
||||||
|
{ title: "Step 1", description: "Planting Details" },
|
||||||
|
{ title: "Step 2", description: "Harvest Details" },
|
||||||
|
{ title: "Step 3", description: "Select Map Area" },
|
||||||
|
];
|
||||||
|
|
||||||
export default function SetupPage() {
|
export default function SetupPage() {
|
||||||
const [plantingDetails, setPlantingDetails] = useState<plantingSchema | null>(
|
const [step, setStep] = useState(1);
|
||||||
|
const [plantingDetails, setPlantingDetails] = useState<PlantingSchema | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
const [harvestDetails, setHarvestDetails] = useState<harvestSchema | null>(
|
const [harvestDetails, setHarvestDetails] = useState<HarvestSchema | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
const [mapData, setMapData] = useState<{ lat: number; lng: number }[] | null>(
|
const [mapData, setMapData] = useState<{ lat: number; lng: number }[] | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
// handle planting details submission
|
const handleNext = () => {
|
||||||
const handlePlantingDetailsChange = (data: plantingSchema) => {
|
if (step === 1 && !plantingDetails) {
|
||||||
setPlantingDetails(data);
|
alert("Please complete the Planting Details before proceeding.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (step === 2 && !harvestDetails) {
|
||||||
|
alert("Please complete the Harvest Details before proceeding.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setStep((prev) => prev + 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
// handle harvest details submission
|
const handleBack = () => {
|
||||||
const handleHarvestDetailsChange = (data: harvestSchema) => {
|
setStep((prev) => prev - 1);
|
||||||
setHarvestDetails(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// handle map area selection
|
|
||||||
const handleMapDataChange = (data: { lat: number; lng: number }[]) => {
|
|
||||||
setMapData((prevMapData) => {
|
|
||||||
if (prevMapData) {
|
|
||||||
return [...prevMapData, ...data];
|
|
||||||
} else {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// log the changes
|
|
||||||
useEffect(() => {
|
|
||||||
// console.log(plantingDetails);
|
|
||||||
// console.log(harvestDetails);
|
|
||||||
console.table(mapData);
|
|
||||||
}, [plantingDetails, harvestDetails, mapData]);
|
|
||||||
|
|
||||||
const handleSubmit = () => {
|
const handleSubmit = () => {
|
||||||
if (!plantingDetails || !harvestDetails || !mapData) {
|
if (!mapData) {
|
||||||
alert("Please complete all sections before submitting.");
|
alert("Please select an area on the map before submitting.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formData = {
|
console.log("Submitting:", { plantingDetails, harvestDetails, mapData });
|
||||||
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 */}
|
{/* Stepper Navigation */}
|
||||||
<div className="flex justify-center">
|
<div className="flex justify-between items-center mb-5">
|
||||||
<h1 className="text-2xl">Planting Details</h1>
|
{steps.map((item, index) => (
|
||||||
</div>
|
<div key={index} className="flex flex-col items-center">
|
||||||
<Separator className="mt-3" />
|
<div
|
||||||
<div className="mt-10 flex justify-center">
|
className={`w-10 h-10 flex items-center justify-center rounded-full text-white font-bold ${
|
||||||
<PlantingDetailsForm onChange={handlePlantingDetailsChange} />
|
step === index + 1 ? "bg-blue-500" : "bg-gray-500"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{index + 1}
|
||||||
|
</div>
|
||||||
|
<span className="font-medium mt-2">{item.title}</span>
|
||||||
|
<span className="text-gray-500 text-sm">{item.description}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Harvest Details Section */}
|
<Separator className="mb-5" />
|
||||||
<div className="flex justify-center mt-20">
|
|
||||||
<h1 className="text-2xl">Harvest Details</h1>
|
|
||||||
</div>
|
|
||||||
<Separator className="mt-3" />
|
|
||||||
<div className="mt-10 flex justify-center">
|
|
||||||
<HarvestDetailsForm onChange={handleHarvestDetailsChange} />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Map Section */}
|
{step === 1 && (
|
||||||
<div className="mt-10">
|
<>
|
||||||
<div className="flex justify-center mt-20">
|
<h2 className="text-xl text-center">Planting Details</h2>
|
||||||
<h1 className="text-2xl">Map</h1>
|
<PlantingDetailsForm onChange={setPlantingDetails} />
|
||||||
</div>
|
</>
|
||||||
<Separator className="mt-3" />
|
)}
|
||||||
<div className="mt-10">
|
|
||||||
<GoogleMapWithDrawing onAreaSelected={handleMapDataChange} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Submit Button */}
|
{step === 2 && (
|
||||||
<div className="mt-10 flex justify-center">
|
<>
|
||||||
<button onClick={handleSubmit} className="btn btn-primary">
|
<h2 className="text-xl text-center">Harvest Details</h2>
|
||||||
Submit All Data
|
<HarvestDetailsForm onChange={setHarvestDetails} />
|
||||||
</button>
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{step === 3 && (
|
||||||
|
<>
|
||||||
|
<h2 className="text-xl text-center">Select Area on Map</h2>
|
||||||
|
<GoogleMapWithDrawing onAreaSelected={setMapData} />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="mt-10 flex justify-between">
|
||||||
|
{step > 1 && (
|
||||||
|
<button onClick={handleBack} className="btn btn-secondary">
|
||||||
|
Back
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{step < 3 ? (
|
||||||
|
<button onClick={handleNext} className="btn btn-primary">
|
||||||
|
Next
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button onClick={handleSubmit} className="btn btn-success">
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -21,42 +21,7 @@ const GoogleMapWithDrawing = ({
|
|||||||
(overlay: google.maps.drawing.OverlayCompleteEvent) => {
|
(overlay: google.maps.drawing.OverlayCompleteEvent) => {
|
||||||
const shape = overlay.overlay;
|
const shape = overlay.overlay;
|
||||||
|
|
||||||
// check the shape of the drawing and extract lat/lng values
|
if (shape instanceof google.maps.Polyline) {
|
||||||
if (shape instanceof google.maps.Polygon) {
|
|
||||||
const path = shape.getPath();
|
|
||||||
const coordinates = path.getArray().map((latLng) => ({
|
|
||||||
lat: latLng.lat(),
|
|
||||||
lng: latLng.lng(),
|
|
||||||
}));
|
|
||||||
console.log("Polygon coordinates:", coordinates);
|
|
||||||
onAreaSelected(coordinates);
|
|
||||||
} else if (shape instanceof google.maps.Rectangle) {
|
|
||||||
const bounds = shape.getBounds();
|
|
||||||
if (bounds) {
|
|
||||||
const northEast = bounds.getNorthEast();
|
|
||||||
const southWest = bounds.getSouthWest();
|
|
||||||
const coordinates = [
|
|
||||||
{ lat: northEast.lat(), lng: northEast.lng() },
|
|
||||||
{ lat: southWest.lat(), lng: southWest.lng() },
|
|
||||||
];
|
|
||||||
console.log("Rectangle coordinates:", coordinates);
|
|
||||||
onAreaSelected(coordinates);
|
|
||||||
}
|
|
||||||
} else if (shape instanceof google.maps.Circle) {
|
|
||||||
const center = shape.getCenter();
|
|
||||||
const radius = shape.getRadius();
|
|
||||||
if (center) {
|
|
||||||
const coordinates = [
|
|
||||||
{
|
|
||||||
lat: center.lat(),
|
|
||||||
lng: center.lng(),
|
|
||||||
radius: radius, // circle's radius in meters
|
|
||||||
},
|
|
||||||
];
|
|
||||||
console.log("Circle center:", coordinates);
|
|
||||||
onAreaSelected(coordinates);
|
|
||||||
}
|
|
||||||
} else if (shape instanceof google.maps.Polyline) {
|
|
||||||
const path = shape.getPath();
|
const path = shape.getPath();
|
||||||
const coordinates = path.getArray().map((latLng) => ({
|
const coordinates = path.getArray().map((latLng) => ({
|
||||||
lat: latLng.lat(),
|
lat: latLng.lat(),
|
||||||
@ -90,9 +55,6 @@ const GoogleMapWithDrawing = ({
|
|||||||
drawingControlOptions: {
|
drawingControlOptions: {
|
||||||
position: google.maps.ControlPosition.TOP_CENTER,
|
position: google.maps.ControlPosition.TOP_CENTER,
|
||||||
drawingModes: [
|
drawingModes: [
|
||||||
google.maps.drawing.OverlayType.POLYGON,
|
|
||||||
google.maps.drawing.OverlayType.RECTANGLE,
|
|
||||||
google.maps.drawing.OverlayType.CIRCLE,
|
|
||||||
google.maps.drawing.OverlayType.POLYLINE,
|
google.maps.drawing.OverlayType.POLYLINE,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
6881
frontend/package-lock.json
generated
Normal file
6881
frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user