Skip to content

Commit

Permalink
add unstyled teacher profile page with gauth info and x/10 absences
Browse files Browse the repository at this point in the history
  • Loading branch information
135ze committed Sep 28, 2024
1 parent ec030ee commit c6555f1
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/api/users/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { PrismaClient } from '@prisma/client';
import { NextRequest, NextResponse } from 'next/server';

const prisma = new PrismaClient();

export async function GET(req: NextRequest, params) {
// GETABSENCES
const { searchParams } = new URL(req.url);
const encodedAbsences = searchParams.get('getAbsences');
let getAbsences = false;
if (encodedAbsences) {
getAbsences = Boolean(decodeURIComponent(encodedAbsences));
}

// ID
if (!params || !params.params || !params.params.id) {
return new NextResponse('Invalid id provided', { status: 400 });
}

const id = params.params.id;

if (Number.isNaN(id)) {
return new NextResponse('ID not a number', { status: 400 });
}

try {
const user = await prisma.user.findUnique({
where: { id },
include: { absences: getAbsences },
});

if (!user) {
return new NextResponse('User not found', { status: 400 });
} else {
return new NextResponse(JSON.stringify(user), { status: 200 });
}
} catch (error) {
return new NextResponse('Internal server error', { status: 500 });
}
}
56 changes: 56 additions & 0 deletions app/api/users/email/[email]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { PrismaClient } from '@prisma/client';
import { NextRequest, NextResponse } from 'next/server';

const prisma = new PrismaClient();

export async function GET(req: NextRequest, params) {
// GETABSENCES
const { searchParams } = new URL(req.url);
const encodedAbsences = searchParams.get('getAbsences');
let getAbsences = false;
if (encodedAbsences) {
getAbsences = Boolean(decodeURIComponent(encodedAbsences));
}

// EMAIL
if (!params || !params.params || !params.params.email) {
return new NextResponse('Invalid email provided', { status: 400 });
}

// TODO: rename realEmail to email to be used in the findUnique later
const realEmail = params.params.email;

try {
// TODO: remove once emails are set up
// fake stuff begins
// find random email in db for now
const useFake = true;
const fakeEmailReq = await prisma.user.findFirst({
select: {
email: true,
},
});
let email;
if (!fakeEmailReq) {
return new NextResponse('DEV ERROR: local db not seeded!', {
status: 400,
});
} else {
email = useFake ? fakeEmailReq.email : realEmail;
}
// fake stuff ends

const user = await prisma.user.findUnique({
where: { email },
include: { absences: getAbsences },
});

if (!user) {
return new NextResponse('User not found', { status: 400 });
} else {
return new NextResponse(JSON.stringify(user), { status: 200 });
}
} catch (error) {
return new NextResponse('Internal server error', { status: 500 });
}
}
9 changes: 9 additions & 0 deletions app/api/users/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PrismaClient } from '@prisma/client';
import { NextResponse } from 'next/server';

const prisma = new PrismaClient();

export async function GET() {
const users = await prisma.user.findMany();
return NextResponse.json(users);
}
63 changes: 63 additions & 0 deletions src/pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useSession } from 'next-auth/react';
import { useEffect, useState } from 'react';
import { SignOutButton } from '../components/SignOutButton';

export default function AnotherPage() {
const { data: session, status } = useSession();
const [numAbsences, setNumAbsences] = useState(null);
const [usedAbsences, setUsedAbsences] = useState(null);

useEffect(() => {
const fetchUserInfoByEmail = async () => {
if (!session || !session.user || !session.user.email) return;

const email = session.user.email;
const apiUrl = `/api/users/email/${email}?getAbsences=true`;

try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
setNumAbsences(data['numOfAbsences']);
setUsedAbsences(data['absences'].length);
} catch (error) {
console.error('Error fetching data:', error);
}
};

fetchUserInfoByEmail();
}, [session]);

return (
<div>
<h1>Profile</h1>
<hr />
{status === 'loading' ? (
<p>Loading...</p>
) : session && session.user ? (
<div>
<h2>Personal Information</h2>
<p>Name: {session.user.name}</p>
<p>Email: {session.user.email}</p>
<p>Image:</p>
{session.user.image && (
<img src={session.user.image} alt="User Image" />
)}

<hr />
<h2>Metrics</h2>
<p>
Absences: {usedAbsences} / {numAbsences}{' '}
</p>
<hr />
</div>
) : (
<p>Error: Signed out</p>
)}
<hr />
<SignOutButton />
</div>
);
}

0 comments on commit c6555f1

Please sign in to comment.