55
66import { useState , useEffect } from "react" ;
77import { setUsername as storeUsername , clearSession , onAuthFailure , getMe , getCohorts , getStudentsByCohort } from "./api/client" ;
8- import type { Student } from "./api/client" ;
8+ import type { Student , Cohort } from "./api/client" ;
99import { authClient } from "./lib/auth-client" ;
1010import { Login } from "./components/Login" ;
1111import { MessageFeed } from "./components/MessageFeed" ;
@@ -27,14 +27,21 @@ function App() {
2727 const [ username , setUsername ] = useState < string | null > ( null ) ;
2828 const [ role , setRole ] = useState < "instructor" | "student" | null > ( null ) ;
2929 const [ sessionInvalid , setSessionInvalid ] = useState ( false ) ;
30- const [ impersonating , setImpersonating ] = useState < { discordId : string ; name : string } | null > ( null ) ;
31- const [ impersonateStudents , setImpersonateStudents ] = useState < { discordId : string ; name : string } [ ] > ( [ ] ) ;
30+ const [ impersonating , setImpersonating ] = useState < { discordId : string ; name : string ; cohortId : number } | null > ( null ) ;
31+ const [ impersonateStudents , setImpersonateStudents ] = useState < { discordId : string ; name : string ; cohortId : number } [ ] > ( [ ] ) ;
32+ const [ cohorts , setCohorts ] = useState < Cohort [ ] > ( [ ] ) ;
33+ const [ studentCohortId , setStudentCohortId ] = useState < number | null > ( null ) ;
3234
3335 /** Fetches the user's role and identity after login. */
3436 const fetchRole = async ( ) => {
3537 const me = await getMe ( ) ;
3638 if ( me ) {
3739 setRole ( me . role ) ;
40+ if ( me . role === "student" && me . cohortId ) {
41+ setStudentCohortId ( me . cohortId ) ;
42+ const allCohorts = await getCohorts ( ) ;
43+ setCohorts ( allCohorts ) ;
44+ }
3845 }
3946 } ;
4047
@@ -62,15 +69,16 @@ function App() {
6269 useEffect ( ( ) => {
6370 if ( role !== "instructor" ) return ;
6471 ( async ( ) => {
65- const cohorts = await getCohorts ( ) ;
72+ const allCohorts = await getCohorts ( ) ;
73+ setCohorts ( allCohorts ) ;
6674 const allStudents : Student [ ] = [ ] ;
67- for ( const cohort of cohorts ) {
75+ for ( const cohort of allCohorts ) {
6876 const students = await getStudentsByCohort ( cohort . id ) ;
6977 allStudents . push ( ...students ) ;
7078 }
7179 const withDiscord = allStudents
7280 . filter ( ( s ) => s . discordUserId )
73- . map ( ( s ) => ( { discordId : s . discordUserId ! , name : s . name } ) )
81+ . map ( ( s ) => ( { discordId : s . discordUserId ! , name : s . name , cohortId : s . cohortId } ) )
7482 . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
7583 setImpersonateStudents ( withDiscord ) ;
7684 } ) ( ) ;
@@ -106,11 +114,14 @@ function App() {
106114
107115 // Student portal
108116 if ( role === "student" ) {
117+ const cohort = cohorts . find ( ( c ) => c . id === studentCohortId ) ;
109118 return (
110119 < StudentPortal
111120 username = { username }
112121 sessionInvalid = { sessionInvalid }
113122 onLogout = { handleLogout }
123+ cohortStartDate = { cohort ?. startDate ?? undefined }
124+ cohortEndDate = { cohort ?. endDate ?? undefined }
114125 />
115126 ) ;
116127 }
@@ -179,6 +190,8 @@ function App() {
179190 sessionInvalid = { sessionInvalid }
180191 onLogout = { handleLogout }
181192 studentDiscordId = { impersonating . discordId }
193+ cohortStartDate = { cohorts . find ( ( c ) => c . id === impersonating . cohortId ) ?. startDate ?? undefined }
194+ cohortEndDate = { cohorts . find ( ( c ) => c . id === impersonating . cohortId ) ?. endDate ?? undefined }
182195 />
183196 ) : (
184197 < >
0 commit comments