Clean codebase- start from scratch

This commit is contained in:
2024-11-19 22:14:54 -05:00
parent 9d9aa52285
commit b4a05e0bcd
97 changed files with 6376 additions and 3624 deletions

View File

@@ -0,0 +1,24 @@
import { Sidebar } from "~/components/sidebar";
import { cn } from "~/lib/utils";
import { StudyProvider } from "~/context/StudyContext";
export default function DashboardLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<StudyProvider>
<div className="flex h-screen overflow-hidden">
<Sidebar />
<main className={cn(
"flex-1 overflow-y-auto",
"lg:pt-8 p-8",
"pt-[calc(3.5rem+2rem)]"
)}>
{children}
</main>
</div>
</StudyProvider>
);
}

View File

@@ -0,0 +1,8 @@
export default function Dashboard() {
return (
<div>
<p>Dashboard</p>
</div>
);
}

View File

@@ -0,0 +1,174 @@
'use client';
import { useEffect, useState } from "react";
import { Button } from "~/components/ui/button";
import { Card, CardHeader, CardTitle, CardContent } from "~/components/ui/card";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
import { PlusIcon, Trash2Icon } from "lucide-react";
import {
Select,
SelectTrigger,
SelectValue,
SelectContent,
SelectItem,
SelectLabel,
} from "~/components/ui/select";
interface Study {
id: number;
title: string;
}
interface Participant {
id: number;
name: string;
studyId: number;
}
export default function Participants() {
const [studies, setStudies] = useState<Study[]>([]);
const [participants, setParticipants] = useState<Participant[]>([]);
const [selectedStudyId, setSelectedStudyId] = useState<number | null>(null);
const [participantName, setParticipantName] = useState("");
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchStudies();
}, []);
const fetchStudies = async () => {
try {
const response = await fetch('/api/studies');
const data = await response.json();
setStudies(data);
} catch (error) {
console.error('Error fetching studies:', error);
} finally {
setLoading(false);
}
};
const fetchParticipants = async (studyId: number) => {
try {
const response = await fetch(`/api/participants?studyId=${studyId}`);
const data = await response.json();
setParticipants(data);
} catch (error) {
console.error('Error fetching participants:', error);
}
};
const handleStudyChange = (studyId: string) => {
const id = parseInt(studyId); // Convert the string to a number
setSelectedStudyId(id);
fetchParticipants(id);
};
const addParticipant = async (e: React.FormEvent) => {
e.preventDefault();
if (!selectedStudyId) return;
try {
const response = await fetch(`/api/participants`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: participantName,
studyId: selectedStudyId,
}),
});
if (response.ok) {
const newParticipant = await response.json();
setParticipants([...participants, newParticipant]);
setParticipantName("");
} else {
console.error('Error adding participant:', response.statusText);
}
} catch (error) {
console.error('Error adding participant:', error);
}
};
const deleteParticipant = async (id: number) => {
try {
const response = await fetch(`/api/participants/${id}`, {
method: 'DELETE',
});
if (response.ok) {
setParticipants(participants.filter(participant => participant.id !== id));
} else {
console.error('Error deleting participant:', response.statusText);
}
} catch (error) {
console.error('Error deleting participant:', error);
}
};
if (loading) {
return <div>Loading...</div>;
}
return (
<div className="max-w-4xl mx-auto">
<h1 className="text-3xl font-bold mb-4">Manage Participants</h1>
<div className="mb-4">
<Label htmlFor="study">Select Study</Label>
<Select onValueChange={handleStudyChange}>
<SelectTrigger>
<SelectValue placeholder="Select a study" />
</SelectTrigger>
<SelectContent>
{studies.map((study) => (
<SelectItem key={study.id} value={study.id.toString()}>
{study.title}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<Card>
<CardHeader>
<CardTitle>Add New Participant</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={addParticipant} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="name">Participant Name</Label>
<Input
type="text"
id="name"
value={participantName}
onChange={(e) => setParticipantName(e.target.value)}
required
/>
</div>
<Button type="submit" disabled={!selectedStudyId}>
<PlusIcon className="w-4 h-4 mr-2" />
Add Participant
</Button>
</form>
</CardContent>
</Card>
<div className="mt-4">
<h2 className="text-xl font-semibold">Participant List</h2>
<ul>
{participants.map((participant) => (
<li key={participant.id} className="flex justify-between items-center">
<span>{participant.name}</span>
<Button onClick={() => deleteParticipant(participant.id)} variant="destructive">
<Trash2Icon className="w-4 h-4" />
</Button>
</li>
))}
</ul>
</div>
</div>
);
}

View File

@@ -0,0 +1,152 @@
'use client';
import { useEffect, useState } from "react";
import { Button } from "~/components/ui/button";
import { PlusIcon, Trash2Icon } from "lucide-react";
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
CardFooter
} from "~/components/ui/card";
import { Input } from "~/components/ui/input";
import { Textarea } from "~/components/ui/textarea";
import { Label } from "~/components/ui/label";
interface Study {
id: number;
title: string;
description: string | null;
createdAt: string;
}
export default function Studies() {
const [studies, setStudies] = useState<Study[]>([]);
const [loading, setLoading] = useState(true);
const [newStudyTitle, setNewStudyTitle] = useState("");
const [newStudyDescription, setNewStudyDescription] = useState("");
useEffect(() => {
fetchStudies();
}, []);
const fetchStudies = async () => {
try {
const response = await fetch('/api/studies');
const data = await response.json();
setStudies(data);
} catch (error) {
console.error('Error fetching studies:', error);
} finally {
setLoading(false);
}
};
const createStudy = async (e: React.FormEvent) => {
e.preventDefault();
try {
const response = await fetch('/api/studies', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: newStudyTitle,
description: newStudyDescription,
}),
});
const newStudy = await response.json();
setStudies([...studies, newStudy]);
setNewStudyTitle("");
setNewStudyDescription("");
} catch (error) {
console.error('Error creating study:', error);
}
};
const deleteStudy = async (id: number) => {
try {
await fetch(`/api/studies/${id}`, {
method: 'DELETE',
});
setStudies(studies.filter(study => study.id !== id));
} catch (error) {
console.error('Error deleting study:', error);
}
};
if (loading) {
return <div>Loading...</div>;
}
return (
<div className="max-w-4xl mx-auto">
<div className="flex justify-between items-center mb-8">
<h1 className="text-3xl font-bold">Studies</h1>
</div>
<Card className="mb-8">
<CardHeader>
<CardTitle>Create New Study</CardTitle>
<CardDescription>
Add a new research study to your collection
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={createStudy} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="title">Study Title</Label>
<Input
type="text"
id="title"
value={newStudyTitle}
onChange={(e) => setNewStudyTitle(e.target.value)}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="description">Description</Label>
<Textarea
id="description"
value={newStudyDescription}
onChange={(e) => setNewStudyDescription(e.target.value)}
rows={3}
/>
</div>
<Button type="submit">
<PlusIcon className="w-4 h-4 mr-2" />
Create Study
</Button>
</form>
</CardContent>
</Card>
<div className="grid gap-4">
{studies.map((study) => (
<Card key={study.id}>
<CardHeader>
<div className="flex justify-between items-start">
<div>
<CardTitle>{study.title}</CardTitle>
{study.description && (
<CardDescription className="mt-1.5">
{study.description}
</CardDescription>
)}
</div>
<Button variant="ghost" size="icon" className="text-destructive" onClick={() => deleteStudy(study.id)}>
<Trash2Icon className="w-4 h-4" />
</Button>
</div>
</CardHeader>
<CardFooter className="text-sm text-muted-foreground">
Created: {new Date(study.createdAt).toLocaleDateString()}
</CardFooter>
</Card>
))}
</div>
</div>
);
}