generated from hack4impact-upenn/boilerplate-s2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from hack4impact-upenn/joseph-and-caroline/stud…
…ent-routes Teacher Homepage
- Loading branch information
Showing
11 changed files
with
308 additions
and
4 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
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,89 @@ | ||
import React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import Paper from '@mui/material/Paper'; | ||
import { styled } from '@mui/system'; | ||
// eslint-disable-next-line | ||
import { useData } from './util/api'; | ||
import StudentCard from './components/buttons/StudentCard'; | ||
import PageHeader from './components/PageHeader'; | ||
|
||
const ScrollableBox = styled(Box)({ | ||
overflowY: 'auto', | ||
maxHeight: '100%', | ||
}); | ||
|
||
// // eslint-disable-next-line | ||
// function createStudentCard(student: any) { | ||
// const id = student.user_id; | ||
// console.log(id) | ||
// const user = useData(`users/${id}`); | ||
// console.log(user) | ||
// if (user) { | ||
// const info = user.data; | ||
// const name = `${info.firstName} ${info.lastName}`; | ||
// const lesson = student.lesson_level; | ||
// return <StudentCard studentName={name} lesson={lesson} />; | ||
// } | ||
// } | ||
|
||
// eslint-disable-next-line | ||
function createData(data: any) { | ||
return data.map((student: any) => { | ||
return <StudentCard studentID={student.user_id} lesson="Lesson 1" />; | ||
}); | ||
} | ||
|
||
function SplitGrid() { | ||
const id = '111'; | ||
const students = useData(`student/teacher/${id}`); | ||
const studentData = students?.data ?? []; | ||
|
||
// const student_users = [] | ||
// for (let i = 0; i < studentData.length; i++) { | ||
// const user_id = studentData[i].user_id; | ||
// const user = useData(`users/${id}`); | ||
// student_users.push(user); | ||
// } | ||
|
||
console.log(studentData); | ||
|
||
return ( | ||
<Box display="flex" flexDirection="column" width="100%" height="100vh"> | ||
<PageHeader /> | ||
|
||
<Box display="flex" flexGrow={5}> | ||
<Paper | ||
sx={{ | ||
width: '30%', | ||
overflowY: 'auto', | ||
maxHeight: 'calc(100vh - 64px)', // Subtract the Toolbar height (default is 64px) | ||
bgcolor: 'white', | ||
p: 2, | ||
}} | ||
elevation={0} | ||
square | ||
> | ||
<h2>Students</h2> | ||
{createData(studentData)} | ||
</Paper> | ||
|
||
<Paper | ||
sx={{ | ||
width: '70%', | ||
overflowY: 'auto', | ||
maxHeight: 'calc(100vh - 64px)', // Subtract the Toolbar height (default is 64px) | ||
bgcolor: '#EDEDED', | ||
p: 2, | ||
paddingX: 4, | ||
}} | ||
elevation={0} | ||
square | ||
> | ||
<h2>Class Progress</h2> | ||
</Paper> | ||
</Box> | ||
</Box> | ||
); | ||
} | ||
|
||
export default SplitGrid; |
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,52 @@ | ||
/* eslint-disable react/jsx-no-bind */ | ||
import { CardActionArea, CardContent, Typography, Card } from '@mui/material'; | ||
import React, { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { useData } from '../../util/api'; | ||
|
||
type StudentCardProps = { | ||
studentID: string; | ||
lesson: string; | ||
}; | ||
|
||
function StudentCard({ studentID, lesson }: StudentCardProps) { | ||
const navigate = useNavigate(); | ||
console.log(studentID); | ||
const user = useData(`user/${studentID}`); | ||
console.log(user); | ||
let label = 'Name'; | ||
if (user) { | ||
const info = user.data; | ||
const name = `${info.firstName} ${info.lastName}`; | ||
label = name; | ||
} | ||
|
||
function handleClick() { | ||
// const s = `/city-dashboard/${cityName}`; | ||
// navigate(s); | ||
} | ||
|
||
return ( | ||
<Card sx={{ p: 2, bgcolor: '#EDEDED', mb: 2, borderRadius: '8px' }}> | ||
<CardActionArea onClick={handleClick}> | ||
<CardContent> | ||
<Typography variant="h5">{label}</Typography> | ||
<Typography | ||
sx={{ | ||
color: '#0175C0', | ||
display: 'flex', | ||
alignItems: 'center', | ||
}} | ||
variant="subtitle1" | ||
> | ||
<b> | ||
<i>{lesson}</i> | ||
</b> | ||
</Typography> | ||
</CardContent> | ||
</CardActionArea> | ||
</Card> | ||
); | ||
} | ||
|
||
export default StudentCard; |
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,39 @@ | ||
/** | ||
* All the controller functions containing the logic for routes relating to | ||
* admin users such as getting all users, deleting users and upgrading users. | ||
*/ | ||
import express from 'express'; | ||
import ApiError from '../util/apiError'; | ||
import StatusCode from '../util/statusCode'; | ||
import { getAllStudentsFromDB } from '../services/student.service'; | ||
|
||
/** | ||
* Get students by teacher_id | ||
*/ | ||
const getStudentsFromTeacherId = async ( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
) => { | ||
const { id } = req.params; | ||
|
||
if (!id) { | ||
next(ApiError.internal('Request must include a valid teacher_id param')); | ||
} | ||
|
||
return ( | ||
getAllStudentsFromDB() | ||
.then((studentList) => { | ||
return studentList.filter((student) => student.teacher_id === id); | ||
}) | ||
.then((filteredList) => { | ||
res.status(StatusCode.OK).send(filteredList); | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
.catch((e) => { | ||
next(ApiError.internal('Unable to retrieve all users')); | ||
}) | ||
); | ||
}; | ||
|
||
export { getStudentsFromTeacherId }; |
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,33 @@ | ||
/** | ||
* All the controller functions containing the logic for routes relating to | ||
* admin users such as getting all users, deleting users and upgrading users. | ||
*/ | ||
import express from 'express'; | ||
import ApiError from '../util/apiError'; | ||
import StatusCode from '../util/statusCode'; | ||
import { getUserById } from '../services/user.service'; | ||
|
||
// get a specific student | ||
const getUser = async ( | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
) => { | ||
const { id } = req.params; | ||
if (!id) { | ||
next(ApiError.internal('Request must include a valid userID param')); | ||
} | ||
|
||
return ( | ||
getUserById(id) | ||
.then((user) => { | ||
res.status(StatusCode.OK).send(user); | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
.catch((e) => { | ||
next(ApiError.internal('Unable to retrieve specified student')); | ||
}) | ||
); | ||
}; | ||
|
||
export { getUser }; |
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
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
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,19 @@ | ||
/** | ||
* Specifies the middleware and controller functions to call for each route | ||
* relating to admin users. | ||
*/ | ||
import express from 'express'; | ||
import { isAdmin } from '../controllers/admin.middleware'; | ||
import { getStudentsFromTeacherId } from '../controllers/student.controller'; | ||
import { isAuthenticated } from '../controllers/auth.middleware'; | ||
import 'dotenv/config'; | ||
|
||
const router = express.Router(); | ||
|
||
/** | ||
* A GET route to get all users. Checks first if the requestor is a | ||
* authenticated and is an admin. | ||
*/ | ||
router.get('/teacher/:id', isAuthenticated, isAdmin, getStudentsFromTeacherId); | ||
|
||
export default router; |
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
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,18 @@ | ||
/** | ||
* Specifies the middleware and controller functions to call for each route | ||
* relating to admin users. | ||
*/ | ||
import express from 'express'; | ||
import { isAdmin } from '../controllers/admin.middleware'; | ||
import { getUser } from '../controllers/user.controller'; | ||
import { isAuthenticated } from '../controllers/auth.middleware'; | ||
import 'dotenv/config'; | ||
|
||
const router = express.Router(); | ||
|
||
/** | ||
* A GET route to get a user by their ID | ||
*/ | ||
router.get('/:id', isAuthenticated, isAdmin, getUser); | ||
|
||
export default router; |
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,14 @@ | ||
/** | ||
* All the functions for interacting with user data in the MongoDB database | ||
*/ | ||
import { Student } from '../models/student.model'; | ||
|
||
/** | ||
* @returns All the {@link Student}s in the database. | ||
*/ | ||
const getAllStudentsFromDB = async () => { | ||
const studentList = await Student.find({}).exec(); | ||
return studentList; | ||
}; | ||
|
||
export { getAllStudentsFromDB }; |