Spaces:
Runtime error
Runtime error
File size: 7,482 Bytes
ae02946 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 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;
|