feat: Enhance user management and UI components

- Updated user API routes to include imageUrl for better user representation.
- Added @radix-ui/react-toast dependency for improved user notifications.
- Refactored dashboard and studies components to incorporate new user fields and loading states.
- Enhanced the layout and structure of the dashboard, studies, and participants pages for better user experience.
- Implemented a dialog for adding participants, improving the participant management workflow.
- Updated breadcrumb navigation to reflect the current study context more accurately.
- Cleaned up unused imports and optimized component rendering for performance.
This commit is contained in:
2024-12-05 13:21:33 -05:00
parent 207f4d7fb8
commit 80171b2d70
17 changed files with 712 additions and 454 deletions

View File

@@ -6,7 +6,6 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "~/com
import { Button } from "~/components/ui/button";
import { useToast } from "~/hooks/use-toast";
import { Plus, Trash2 } from "lucide-react";
import Link from "next/link";
import { useActiveStudy } from "~/context/active-study";
import { hasPermission } from "~/lib/permissions-client";
import { PERMISSIONS } from "~/lib/permissions";
@@ -21,6 +20,15 @@ import {
AlertDialogTitle,
AlertDialogTrigger,
} from "~/components/ui/alert-dialog";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "~/components/ui/dialog";
import {
Table,
TableBody,
@@ -30,6 +38,10 @@ import {
TableRow,
} from "~/components/ui/table";
import { getApiUrl } from "~/lib/fetch-utils";
import { Skeleton } from "~/components/ui/skeleton";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
interface Participant {
id: number;
name: string;
@@ -40,6 +52,8 @@ interface Participant {
export default function ParticipantsList() {
const [participants, setParticipants] = useState<Participant[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [isAddingParticipant, setIsAddingParticipant] = useState(false);
const [newParticipantName, setNewParticipantName] = useState("");
const { id } = useParams();
const { toast } = useToast();
const { activeStudy } = useActiveStudy();
@@ -50,13 +64,7 @@ export default function ParticipantsList() {
const fetchParticipants = useCallback(async () => {
try {
const response = await fetch(getApiUrl(`/api/studies/${id}/participants`), {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const response = await fetch(getApiUrl(`/api/studies/${id}/participants`));
if (!response.ok) throw new Error("Failed to fetch participants");
const data = await response.json();
setParticipants(data.data || []);
@@ -70,7 +78,7 @@ export default function ParticipantsList() {
} finally {
setIsLoading(false);
}
}, [toast, id]);
}, [id, toast]);
useEffect(() => {
fetchParticipants();
@@ -103,13 +111,82 @@ export default function ParticipantsList() {
}
};
const handleAddParticipant = async () => {
if (!newParticipantName.trim()) return;
setIsAddingParticipant(true);
try {
const response = await fetch(getApiUrl(`/api/studies/${id}/participants`), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name: newParticipantName }),
});
if (!response.ok) throw new Error("Failed to add participant");
const data = await response.json();
setParticipants([...participants, data.data]);
setNewParticipantName("");
toast({
title: "Success",
description: "Participant added successfully",
});
} catch (error) {
console.error("Error adding participant:", error);
toast({
title: "Error",
description: "Failed to add participant",
variant: "destructive",
});
} finally {
setIsAddingParticipant(false);
}
};
if (isLoading) {
return (
<Card>
<CardContent className="py-8">
<p className="text-center text-muted-foreground">Loading participants...</p>
</CardContent>
</Card>
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<Skeleton className="h-8 w-[200px] mb-2" />
<Skeleton className="h-4 w-[300px]" />
</div>
<Skeleton className="h-10 w-[140px]" />
</div>
<Card>
<CardHeader>
<Skeleton className="h-5 w-[150px] mb-2" />
<Skeleton className="h-4 w-[250px]" />
</CardHeader>
<CardContent>
<div className="rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead><Skeleton className="h-4 w-[40px]" /></TableHead>
<TableHead><Skeleton className="h-4 w-[120px]" /></TableHead>
<TableHead><Skeleton className="h-4 w-[100px]" /></TableHead>
<TableHead className="w-[100px]"><Skeleton className="h-4 w-[60px]" /></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{[1, 2, 3].map((i) => (
<TableRow key={i}>
<TableCell><Skeleton className="h-4 w-[30px]" /></TableCell>
<TableCell><Skeleton className="h-4 w-[150px]" /></TableCell>
<TableCell><Skeleton className="h-4 w-[100px]" /></TableCell>
<TableCell><Skeleton className="h-8 w-8" /></TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</CardContent>
</Card>
</div>
);
}
@@ -123,12 +200,41 @@ export default function ParticipantsList() {
</p>
</div>
{canCreateParticipant && (
<Button asChild>
<Link href={`/dashboard/studies/${id}/participants/new`}>
<Plus className="mr-2 h-4 w-4" />
Add Participant
</Link>
</Button>
<Dialog>
<DialogTrigger asChild>
<Button>
<Plus className="mr-2 h-4 w-4" />
Add Participant
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Add Participant</DialogTitle>
<DialogDescription>
Add a new participant to {activeStudy?.title}
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label htmlFor="name">Participant Name</Label>
<Input
id="name"
placeholder="Enter participant name"
value={newParticipantName}
onChange={(e) => setNewParticipantName(e.target.value)}
/>
</div>
</div>
<DialogFooter>
<Button
onClick={handleAddParticipant}
disabled={isAddingParticipant || !newParticipantName.trim()}
>
{isAddingParticipant ? "Adding..." : "Add Participant"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)}
</div>
@@ -198,12 +304,40 @@ export default function ParticipantsList() {
{canCreateParticipant && (
<>
.{" "}
<Link
href={`/dashboard/studies/${id}/participants/new`}
className="font-medium text-primary hover:underline"
>
Add your first participant
</Link>
<Dialog>
<DialogTrigger asChild>
<Button variant="link" className="px-2 py-0">
Add your first participant
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Add Participant</DialogTitle>
<DialogDescription>
Add a new participant to {activeStudy?.title}
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label htmlFor="name-empty">Participant Name</Label>
<Input
id="name-empty"
placeholder="Enter participant name"
value={newParticipantName}
onChange={(e) => setNewParticipantName(e.target.value)}
/>
</div>
</div>
<DialogFooter>
<Button
onClick={handleAddParticipant}
disabled={isAddingParticipant || !newParticipantName.trim()}
>
{isAddingParticipant ? "Adding..." : "Add Participant"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
)}
</div>