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

@@ -4,170 +4,179 @@ import { useCallback, useEffect, useState } from "react";
import { useParams } from "next/navigation";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "~/components/ui/card";
import { Button } from "~/components/ui/button";
import {
Users,
FileText,
BarChart,
PlayCircle,
Plus,
Settings2
} from "lucide-react";
import { useToast } from "~/hooks/use-toast";
import { Plus, Users, FileText, BarChart, PlayCircle } from "lucide-react";
import Link from "next/link";
import { useActiveStudy } from "~/context/active-study";
import { getApiUrl } from "~/lib/fetch-utils";
import { Skeleton } from "~/components/ui/skeleton";
interface StudyStats {
participantCount: number;
completedTrialsCount: number;
pendingFormsCount: number;
formCount: number;
trialCount: number;
}
export default function StudyDashboard() {
const [stats, setStats] = useState<StudyStats>({
participantCount: 0,
completedTrialsCount: 0,
pendingFormsCount: 0,
formCount: 0,
trialCount: 0,
});
const [loading, setLoading] = useState(true);
const [isLoading, setIsLoading] = useState(true);
const { id } = useParams();
const { toast } = useToast();
const { activeStudy } = useActiveStudy();
const fetchStudyStats = useCallback(async () => {
const fetchStats = useCallback(async () => {
try {
const response = await fetch(getApiUrl(`/api/studies/${id}/stats`));
if (!response.ok) throw new Error("Failed to fetch study statistics");
const data = await response.json();
setStats(data.data);
if (!response.ok) throw new Error("Failed to fetch stats");
const { data } = await response.json();
setStats({
participantCount: data?.participantCount ?? 0,
formCount: data?.formCount ?? 0,
trialCount: data?.trialCount ?? 0
});
} catch (error) {
console.error("Error fetching study stats:", error);
console.error("Error fetching stats:", error);
toast({
title: "Error",
description: "Failed to load study statistics",
variant: "destructive",
});
// Set default values on error
setStats({
participantCount: 0,
formCount: 0,
trialCount: 0
});
} finally {
setLoading(false);
setIsLoading(false);
}
}, [toast, id]);
useEffect(() => {
fetchStudyStats();
}, [fetchStudyStats]);
fetchStats();
}, [fetchStats]);
if (loading) {
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-[400px]">
<div className="animate-spin h-8 w-8 border-4 border-primary border-t-transparent rounded-full" />
<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>
</div>
<div className="grid gap-4 grid-cols-1 md:grid-cols-3">
{[1, 2, 3].map((i) => (
<Card key={i}>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<Skeleton className="h-4 w-[100px]" />
<Skeleton className="h-4 w-4" />
</CardHeader>
<CardContent>
<Skeleton className="h-7 w-[50px] mb-1" />
<Skeleton className="h-3 w-[120px]" />
</CardContent>
</Card>
))}
</div>
<Card>
<CardHeader>
<Skeleton className="h-5 w-[120px] mb-2" />
<Skeleton className="h-4 w-[200px]" />
</CardHeader>
<CardContent className="flex gap-4">
<Skeleton className="h-10 w-[140px]" />
<Skeleton className="h-10 w-[120px]" />
</CardContent>
</Card>
</div>
);
}
return (
<div className="space-y-6">
<div className="grid gap-4 md:grid-cols-3">
<div className="flex items-center justify-between">
<div>
<h2 className="text-2xl font-bold tracking-tight">{activeStudy?.title}</h2>
<p className="text-muted-foreground">
Overview of your study&apos;s progress and statistics
</p>
</div>
</div>
<div className="grid gap-4 grid-cols-1 md:grid-cols-3">
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Participants</CardTitle>
<CardTitle className="text-sm font-medium">
Participants
</CardTitle>
<Users className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{stats.participantCount}</div>
<p className="text-xs text-muted-foreground">Total participants enrolled</p>
<p className="text-xs text-muted-foreground">
Total enrolled participants
</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Completed Trials</CardTitle>
<PlayCircle className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{stats.completedTrialsCount}</div>
<p className="text-xs text-muted-foreground">Successfully completed trials</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Pending Forms</CardTitle>
<CardTitle className="text-sm font-medium">
Forms
</CardTitle>
<FileText className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{stats.pendingFormsCount}</div>
<p className="text-xs text-muted-foreground">Forms awaiting completion</p>
</CardContent>
</Card>
</div>
<div className="grid gap-4 md:grid-cols-2">
<Card>
<CardHeader>
<CardTitle>Quick Actions</CardTitle>
<CardDescription>Common tasks for this study</CardDescription>
</CardHeader>
<CardContent className="space-y-2">
<Button
variant="outline"
className="w-full justify-start"
asChild
>
<Link href={`/dashboard/studies/${id}/participants/new`}>
<Plus className="mr-2 h-4 w-4" />
Add Participant
</Link>
</Button>
<Button
variant="outline"
className="w-full justify-start"
asChild
>
<Link href={`/dashboard/studies/${id}/trials/new`}>
<PlayCircle className="mr-2 h-4 w-4" />
Start New Trial
</Link>
</Button>
<Button
variant="outline"
className="w-full justify-start"
asChild
>
<Link href={`/dashboard/studies/${id}/forms/new`}>
<FileText className="mr-2 h-4 w-4" />
Create Form
</Link>
</Button>
<div className="text-2xl font-bold">{stats.formCount}</div>
<p className="text-xs text-muted-foreground">
Active study forms
</p>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Recent Activity</CardTitle>
<CardDescription>Latest updates and changes</CardDescription>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">
Trials
</CardTitle>
<BarChart className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent className="space-y-2">
<Button
variant="outline"
className="w-full justify-start"
asChild
>
<Link href={`/dashboard/studies/${id}/analysis`}>
<BarChart className="mr-2 h-4 w-4" />
View Analytics
</Link>
</Button>
<Button
variant="outline"
className="w-full justify-start"
asChild
>
<Link href={`/dashboard/studies/${id}/settings`}>
<Settings2 className="mr-2 h-4 w-4" />
Study Settings
</Link>
</Button>
<CardContent>
<div className="text-2xl font-bold">{stats.trialCount}</div>
<p className="text-xs text-muted-foreground">
Completed trials
</p>
</CardContent>
</Card>
</div>
<Card>
<CardHeader>
<CardTitle>Quick Actions</CardTitle>
<CardDescription>Common tasks and actions for this study</CardDescription>
</CardHeader>
<CardContent className="flex gap-4">
<Button asChild>
<Link href={`/dashboard/studies/${id}/participants/new`}>
<Plus className="w-4 h-4 mr-2" />
Add Participant
</Link>
</Button>
<Button variant="outline" asChild>
<Link href={`/dashboard/studies/${id}/trials/new`}>
<PlayCircle className="w-4 h-4 mr-2" />
Start Trial
</Link>
</Button>
</CardContent>
</Card>
</div>
);
}