-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unstyled teacher profile page with gauth info and x/10 absences
- Loading branch information
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |