Spaces:
Runtime error
Runtime error
Download src/components/ScheduleTaskDialog.tsx from Pavankumardns/flora: direct link, hf CLI and curl.
- Browser
- Download file 7.48 kB
-
https://huggingface.co/spaces/Pavankumardns/flora/resolve/main/src/components/ScheduleTaskDialog.tsx
- Command line
-
hf download hf://spaces/Pavankumardns/flora/src/components/ScheduleTaskDialog.tsx
-
curl -L -o ScheduleTaskDialog.tsx https://huggingface.co/spaces/Pavankumardns/flora/resolve/main/src/components/ScheduleTaskDialog.tsx
7.48 kB
| 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 ( | |
| <Dialog open={open} onOpenChange={setOpen}> | |
| <DialogTrigger asChild> | |
| <Button variant="hero-outline" className="w-full justify-start" size="sm"> | |
| <Calendar className="w-4 h-4" /> | |
| Schedule Task | |
| </Button> | |
| </DialogTrigger> | |
| <DialogContent className="sm:max-w-[425px]"> | |
| <DialogHeader> | |
| <DialogTitle>Schedule Garden Task</DialogTitle> | |
| <DialogDescription> | |
| Create a reminder for watering, fertilizing, or other garden tasks. | |
| </DialogDescription> | |
| </DialogHeader> | |
| <form onSubmit={handleSubmit} className="space-y-4 mt-4"> | |
| <div className="space-y-2"> | |
| <Label htmlFor="title">Task Title *</Label> | |
| <Input | |
| id="title" | |
| placeholder="e.g., Water tomato plants" | |
| value={formData.title} | |
| onChange={(e) => setFormData({ ...formData, title: e.target.value })} | |
| required | |
| /> | |
| </div> | |
| <div className="space-y-2"> | |
| <Label htmlFor="type">Task Type</Label> | |
| <Select | |
| value={formData.type} | |
| onValueChange={(value) => setFormData({ ...formData, type: value })} | |
| > | |
| <SelectTrigger> | |
| <SelectValue /> | |
| </SelectTrigger> | |
| <SelectContent> | |
| {taskTypes.map((type) => ( | |
| <SelectItem key={type.value} value={type.value}> | |
| {type.label} | |
| </SelectItem> | |
| ))} | |
| </SelectContent> | |
| </Select> | |
| </div> | |
| <div className="space-y-2"> | |
| <Label htmlFor="description">Description</Label> | |
| <Textarea | |
| id="description" | |
| placeholder="Add notes about this task..." | |
| value={formData.description} | |
| onChange={(e) => setFormData({ ...formData, description: e.target.value })} | |
| rows={3} | |
| /> | |
| </div> | |
| <div className="grid grid-cols-2 gap-4"> | |
| <div className="space-y-2"> | |
| <Label htmlFor="dueDate">Due Date *</Label> | |
| <Input | |
| id="dueDate" | |
| type="date" | |
| value={formData.dueDate} | |
| onChange={(e) => setFormData({ ...formData, dueDate: e.target.value })} | |
| required | |
| /> | |
| </div> | |
| <div className="space-y-2"> | |
| <Label htmlFor="dueTime">Time</Label> | |
| <Input | |
| id="dueTime" | |
| type="time" | |
| value={formData.dueTime} | |
| onChange={(e) => setFormData({ ...formData, dueTime: e.target.value })} | |
| /> | |
| </div> | |
| </div> | |
| <div className="space-y-2"> | |
| <Label htmlFor="priority">Priority</Label> | |
| <Select | |
| value={formData.priority} | |
| onValueChange={(value) => setFormData({ ...formData, priority: value })} | |
| > | |
| <SelectTrigger> | |
| <SelectValue /> | |
| </SelectTrigger> | |
| <SelectContent> | |
| <SelectItem value="low">Low</SelectItem> | |
| <SelectItem value="medium">Medium</SelectItem> | |
| <SelectItem value="high">High</SelectItem> | |
| </SelectContent> | |
| </Select> | |
| </div> | |
| <div className="flex gap-2 pt-4"> | |
| <Button | |
| type="button" | |
| variant="outline" | |
| onClick={() => setOpen(false)} | |
| className="flex-1" | |
| disabled={isSubmitting} | |
| > | |
| Cancel | |
| </Button> | |
| <Button | |
| type="submit" | |
| variant="hero" | |
| className="flex-1" | |
| disabled={isSubmitting} | |
| > | |
| {isSubmitting ? ( | |
| <> | |
| <Loader2 className="w-4 h-4 animate-spin" /> | |
| Scheduling... | |
| </> | |
| ) : ( | |
| "Schedule Task" | |
| )} | |
| </Button> | |
| </div> | |
| </form> | |
| </DialogContent> | |
| </Dialog> | |
| ); | |
| }; | |
| export default ScheduleTaskDialog; | |