2026-07-29 06:12:35 -04:00
|
|
|
'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 { Textarea } from '@/components/ui/textarea';
|
|
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from '@/components/ui/select';
|
|
|
|
|
import { toast } from 'sonner';
|
2026-07-29 19:26:17 +00:00
|
|
|
import { TaskTemplates } from '@/components/tasks/task-templates';
|
2026-07-29 06:12:35 -04:00
|
|
|
|
|
|
|
|
interface TaskCreateDialogProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
domainId: string;
|
|
|
|
|
defaultStatus?: 'todo' | 'in_progress' | 'done' | 'cancelled';
|
|
|
|
|
onCreated: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TaskCreateDialog({
|
|
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
domainId,
|
|
|
|
|
defaultStatus = 'todo',
|
|
|
|
|
onCreated,
|
|
|
|
|
}: TaskCreateDialogProps) {
|
|
|
|
|
const [title, setTitle] = useState('');
|
|
|
|
|
const [description, setDescription] = useState('');
|
|
|
|
|
const [status, setStatus] = useState(defaultStatus);
|
|
|
|
|
const [priority, setPriority] = useState<'low' | 'medium' | 'high' | 'urgent'>('medium');
|
|
|
|
|
const [dueDate, setDueDate] = useState('');
|
|
|
|
|
const [estimatedMinutes, setEstimatedMinutes] = useState('');
|
|
|
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
|
|
|
|
|
// Reset form when dialog opens
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (open) {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setDescription('');
|
|
|
|
|
setStatus(defaultStatus);
|
|
|
|
|
setPriority('medium');
|
|
|
|
|
setDueDate('');
|
|
|
|
|
setEstimatedMinutes('');
|
|
|
|
|
setError('');
|
|
|
|
|
}
|
|
|
|
|
}, [open, defaultStatus]);
|
|
|
|
|
|
|
|
|
|
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (!domainId) {
|
|
|
|
|
setError('No domain selected');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setSubmitting(true);
|
|
|
|
|
setError('');
|
|
|
|
|
|
|
|
|
|
const body: Record<string, unknown> = {
|
|
|
|
|
title,
|
|
|
|
|
status,
|
|
|
|
|
priority,
|
|
|
|
|
};
|
|
|
|
|
if (description) body.description = description;
|
|
|
|
|
if (dueDate) body.dueDate = new Date(dueDate).toISOString();
|
|
|
|
|
if (estimatedMinutes) body.estimatedMinutes = parseInt(estimatedMinutes, 10);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`/api/domains/${domainId}/tasks`, {
|
|
|
|
|
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 task');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toast.success('Task created');
|
|
|
|
|
onOpenChange(false);
|
|
|
|
|
onCreated();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err instanceof Error ? err.message : 'Unable to create task');
|
|
|
|
|
} finally {
|
|
|
|
|
setSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogContent className="sm:max-w-[500px]">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>New Task</DialogTitle>
|
|
|
|
|
<DialogDescription>Create a new task to track your work.</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<form className="space-y-4" onSubmit={handleSubmit}>
|
2026-07-29 19:26:17 +00:00
|
|
|
<TaskTemplates onSelect={(t) => {
|
|
|
|
|
setTitle(t.title);
|
|
|
|
|
setDescription(t.description);
|
|
|
|
|
setPriority(t.priority);
|
|
|
|
|
setStatus(t.status);
|
|
|
|
|
}} />
|
|
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="task-title">Title *</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="task-title"
|
|
|
|
|
value={title}
|
|
|
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
|
|
|
placeholder="What needs to be done?"
|
|
|
|
|
autoFocus
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="task-description">Description</Label>
|
|
|
|
|
<Textarea
|
|
|
|
|
id="task-description"
|
|
|
|
|
value={description}
|
|
|
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
|
|
|
placeholder="Add details..."
|
|
|
|
|
rows={3}
|
|
|
|
|
/>
|
|
|
|
|
</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 any)}>
|
|
|
|
|
<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>
|
|
|
|
|
<SelectItem value="cancelled">Cancelled</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="task-priority">Priority</Label>
|
|
|
|
|
<Select value={priority} onValueChange={(v) => setPriority(v as any)}>
|
|
|
|
|
<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-due-date">Due Date</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="task-due-date"
|
|
|
|
|
type="date"
|
|
|
|
|
value={dueDate}
|
|
|
|
|
onChange={(e) => setDueDate(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="task-estimate">Est. Minutes</Label>
|
|
|
|
|
<Input
|
|
|
|
|
id="task-estimate"
|
|
|
|
|
type="number"
|
|
|
|
|
min={1}
|
|
|
|
|
value={estimatedMinutes}
|
|
|
|
|
onChange={(e) => setEstimatedMinutes(e.target.value)}
|
|
|
|
|
placeholder="e.g. 30"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</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 || !title || !domainId}>
|
|
|
|
|
{submitting ? 'Creating...' : 'Create Task'}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</form>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|