feat(sections): refactor section dialog for edit/delete, add dropdown menu in project detail
This commit is contained in:
@@ -2,10 +2,26 @@
|
|||||||
|
|
||||||
import { useState, useEffect, useCallback } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import { useParams } from "next/navigation";
|
import { useParams } from "next/navigation";
|
||||||
import { Plus, ArrowLeft, GripVertical, MoreHorizontal } from "lucide-react";
|
import { Plus, ArrowLeft, GripVertical, MoreHorizontal, Pencil, Trash2 } from "lucide-react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Progress } from "@/components/ui/progress";
|
import { Progress } from "@/components/ui/progress";
|
||||||
|
import {
|
||||||
|
DropdownMenu,
|
||||||
|
DropdownMenuContent,
|
||||||
|
DropdownMenuItem,
|
||||||
|
DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
} from "@/components/ui/alert-dialog";
|
||||||
import { SectionDialog } from "@/components/projects/section-dialog";
|
import { SectionDialog } from "@/components/projects/section-dialog";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
@@ -65,6 +81,9 @@ export default function ProjectDetailPage() {
|
|||||||
const [domainId, setDomainId] = useState<string | null>(null);
|
const [domainId, setDomainId] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [sectionDialogOpen, setSectionDialogOpen] = useState(false);
|
const [sectionDialogOpen, setSectionDialogOpen] = useState(false);
|
||||||
|
const [editSection, setEditSection] = useState<Section | null>(null);
|
||||||
|
const [deleteSection, setDeleteSection] = useState<Section | null>(null);
|
||||||
|
const [deletingSection, setDeletingSection] = useState(false);
|
||||||
const [draggedTaskId, setDraggedTaskId] = useState<string | null>(null);
|
const [draggedTaskId, setDraggedTaskId] = useState<string | null>(null);
|
||||||
|
|
||||||
// Extract domainId from the project data
|
// Extract domainId from the project data
|
||||||
@@ -239,9 +258,31 @@ export default function ProjectDetailPage() {
|
|||||||
<Badge variant="outline" className="text-xs">Milestone</Badge>
|
<Badge variant="outline" className="text-xs">Milestone</Badge>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<span className="text-xs text-muted-foreground">
|
<div className="flex items-center gap-1">
|
||||||
{(tasksBySection.get(section.id) || []).length}
|
<span className="text-xs text-muted-foreground">
|
||||||
</span>
|
{(tasksBySection.get(section.id) || []).length}
|
||||||
|
</span>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<button
|
||||||
|
className="rounded-md p-1 text-muted-foreground hover:text-foreground hover:bg-accent transition-colors"
|
||||||
|
aria-label={`Options for ${section.name}`}
|
||||||
|
>
|
||||||
|
<MoreHorizontal className="h-3.5 w-3.5" />
|
||||||
|
</button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => setEditSection(section)}>
|
||||||
|
<Pencil className="mr-2 h-4 w-4" />
|
||||||
|
Edit
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem onClick={() => setDeleteSection(section)}>
|
||||||
|
<Trash2 className="mr-2 h-4 w-4" />
|
||||||
|
Delete
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
className="space-y-2 min-h-[100px]"
|
className="space-y-2 min-h-[100px]"
|
||||||
@@ -297,6 +338,53 @@ export default function ProjectDetailPage() {
|
|||||||
domainId={domainId || ''}
|
domainId={domainId || ''}
|
||||||
onCreated={fetchProject}
|
onCreated={fetchProject}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{editSection && (
|
||||||
|
<SectionDialog
|
||||||
|
open={!!editSection}
|
||||||
|
onOpenChange={(open) => { if (!open) setEditSection(null); }}
|
||||||
|
projectId={projectId}
|
||||||
|
domainId={domainId || ''}
|
||||||
|
onCreated={fetchProject}
|
||||||
|
existingSection={editSection}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<AlertDialog open={!!deleteSection} onOpenChange={(open) => { if (!open) setDeleteSection(null); }}>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>Delete Section</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
Are you sure you want to delete "{deleteSection?.name}"? This action cannot be undone. Tasks in this section will become unassigned.
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
<AlertDialogAction
|
||||||
|
disabled={deletingSection}
|
||||||
|
onClick={async () => {
|
||||||
|
if (!deleteSection || !domainId) return;
|
||||||
|
setDeletingSection(true);
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections/${deleteSection.id}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error('Failed to delete');
|
||||||
|
toast.success('Section deleted');
|
||||||
|
setDeleteSection(null);
|
||||||
|
fetchProject();
|
||||||
|
} catch {
|
||||||
|
toast.error('Failed to delete section');
|
||||||
|
} finally {
|
||||||
|
setDeletingSection(false);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{deletingSection ? 'Deleting...' : 'Delete'}
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,7 +188,6 @@ export default function ProjectsPage() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
|
||||||
</Card>
|
</Card>
|
||||||
</Link>
|
</Link>
|
||||||
<div className="absolute right-2 top-2">
|
<div className="absolute right-2 top-2">
|
||||||
|
|||||||
@@ -10,6 +10,16 @@ import {
|
|||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from '@/components/ui/dialog';
|
} from '@/components/ui/dialog';
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
} from '@/components/ui/alert-dialog';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import {
|
import {
|
||||||
@@ -21,12 +31,23 @@ import {
|
|||||||
} from '@/components/ui/select';
|
} from '@/components/ui/select';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
|
interface Section {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
projectId: string;
|
||||||
|
kind: 'section' | 'milestone';
|
||||||
|
status: 'planned' | 'in_progress' | 'complete';
|
||||||
|
targetDate: string | null;
|
||||||
|
sortOrder: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface SectionDialogProps {
|
interface SectionDialogProps {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onOpenChange: (open: boolean) => void;
|
onOpenChange: (open: boolean) => void;
|
||||||
projectId: string;
|
projectId: string;
|
||||||
domainId: string;
|
domainId: string;
|
||||||
onCreated: () => void;
|
onCreated: () => void;
|
||||||
|
existingSection?: Section;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SectionDialog({
|
export function SectionDialog({
|
||||||
@@ -35,23 +56,34 @@ export function SectionDialog({
|
|||||||
projectId,
|
projectId,
|
||||||
domainId,
|
domainId,
|
||||||
onCreated,
|
onCreated,
|
||||||
|
existingSection,
|
||||||
}: SectionDialogProps) {
|
}: SectionDialogProps) {
|
||||||
|
const isEdit = !!existingSection;
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
const [kind, setKind] = useState<'section' | 'milestone'>('section');
|
const [kind, setKind] = useState<'section' | 'milestone'>('section');
|
||||||
const [status, setStatus] = useState<'planned' | 'in_progress' | 'complete'>('planned');
|
const [status, setStatus] = useState<'planned' | 'in_progress' | 'complete'>('planned');
|
||||||
const [targetDate, setTargetDate] = useState('');
|
const [targetDate, setTargetDate] = useState('');
|
||||||
const [submitting, setSubmitting] = useState(false);
|
const [submitting, setSubmitting] = useState(false);
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
|
const [deleteOpen, setDeleteOpen] = useState(false);
|
||||||
|
const [deleting, setDeleting] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open) {
|
if (open) {
|
||||||
setName('');
|
if (existingSection) {
|
||||||
setKind('section');
|
setName(existingSection.name);
|
||||||
setStatus('planned');
|
setKind(existingSection.kind);
|
||||||
setTargetDate('');
|
setStatus(existingSection.status);
|
||||||
|
setTargetDate(existingSection.targetDate ? existingSection.targetDate.split('T')[0] : '');
|
||||||
|
} else {
|
||||||
|
setName('');
|
||||||
|
setKind('section');
|
||||||
|
setStatus('planned');
|
||||||
|
setTargetDate('');
|
||||||
|
}
|
||||||
setError('');
|
setError('');
|
||||||
}
|
}
|
||||||
}, [open]);
|
}, [open, existingSection]);
|
||||||
|
|
||||||
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -66,98 +98,164 @@ export function SectionDialog({
|
|||||||
if (targetDate) body.targetDate = new Date(targetDate).toISOString();
|
if (targetDate) body.targetDate = new Date(targetDate).toISOString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections`, {
|
let response: Response;
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
if (isEdit && existingSection) {
|
||||||
body: JSON.stringify(body),
|
response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections/${existingSection.id}`, {
|
||||||
});
|
method: 'PATCH',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const err = await response.json();
|
const err = await response.json();
|
||||||
throw new Error(err.error?.message || 'Unable to create section');
|
throw new Error(err.error?.message || `Unable to ${isEdit ? 'update' : 'create'} section`);
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.success('Section created');
|
toast.success(isEdit ? 'Section updated' : 'Section created');
|
||||||
onOpenChange(false);
|
onOpenChange(false);
|
||||||
onCreated();
|
onCreated();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Unable to create section');
|
setError(err instanceof Error ? err.message : `Unable to ${isEdit ? 'update' : 'create'} section`);
|
||||||
} finally {
|
} finally {
|
||||||
setSubmitting(false);
|
setSubmitting(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleDelete() {
|
||||||
|
if (!existingSection) return;
|
||||||
|
setDeleting(true);
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/domains/${domainId}/projects/${projectId}/sections/${existingSection.id}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const err = await response.json();
|
||||||
|
throw new Error(err.error?.message || 'Unable to delete section');
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success('Section deleted');
|
||||||
|
setDeleteOpen(false);
|
||||||
|
onOpenChange(false);
|
||||||
|
onCreated();
|
||||||
|
} catch (err) {
|
||||||
|
toast.error(err instanceof Error ? err.message : 'Unable to delete section');
|
||||||
|
} finally {
|
||||||
|
setDeleting(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
<>
|
||||||
<DialogContent className="sm:max-w-[450px]">
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
<DialogHeader>
|
<DialogContent className="sm:max-w-[450px]">
|
||||||
<DialogTitle>New Section</DialogTitle>
|
<DialogHeader>
|
||||||
<DialogDescription>Add a section or milestone to organize tasks.</DialogDescription>
|
<DialogTitle>{isEdit ? 'Edit Section' : 'New Section'}</DialogTitle>
|
||||||
</DialogHeader>
|
<DialogDescription>
|
||||||
<form className="space-y-4" onSubmit={handleSubmit}>
|
{isEdit ? 'Update this section or milestone.' : 'Add a section or milestone to organize tasks.'}
|
||||||
<div className="space-y-2">
|
</DialogDescription>
|
||||||
<Label htmlFor="section-name">Name *</Label>
|
</DialogHeader>
|
||||||
<Input
|
<form className="space-y-4" onSubmit={handleSubmit}>
|
||||||
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">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="section-kind">Kind</Label>
|
<Label htmlFor="section-name">Name *</Label>
|
||||||
<Select value={kind} onValueChange={(v) => setKind(v as any)}>
|
<Input
|
||||||
<SelectTrigger id="section-kind">
|
id="section-name"
|
||||||
<SelectValue />
|
value={name}
|
||||||
</SelectTrigger>
|
onChange={(e) => setName(e.target.value)}
|
||||||
<SelectContent>
|
placeholder="e.g. Backend, Design, Launch"
|
||||||
<SelectItem value="section">Section</SelectItem>
|
autoFocus
|
||||||
<SelectItem value="milestone">Milestone</SelectItem>
|
required
|
||||||
</SelectContent>
|
/>
|
||||||
</Select>
|
</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>
|
||||||
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="section-status">Status</Label>
|
<Label htmlFor="section-target-date">Target date</Label>
|
||||||
<Select value={status} onValueChange={(v) => setStatus(v as any)}>
|
<Input
|
||||||
<SelectTrigger id="section-status">
|
id="section-target-date"
|
||||||
<SelectValue />
|
type="date"
|
||||||
</SelectTrigger>
|
value={targetDate}
|
||||||
<SelectContent>
|
onChange={(e) => setTargetDate(e.target.value)}
|
||||||
<SelectItem value="planned">Planned</SelectItem>
|
/>
|
||||||
<SelectItem value="in_progress">In Progress</SelectItem>
|
|
||||||
<SelectItem value="complete">Complete</SelectItem>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
{error && <p className="text-sm text-destructive" role="alert">{error}</p>}
|
||||||
<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 className="flex items-center justify-between sm:justify-between">
|
||||||
|
{isEdit && (
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="destructive"
|
||||||
|
onClick={() => setDeleteOpen(true)}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button type="submit" disabled={submitting || !name}>
|
||||||
|
{submitting ? (isEdit ? 'Saving...' : 'Creating...') : (isEdit ? 'Save' : 'Create Section')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</DialogFooter>
|
||||||
|
</form>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
<DialogFooter>
|
<AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}>
|
||||||
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
|
<AlertDialogContent>
|
||||||
Cancel
|
<AlertDialogHeader>
|
||||||
</Button>
|
<AlertDialogTitle>Delete Section</AlertDialogTitle>
|
||||||
<Button type="submit" disabled={submitting || !name}>
|
<AlertDialogDescription>
|
||||||
{submitting ? 'Creating...' : 'Create Section'}
|
Are you sure you want to delete "{existingSection?.name}"? This action cannot be undone.
|
||||||
</Button>
|
</AlertDialogDescription>
|
||||||
</DialogFooter>
|
</AlertDialogHeader>
|
||||||
</form>
|
<AlertDialogFooter>
|
||||||
</DialogContent>
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
</Dialog>
|
<AlertDialogAction onClick={handleDelete} disabled={deleting}>
|
||||||
|
{deleting ? 'Deleting...' : 'Delete'}
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user