From 20c1efa32554fadccfa6898e250e866adcde40eb Mon Sep 17 00:00:00 2001 From: Sosokker Date: Fri, 14 Feb 2025 04:16:09 +0700 Subject: [PATCH] ui: modify new crop dialog --- .../(sidebar)/farms/[farmId]/crop-card.tsx | 15 +- .../(sidebar)/farms/[farmId]/crop-dialog.tsx | 131 ++++++++++++++++++ .../app/(sidebar)/farms/[farmId]/page.tsx | 59 ++++---- frontend/app/(sidebar)/farms/page.tsx | 15 +- 4 files changed, 182 insertions(+), 38 deletions(-) create mode 100644 frontend/app/(sidebar)/farms/[farmId]/crop-dialog.tsx diff --git a/frontend/app/(sidebar)/farms/[farmId]/crop-card.tsx b/frontend/app/(sidebar)/farms/[farmId]/crop-card.tsx index 6d9dd72..2304364 100644 --- a/frontend/app/(sidebar)/farms/[farmId]/crop-card.tsx +++ b/frontend/app/(sidebar)/farms/[farmId]/crop-card.tsx @@ -4,9 +4,10 @@ import { Crop } from "@/types"; interface CropCardProps { crop: Crop; + onClick?: () => void; } -export function CropCard({ crop }: CropCardProps) { +export function CropCard({ crop, onClick }: CropCardProps) { const statusColors = { growing: "text-green-500", harvested: "text-yellow-500", @@ -14,8 +15,10 @@ export function CropCard({ crop }: CropCardProps) { }; return ( - - + +
@@ -24,10 +27,10 @@ export function CropCard({ crop }: CropCardProps) {
-
-

{crop.name}

+
+

{crop.name}

- +

Planted: {crop.plantedDate.toLocaleDateString()}

diff --git a/frontend/app/(sidebar)/farms/[farmId]/crop-dialog.tsx b/frontend/app/(sidebar)/farms/[farmId]/crop-dialog.tsx new file mode 100644 index 0000000..108dd20 --- /dev/null +++ b/frontend/app/(sidebar)/farms/[farmId]/crop-dialog.tsx @@ -0,0 +1,131 @@ +"use client"; + +import { useState } from "react"; +import { Dialog, DialogContent } from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Card } from "@/components/ui/card"; +import { Check, MapPin } from "lucide-react"; +import { cn } from "@/lib/utils"; +import type { Crop } from "@/types"; + +interface Plant { + id: string; + name: string; + image: string; + growthTime: string; +} + +const plants: Plant[] = [ + { + id: "durian", + name: "Durian", + image: "/placeholder.svg?height=80&width=80", + growthTime: "4-5 months", + }, + { + id: "mango", + name: "Mango", + image: "/placeholder.svg?height=80&width=80", + growthTime: "3-4 months", + }, + { + id: "coconut", + name: "Coconut", + image: "/placeholder.svg?height=80&width=80", + growthTime: "5-6 months", + }, +]; + +interface CropDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + onSubmit: (data: Partial) => Promise; +} + +export function CropDialog({ open, onOpenChange, onSubmit }: CropDialogProps) { + const [selectedPlant, setSelectedPlant] = useState(null); + const [location, setLocation] = useState({ lat: 13.7563, lng: 100.5018 }); // Bangkok coordinates + + const handleSubmit = async () => { + if (!selectedPlant) return; + + await onSubmit({ + name: plants.find((p) => p.id === selectedPlant)?.name || "", + plantedDate: new Date(), + status: "planned", + }); + + setSelectedPlant(null); + onOpenChange(false); + }; + + return ( + + +
+ {/* Left side - Plant Selection */} +
+

Select Plant to Grow

+
+ {plants.map((plant) => ( + setSelectedPlant(plant.id)}> +
+ {plant.name} +
+
+

{plant.name}

+ {selectedPlant === plant.id && } +
+

Growth time: {plant.growthTime}

+
+
+
+ ))} +
+
+ + {/* Right side - Map */} +
+
+ {/* Placeholder map - Replace with your map component */} +
+
+ +

+ Map placeholder +
+ Lat: {location.lat.toFixed(4)} +
+ Lng: {location.lng.toFixed(4)} +

+
+
+
+
+ + {/* Footer */} +
+
+ + +
+
+
+
+
+ ); +} diff --git a/frontend/app/(sidebar)/farms/[farmId]/page.tsx b/frontend/app/(sidebar)/farms/[farmId]/page.tsx index aa500ce..43eaab7 100644 --- a/frontend/app/(sidebar)/farms/[farmId]/page.tsx +++ b/frontend/app/(sidebar)/farms/[farmId]/page.tsx @@ -6,29 +6,28 @@ import { ArrowLeft, MapPin, Plus, Sprout } from "lucide-react"; import { Card, CardContent, CardHeader } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Separator } from "@/components/ui/separator"; -import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"; -import { AddCropForm } from "./add-crop-form"; +import { CropDialog } from "./crop-dialog"; import { CropCard } from "./crop-card"; import { Farm, Crop } from "@/types"; import React from "react"; const crops: Crop[] = [ { - id: "crop1", + id: "1", farmId: "1", name: "Monthong Durian", plantedDate: new Date("2023-03-15"), status: "growing", }, { - id: "crop2", + id: "2", farmId: "1", name: "Chanee Durian", plantedDate: new Date("2023-02-20"), status: "planned", }, { - id: "crop3", + id: "3", farmId: "2", name: "Kradum Durian", plantedDate: new Date("2022-11-05"), @@ -76,6 +75,7 @@ export default function FarmDetailPage({ params }: { params: Promise<{ farmId: s status: data.status!, }; setCrops((prevCrops) => [...prevCrops, newCrop]); + // When the crop gets added, close the dialog setIsDialogOpen(false); }; @@ -121,33 +121,34 @@ export default function FarmDetailPage({ params }: { params: Promise<{ farmId: s

Crops

- - setIsDialogOpen(true)}> - -
-
- -
-
-

Add Crop

-

Plant a new crop

-
+ {/* Clickable "Add Crop" Card */} + setIsDialogOpen(true)}> + +
+
+
- - - - - Add New Crop - Fill out the form to add a new crop to your farm. - - setIsDialogOpen(false)} /> - -
+
+

Add Crop

+

Plant a new crop

+
+
+ + + + {/* New Crop Dialog */} + {crops.map((crop) => ( - + { + router.push(`/farms/${crop.farmId}/crops/${crop.id}`); + }} + /> ))}
diff --git a/frontend/app/(sidebar)/farms/page.tsx b/frontend/app/(sidebar)/farms/page.tsx index 04895ea..e123112 100644 --- a/frontend/app/(sidebar)/farms/page.tsx +++ b/frontend/app/(sidebar)/farms/page.tsx @@ -4,12 +4,14 @@ import { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"; import { Separator } from "@/components/ui/separator"; import { Input } from "@/components/ui/input"; -import { Search } from "lucide-react"; +import { Link, Search } from "lucide-react"; import { FarmCard } from "./farm-card"; import { AddFarmForm } from "./add-farm-form"; +import { useRouter } from "next/navigation"; import type { Farm } from "@/types"; export default function FarmSetupPage() { + const router = useRouter(); const [isDialogOpen, setIsDialogOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(""); const [farms, setFarms] = useState([ @@ -17,7 +19,7 @@ export default function FarmSetupPage() { id: "1", name: "Green Valley Farm", location: "Bangkok", - type: "durian", + type: "Durian", createdAt: new Date(), }, ]); @@ -70,7 +72,14 @@ export default function FarmSetupPage() { {filteredFarms.map((farm) => ( - + { + router.push(`/farms/${farm.id}`); + }} + /> ))}