255 lines
7.8 KiB
TypeScript
255 lines
7.8 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import { toast } from 'sonner';
|
|
import {
|
|
Sheet,
|
|
SheetContent,
|
|
SheetHeader,
|
|
SheetTitle
|
|
} from '@/components/ui/sheet';
|
|
import { Button } from '@/components/ui/button';
|
|
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 {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle
|
|
} from '@/components/ui/alert-dialog';
|
|
|
|
interface Task {
|
|
id: string;
|
|
title: string;
|
|
description?: string;
|
|
status: 'todo' | 'in_progress' | 'done';
|
|
priority: 'low' | 'medium' | 'high' | 'urgent';
|
|
domain: string;
|
|
due_date?: string;
|
|
project_id?: string;
|
|
tags: string[];
|
|
}
|
|
|
|
interface TaskDetailPanelProps {
|
|
task: Task;
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
onUpdate: () => void;
|
|
}
|
|
|
|
export function TaskDetailPanel({
|
|
task,
|
|
open,
|
|
onOpenChange,
|
|
onUpdate
|
|
}: TaskDetailPanelProps) {
|
|
const [title, setTitle] = useState(task.title);
|
|
const [description, setDescription] = useState(task.description || '');
|
|
const [status, setStatus] = useState(task.status);
|
|
const [priority, setPriority] = useState(task.priority);
|
|
const [domain, setDomain] = useState(task.domain);
|
|
const [domains, setDomains] = useState<{id: string; name: string}[]>([]);
|
|
|
|
useEffect(() => {
|
|
fetch('/api/domains?sort=sort_order').then(r => r.json()).then(data => setDomains(data.items || [])).catch(() => {});
|
|
}, []);
|
|
const [dueDate, setDueDate] = useState(task.due_date || '');
|
|
const [saving, setSaving] = useState(false);
|
|
const [deleteOpen, setDeleteOpen] = useState(false);
|
|
const [deleting, setDeleting] = useState(false);
|
|
|
|
async function handleSave() {
|
|
setSaving(true);
|
|
try {
|
|
const response = await fetch(`/api/tasks/${task.id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
title,
|
|
description,
|
|
status,
|
|
priority,
|
|
domain,
|
|
due_date: dueDate || undefined
|
|
})
|
|
});
|
|
if (!response.ok) throw new Error('Unable to save task');
|
|
onUpdate();
|
|
onOpenChange(false);
|
|
toast.success('Task saved');
|
|
} catch (error) {
|
|
console.error('Failed to update task:', error);
|
|
toast.error('Unable to save task');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
async function handleDelete() {
|
|
setDeleting(true);
|
|
try {
|
|
const response = await fetch(`/api/tasks/${task.id}`, {
|
|
method: 'DELETE'
|
|
});
|
|
if (!response.ok) throw new Error('Unable to delete task');
|
|
onUpdate();
|
|
setDeleteOpen(false);
|
|
onOpenChange(false);
|
|
toast.success('Task deleted');
|
|
} catch (error) {
|
|
console.error('Failed to delete task:', error);
|
|
toast.error('Unable to delete task');
|
|
} finally {
|
|
setDeleting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
|
<SheetContent className="w-[500px] sm:w-[600px] overflow-y-auto">
|
|
<SheetHeader>
|
|
<SheetTitle>Task Details</SheetTitle>
|
|
</SheetHeader>
|
|
|
|
<div className="mt-6 space-y-6">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="title">Title</Label>
|
|
<Input
|
|
id="title"
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
placeholder="Task title"
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="description">Description</Label>
|
|
<Textarea
|
|
id="description"
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
placeholder="Add a description..."
|
|
rows={4}
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="task-status">Status</Label>
|
|
<Select
|
|
value={status}
|
|
onValueChange={(v) => setStatus(v as Task['status'])}
|
|
>
|
|
<SelectTrigger id="task-status">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="todo">To Do</SelectItem>
|
|
<SelectItem value="in_progress">In Progress</SelectItem>
|
|
<SelectItem value="done">Done</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="task-priority">Priority</Label>
|
|
<Select
|
|
value={priority}
|
|
onValueChange={(v) => setPriority(v as Task['priority'])}
|
|
>
|
|
<SelectTrigger id="task-priority">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="low">Low</SelectItem>
|
|
<SelectItem value="medium">Medium</SelectItem>
|
|
<SelectItem value="high">High</SelectItem>
|
|
<SelectItem value="urgent">Urgent</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="task-domain">Domain</Label>
|
|
<Select value={domain} onValueChange={setDomain}>
|
|
<SelectTrigger id="task-domain">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{domains.map((d) => (
|
|
<SelectItem key={d.id} value={d.id}>{d.name}</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="task-due-date">Due Date</Label>
|
|
<Input
|
|
id="task-due-date"
|
|
type="date"
|
|
value={dueDate}
|
|
onChange={(e) => setDueDate(e.target.value)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex gap-2 pt-4">
|
|
<Button onClick={handleSave} disabled={saving}>
|
|
{saving ? 'Saving...' : 'Save Changes'}
|
|
</Button>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
variant="destructive"
|
|
onClick={() => setDeleteOpen(true)}
|
|
className="ml-auto"
|
|
>
|
|
Delete
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Delete task?</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This permanently deletes "{task.title}".
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={deleting}>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
onClick={(event) => {
|
|
event.preventDefault();
|
|
handleDelete();
|
|
}}
|
|
disabled={deleting}
|
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
>
|
|
{deleting ? 'Deleting...' : 'Delete'}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</SheetContent>
|
|
</Sheet>
|
|
);
|
|
}
|