Files
ProjectE/apps/web/components/projects/section-dialog.tsx
T
mbatchelder 064a46f97d feat: Phase 3 - Habits + Projects CRUD API, frontend, completions, sections
Habits REST API:
- GET/POST /api/domains/[domainId]/habits (list with filters, create)
- GET/PATCH/DELETE /api/domains/[domainId]/habits/[id] (detail, update, soft delete)
- POST /api/domains/[domainId]/habits/[id]/complete (completion + streak calc)
- GET /api/domains/[domainId]/habits/[id]/completions (list with date range)
- POST/DELETE /api/domains/[domainId]/habits/[id]/tags

Projects REST API:
- GET/POST /api/domains/[domainId]/projects (list with task counts, create)
- GET/PATCH/DELETE /api/domains/[domainId]/projects/[id] (detail with sections/tasks, update, soft delete)

Sections REST API:
- GET/POST /api/domains/[domainId]/projects/[projectId]/sections (list, create)
- GET/PATCH/DELETE /api/domains/[domainId]/projects/[projectId]/sections/[id]

Frontend:
- Habits page: checklist view, difficulty badges, streak display, filter
- Habit create dialog: name, description, frequency, difficulty, goal, unit, reminder, mood toggle
- Habit completion dialog: value, mood (1-5 emoji), notes
- Calendar heatmap: 365-day grid, color by value, hover tooltip
- Projects page: grid of cards with progress bars, status badges, tags
- Project detail page: sections board, drag tasks between sections
- Project create dialog: name, description, status, color picker, target date
- Section dialog: name, kind (section/milestone), status, target date

Keyboard shortcuts: c h (new habit), c p (new project), c s (new section)

All write routes follow AGENTS.md contract (Drizzle + recordActivity + pg_notify).
Build, typecheck, and 15 new tests pass.
2026-07-29 06:37:37 -04:00

164 lines
4.9 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { toast } from 'sonner';
interface SectionDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
projectId: string;
domainId: string;
onCreated: () => void;
}
export function SectionDialog({
open,
onOpenChange,
projectId,
domainId,
onCreated,
}: SectionDialogProps) {
const [name, setName] = useState('');
const [kind, setKind] = useState<'section' | 'milestone'>('section');
const [status, setStatus] = useState<'planned' | 'in_progress' | 'complete'>('planned');
const [targetDate, setTargetDate] = useState('');
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState('');
useEffect(() => {
if (open) {
setName('');
setKind('section');
setStatus('planned');
setTargetDate('');
setError('');
}
}, [open]);
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!name) {
setError('Name is required');
return;
}
setSubmitting(true);
setError('');
const body: Record<string, unknown> = { name, kind, status };
if (targetDate) body.targetDate = new Date(targetDate).toISOString();
try {
const response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.error?.message || 'Unable to create section');
}
toast.success('Section created');
onOpenChange(false);
onCreated();
} catch (err) {
setError(err instanceof Error ? err.message : 'Unable to create section');
} finally {
setSubmitting(false);
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[450px]">
<DialogHeader>
<DialogTitle>New Section</DialogTitle>
<DialogDescription>Add a section or milestone to organize tasks.</DialogDescription>
</DialogHeader>
<form className="space-y-4" onSubmit={handleSubmit}>
<div className="space-y-2">
<Label htmlFor="section-name">Name *</Label>
<Input
id="section-name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="e.g. Backend, Design, Launch"
autoFocus
required
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="section-kind">Kind</Label>
<Select value={kind} onValueChange={(v) => setKind(v as any)}>
<SelectTrigger id="section-kind">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="section">Section</SelectItem>
<SelectItem value="milestone">Milestone</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="section-status">Status</Label>
<Select value={status} onValueChange={(v) => setStatus(v as any)}>
<SelectTrigger id="section-status">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="planned">Planned</SelectItem>
<SelectItem value="in_progress">In Progress</SelectItem>
<SelectItem value="complete">Complete</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="section-target-date">Target date</Label>
<Input
id="section-target-date"
type="date"
value={targetDate}
onChange={(e) => setTargetDate(e.target.value)}
/>
</div>
{error && <p className="text-sm text-destructive" role="alert">{error}</p>}
<DialogFooter>
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button type="submit" disabled={submitting || !name}>
{submitting ? 'Creating...' : 'Create Section'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}