"use client"; import Link from "next/link"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { deleteCareSchedule, scheduleOnCalendar, toggleCareSchedule, upsertCareSchedule, } from "../server/actions"; import type { CalendarDto } from "../server/calendar-bridge"; import type { CareScheduleDto } from "../server/queries"; const CARE_TYPES = ["watering", "fertilizing", "repotting", "pruning", "pest-control", "other"]; type Props = { plantId: string; schedules: CareScheduleDto[]; calendars: CalendarDto[]; }; function daysLabel(n: number | null): string { if (n === null) return "No due date"; if (n < 0) return `${Math.abs(n)} day${Math.abs(n) !== 1 ? "s" : ""} overdue`; if (n === 0) return "Due today"; return `Due in ${n} day${n !== 1 ? "s" : ""}`; } export function CareScheduleEditor({ plantId, schedules, calendars }: Props) { const [showForm, setShowForm] = useState(false); const [careType, setCareType] = useState("watering"); const [intervalDays, setIntervalDays] = useState("7"); const [formError, setFormError] = useState(null); const [calendarOpenId, setCalendarOpenId] = useState(null); const [selectedCalendarId, setSelectedCalendarId] = useState(calendars[0]?.id ?? ""); const [reminderMinutes, setReminderMinutes] = useState(""); const [calendarSuccess, setCalendarSuccess] = useState(null); const [isPending, startTransition] = useTransition(); const router = useRouter(); function handleAddSchedule(e: React.FormEvent) { e.preventDefault(); const days = parseInt(intervalDays, 10); if (isNaN(days) || days < 1 || days > 365) { setFormError("Interval must be between 1 and 365 days."); return; } setFormError(null); startTransition(async () => { try { await upsertCareSchedule({ plantId, careType, intervalDays: days }); setShowForm(false); setCareType("watering"); setIntervalDays("7"); router.refresh(); } catch { setFormError("Failed to save schedule."); } }); } function handleDelete(id: string) { startTransition(async () => { await deleteCareSchedule({ id }); router.refresh(); }); } function handleToggle(id: string, enabled: boolean) { startTransition(async () => { await toggleCareSchedule({ id, enabled }); router.refresh(); }); } function handleScheduleOnCalendar(scheduleId: string) { if (!selectedCalendarId) return; startTransition(async () => { try { await scheduleOnCalendar({ scheduleId, calendarId: selectedCalendarId, reminderMinutesBefore: reminderMinutes ? parseInt(reminderMinutes, 10) : undefined, }); setCalendarOpenId(null); setCalendarSuccess(scheduleId); setTimeout(() => setCalendarSuccess(null), 4000); } catch (err) { setFormError(err instanceof Error ? err.message : "Failed to add to calendar."); } }); } return (

Schedules

{showForm && (
setIntervalDays(e.target.value)} className="input input-xs" required />
{formError &&

{formError}

}
)} {schedules.length === 0 && !showForm && (

No schedules yet.

)} {schedules.map((s) => (
{s.careType} Every {s.intervalDays} days · {daysLabel(s.daysUntilDue)}
{calendars.length > 0 && s.nextDueAt && ( )}
{calendarOpenId === s.id && (
setReminderMinutes(e.target.value)} className="input input-xs" />
)} {calendarSuccess === s.id && (

Event added to calendar.{" "} View in calendar →

)}
))}
); }