import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { useToast } from "@/hooks/use-toast"; import { useUser } from "@clerk/clerk-react"; import { Calendar, Loader2 } from "lucide-react"; interface ScheduleTaskDialogProps { onTaskAdded: () => void; } const ScheduleTaskDialog = ({ onTaskAdded }: ScheduleTaskDialogProps) => { const { user } = useUser(); const { toast } = useToast(); const [open, setOpen] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const [formData, setFormData] = useState({ title: "", description: "", type: "watering", priority: "medium", dueDate: "", dueTime: "", }); const taskTypes = [ { value: "watering", label: "Watering" }, { value: "fertilizing", label: "Fertilizing" }, { value: "pruning", label: "Pruning" }, { value: "pest_control", label: "Pest Control" }, { value: "harvesting", label: "Harvesting" }, { value: "other", label: "Other" }, ]; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!user?.id) { toast({ title: "Error", description: "You must be logged in to schedule tasks", variant: "destructive", }); return; } if (!formData.title || !formData.dueDate) { toast({ title: "Error", description: "Please fill in title and due date", variant: "destructive", }); return; } setIsSubmitting(true); try { const dueDateTime = `${formData.dueDate}T${formData.dueTime || "09:00"}:00`; const response = await fetch("http://localhost:5000/tasks", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userId: user.id, title: formData.title, description: formData.description, type: formData.type, priority: formData.priority, dueDate: dueDateTime, status: "pending", createdAt: new Date().toISOString(), }), }); if (response.ok) { toast({ title: "Success", description: "Task scheduled successfully!", }); setFormData({ title: "", description: "", type: "watering", priority: "medium", dueDate: "", dueTime: "", }); setOpen(false); onTaskAdded(); } else { throw new Error("Failed to schedule task"); } } catch (error) { toast({ title: "Error", description: "Failed to schedule task. Please try again.", variant: "destructive", }); } finally { setIsSubmitting(false); } }; return ( Schedule Garden Task Create a reminder for watering, fertilizing, or other garden tasks.
setFormData({ ...formData, title: e.target.value })} required />