From 9e524cc9982ffdff2badeecdfcf8266ec0019cb3 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Fri, 24 Apr 2026 18:16:43 +0200 Subject: [PATCH] feat(events,commitments): add event archiving and commitment inline editing Events: archive button (hover) moves event to archived section; archived events collapsible at bottom with count; PATCH /api/events/[id] for status change; getArchivedEvents() query; page fetches both in parallel. Commitments: inline edit mode per row (pencil icon on hover); PATCH /api/commitments/[id] for description/dueDate/financialImpact; extracted CommitmentItem client component, CommitmentsCard stays server component. Co-Authored-By: Claude Sonnet 4.6 --- src/app/api/commitments/[id]/route.ts | 26 +++++ src/app/api/events/[id]/route.ts | 23 ++++ src/app/events/page.tsx | 6 +- src/components/events/EventsGrid.tsx | 88 +++++++++++--- src/components/today/CommitmentItem.tsx | 140 +++++++++++++++++++++++ src/components/today/CommitmentsCard.tsx | 43 ++----- src/db/queries/events.ts | 12 ++ 7 files changed, 286 insertions(+), 52 deletions(-) create mode 100644 src/components/today/CommitmentItem.tsx diff --git a/src/app/api/commitments/[id]/route.ts b/src/app/api/commitments/[id]/route.ts index d45d7b1ab..0bfba23c0 100644 --- a/src/app/api/commitments/[id]/route.ts +++ b/src/app/api/commitments/[id]/route.ts @@ -5,6 +5,32 @@ import { eq, and } from "drizzle-orm"; import { DEFAULT_USER_ID } from "@/lib/constants"; import { isValidUuid } from "@/lib/utils"; +export async function PATCH( + req: NextRequest, + { params }: { params: Promise<{ id: string }> }, +) { + const { id } = await params; + if (!isValidUuid(id)) return NextResponse.json({ error: "Invalid ID" }, { status: 400 }); + + const body = await req.json() as { description?: string; dueDate?: string | null; financialImpact?: string | null }; + if (body.description !== undefined && !body.description.trim()) { + return NextResponse.json({ error: "Description cannot be empty" }, { status: 400 }); + } + + const patch: Record = { updatedAt: new Date() }; + if (body.description !== undefined) patch.description = body.description.trim(); + if ("dueDate" in body) patch.dueDate = body.dueDate ? new Date(body.dueDate) : null; + if ("financialImpact" in body) patch.financialImpact = body.financialImpact?.trim() || null; + + const [updated] = await db + .update(commitments) + .set(patch) + .where(and(eq(commitments.id, id), eq(commitments.userId, DEFAULT_USER_ID))) + .returning(); + + return NextResponse.json({ ok: true, commitment: updated }); +} + export async function DELETE( _req: NextRequest, { params }: { params: Promise<{ id: string }> }, diff --git a/src/app/api/events/[id]/route.ts b/src/app/api/events/[id]/route.ts index 8102cbd81..bd9c6041b 100644 --- a/src/app/api/events/[id]/route.ts +++ b/src/app/api/events/[id]/route.ts @@ -3,8 +3,31 @@ import { db } from "@/db"; import { events } from "@/db/schema"; import { eq, and } from "drizzle-orm"; import { DEFAULT_USER_ID } from "@/lib/constants"; +import { EVENT_STATUS } from "@/lib/constants/statuses"; import { isValidUuid } from "@/lib/utils"; +export async function PATCH( + req: NextRequest, + { params }: { params: Promise<{ id: string }> }, +) { + const { id } = await params; + if (!isValidUuid(id)) return NextResponse.json({ error: "Invalid ID" }, { status: 400 }); + + const body = await req.json() as { status?: string }; + const validStatuses = Object.values(EVENT_STATUS) as string[]; + if (!body.status || !validStatuses.includes(body.status)) { + return NextResponse.json({ error: "Invalid status" }, { status: 400 }); + } + + const [updated] = await db + .update(events) + .set({ status: body.status, updatedAt: new Date() }) + .where(and(eq(events.id, id), eq(events.userId, DEFAULT_USER_ID))) + .returning(); + + return NextResponse.json({ ok: true, event: updated }); +} + export async function DELETE( _req: NextRequest, { params }: { params: Promise<{ id: string }> }, diff --git a/src/app/events/page.tsx b/src/app/events/page.tsx index 17dd66138..ed9c0809f 100644 --- a/src/app/events/page.tsx +++ b/src/app/events/page.tsx @@ -1,15 +1,15 @@ import { PageLayout } from "@/components/ui/page-layout"; -import { getEvents } from "@/db/queries/events"; +import { getEvents, getArchivedEvents } from "@/db/queries/events"; import { EventsGrid } from "@/components/events/EventsGrid"; export default async function EventsPage() { - const items = await getEvents(); + const [items, archived] = await Promise.all([getEvents(), getArchivedEvents()]); return ( - + ); } diff --git a/src/components/events/EventsGrid.tsx b/src/components/events/EventsGrid.tsx index 54a08c1c4..aed7990a5 100644 --- a/src/components/events/EventsGrid.tsx +++ b/src/components/events/EventsGrid.tsx @@ -1,7 +1,7 @@ "use client"; import { useState } from "react"; -import { Search, ExternalLink, Plus, X, Loader2, Calendar } from "lucide-react"; +import { Search, ExternalLink, Plus, X, Loader2, Calendar, Archive, ChevronDown, ChevronRight } from "lucide-react"; import { isPast, format, formatDistanceToNow } from "date-fns"; import { Card } from "@/components/ui/card"; import { DeleteButton } from "@/components/ui/delete-button"; @@ -12,15 +12,30 @@ import type { EventRow } from "@/db/queries/events"; function EventCard({ event, onDelete, + onArchive, + dimmed = false, }: { event: EventRow; onDelete: (id: string) => void; + onArchive?: (id: string) => void; + dimmed?: boolean; }) { + const [archiving, setArchiving] = useState(false); const deadline = event.deadline ? new Date(event.deadline) : null; const overdue = deadline ? isPast(deadline) : false; + const handleArchive = async () => { + setArchiving(true); + await fetch(`/api/events/${event.id}`, { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ status: "archived" }), + }); + onArchive?.(event.id); + }; + return ( -
+
{/* Type + category badges */}
@@ -60,8 +75,18 @@ function EventCard({ )}
- {/* Delete */} -
+ {/* Actions */} +
+ {onArchive && ( + + )} { await fetch(`/api/events/${event.id}`, { method: "DELETE" }); @@ -199,12 +224,20 @@ function AddEventForm({ onCreated }: { onCreated: (event: EventRow) => void }) { // ─── EventsGrid ─────────────────────────────────────────────────────────────── -export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) { +export function EventsGrid({ + initialEvents, + initialArchived = [], +}: { + initialEvents: EventRow[]; + initialArchived?: EventRow[]; +}) { const [items, setItems] = useState(initialEvents); + const [archived, setArchived] = useState(initialArchived); + const [showArchived, setShowArchived] = useState(false); const [query, setQuery] = useState(""); const [typeFilter, setTypeFilter] = useState(null); - // Unique types derived from data — SSOT, no hardcoding + // Unique types derived from active events — SSOT, no hardcoding const types = [...new Set(items.map((e) => e.type).filter(Boolean))].sort() as string[]; const q = query.trim().toLowerCase(); @@ -221,13 +254,16 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) { const withoutDeadline = filtered.filter((e) => !e.deadline); const handleDelete = (id: string) => setItems((prev) => prev.filter((e) => e.id !== id)); + const handleDeleteArchived = (id: string) => setArchived((prev) => prev.filter((e) => e.id !== id)); + + const handleArchive = (id: string) => { + const event = items.find((e) => e.id === id); + setItems((prev) => prev.filter((e) => e.id !== id)); + if (event) setArchived((prev) => [{ ...event, status: "archived" }, ...prev]); + }; const handleCreated = (event: EventRow) => { - // Insert with deadline at front of deadline group, else at end - setItems((prev) => { - if (event.deadline) return [event, ...prev]; - return [...prev, event]; - }); + setItems((prev) => event.deadline ? [event, ...prev] : [...prev, event]); }; return ( @@ -276,7 +312,7 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
)} - {/* Events list */} + {/* Active events */} {filtered.length === 0 ? (
@@ -287,23 +323,20 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) {
) : (
- {/* With deadline — ordered by deadline */} {withDeadline.length > 0 && (
{withDeadline.map((event) => ( - + ))}
)} - - {/* Without deadline */} {withoutDeadline.length > 0 && (
0 ? "mt-2 pt-2 border-t border-white/[0.05]" : ""}> {withDeadline.length > 0 && (
No deadline
)} {withoutDeadline.map((event) => ( - + ))}
)} @@ -312,6 +345,27 @@ export function EventsGrid({ initialEvents }: { initialEvents: EventRow[] }) { + + {/* Archived events — collapsible */} + {archived.length > 0 && ( +
+ + {showArchived && ( + + {archived.map((event) => ( + + ))} + + )} +
+ )} ); } diff --git a/src/components/today/CommitmentItem.tsx b/src/components/today/CommitmentItem.tsx new file mode 100644 index 000000000..e41c6ca62 --- /dev/null +++ b/src/components/today/CommitmentItem.tsx @@ -0,0 +1,140 @@ +"use client"; + +import { useState } from "react"; +import { useRouter } from "next/navigation"; +import { AlertCircle, Pencil, X, Check, Loader2 } from "lucide-react"; +import { formatDistanceToNow, format } from "date-fns"; +import { FulfillCommitmentButton } from "./FulfillCommitmentButton"; +import { DeleteCommitmentButton } from "./DeleteCommitmentButton"; + +type CommitmentItemProps = { + id: string; + description: string; + dueDate: Date | null; + financialImpact: string | null; +}; + +export function CommitmentItem({ id, description, dueDate, financialImpact }: CommitmentItemProps) { + const router = useRouter(); + const [editing, setEditing] = useState(false); + const [desc, setDesc] = useState(description); + const [date, setDate] = useState(dueDate ? format(new Date(dueDate), "yyyy-MM-dd") : ""); + const [impact, setImpact] = useState(financialImpact ?? ""); + const [saving, setSaving] = useState(false); + const [error, setError] = useState(""); + + const isOverdue = dueDate && new Date(dueDate) < new Date(); + + const save = async () => { + if (!desc.trim()) { setError("Description required"); return; } + setSaving(true); + setError(""); + try { + const res = await fetch(`/api/commitments/${id}`, { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + description: desc, + dueDate: date || null, + financialImpact: impact || null, + }), + }); + if (!res.ok) { + const data = await res.json() as { error?: string }; + setError(data.error ?? "Failed to save"); + return; + } + setEditing(false); + router.refresh(); + } finally { + setSaving(false); + } + }; + + const cancel = () => { + setDesc(description); + setDate(dueDate ? format(new Date(dueDate), "yyyy-MM-dd") : ""); + setImpact(financialImpact ?? ""); + setError(""); + setEditing(false); + }; + + if (editing) { + return ( +
+
+
+ setDesc(e.target.value)} + autoFocus + onKeyDown={(e) => { if (e.key === "Escape") cancel(); }} + className="w-full bg-white/[0.04] border border-white/15 rounded px-2 py-1 text-sm text-white/85 placeholder:text-white/20 focus:outline-none focus:border-white/30" + /> +
+ setDate(e.target.value)} + className="bg-white/[0.04] border border-white/10 rounded px-2 py-1 text-xs text-white/70 focus:outline-none focus:border-white/25" + /> + setImpact(e.target.value)} + placeholder="Financial impact" + onKeyDown={(e) => { if (e.key === "Enter") save(); if (e.key === "Escape") cancel(); }} + className="flex-1 bg-white/[0.04] border border-white/10 rounded px-2 py-1 text-xs text-white/70 placeholder:text-white/20 focus:outline-none focus:border-white/25" + /> +
+ {error &&

{error}

} +
+ + +
+
+
+ ); + } + + return ( +
+ {isOverdue ? ( + + ) : ( +
+ )} +
+
{description}
+ {dueDate && ( +
+ {isOverdue ? "Overdue" : "Due"}{" "} + {formatDistanceToNow(new Date(dueDate), { addSuffix: true })} +
+ )} + {financialImpact && ( +
{financialImpact}
+ )} +
+
+ + + +
+
+ ); +} diff --git a/src/components/today/CommitmentsCard.tsx b/src/components/today/CommitmentsCard.tsx index 74e1fee7a..f2556c7fe 100644 --- a/src/components/today/CommitmentsCard.tsx +++ b/src/components/today/CommitmentsCard.tsx @@ -1,9 +1,7 @@ -import { CheckCircle, AlertCircle } from "lucide-react"; +import { CheckCircle } from "lucide-react"; import { Card, CardHeader } from "@/components/ui/card"; import { getActiveCommitments } from "@/db/queries/today"; -import { formatDistanceToNow } from "date-fns"; -import { FulfillCommitmentButton } from "./FulfillCommitmentButton"; -import { DeleteCommitmentButton } from "./DeleteCommitmentButton"; +import { CommitmentItem } from "./CommitmentItem"; import { AddCommitmentButton } from "./AddCommitmentButton"; export async function CommitmentsCard() { @@ -20,34 +18,15 @@ export async function CommitmentsCard() {
No active commitments
) : (
- {items.map((item) => { - const isOverdue = item.dueDate && new Date(item.dueDate) < new Date(); - return ( -
- {isOverdue ? ( - - ) : ( -
- )} -
-
{item.description}
- {item.dueDate && ( -
- {isOverdue ? "Overdue" : "Due"}{" "} - {formatDistanceToNow(new Date(item.dueDate), { addSuffix: true })} -
- )} - {item.financialImpact && ( -
{item.financialImpact}
- )} -
-
- - -
-
- ); - })} + {items.map((item) => ( + + ))}
)} diff --git a/src/db/queries/events.ts b/src/db/queries/events.ts index 60d7f5552..6c53a01ee 100644 --- a/src/db/queries/events.ts +++ b/src/db/queries/events.ts @@ -36,3 +36,15 @@ export async function getEvents(): Promise { asc(events.name), ); } + +export async function getArchivedEvents(): Promise { + return db + .select() + .from(events) + .where(and(eq(events.userId, DEFAULT_USER_ID), eq(events.status, EVENT_STATUS.ARCHIVED))) + .orderBy( + sql`CASE WHEN ${events.deadline} IS NOT NULL THEN 0 ELSE 1 END`, + asc(events.deadline), + asc(events.name), + ); +}