-
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy path[id].js
More file actions
143 lines (128 loc) · 4.48 KB
/
Copy path[id].js
File metadata and controls
143 lines (128 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import Head from 'next/head';
import Layout from '../../../components/layout';
import Link from 'next/link';
import Navbar from '../../../components/navbar';
import { getSession } from 'next-auth/react';
import GlobalDashboardTable from '../../../components/dashtable_v2';
import React from 'react';
import { createSuperblockDashboardObject } from '../../../util/dashboard/createSuperblockDashboardObject';
import { getTotalChallengesForSuperblocks } from '../../../util/student/calculateProgress';
import { fetchStudentData } from '../../../util/student/fetchStudentData';
import { checkIfStudentHasProgressDataForSuperblocksSelectedByTeacher } from '../../../util/student/checkIfStudentHasProgressDataForSuperblocksSelectedByTeacher';
import redirectUser from '../../../util/redirectUser.js';
// NOTE: These functions are deprecated for v9 curriculum (no individual REST API JSON files)
import { getDashedNamesURLs } from '../../../util/legacy/getDashedNamesURLs';
import { getSuperBlockJsons } from '../../../util/legacy/getSuperBlockJsons';
export async function getServerSideProps(context) {
// Dynamic import to prevent Prisma from being bundled for client
const { default: prisma } = await import('../../../prisma/prisma');
//making sure User is the teacher of this classsroom's dashboard
const userSession = await getSession(context);
if (!userSession) {
return redirectUser('/error');
}
const userEmail = await prisma.User.findMany({
where: {
email: userSession['user']['email']
}
});
const classroomTeacherId = await prisma.classroom.findUnique({
where: {
classroomId: context.params.id
},
select: {
classroomTeacherId: true
}
});
if (
classroomTeacherId == null ||
userEmail[0].id == null ||
userEmail[0].id !== classroomTeacherId['classroomTeacherId']
) {
return redirectUser('/classes');
}
const certificationNumbers = await prisma.classroom.findUnique({
where: {
classroomId: context.params.id
},
select: {
fccCertifications: true
}
});
let superblockURLS = await getDashedNamesURLs(
certificationNumbers.fccCertifications
);
let superBlockJsons = await getSuperBlockJsons(superblockURLS); // this is an array of urls
let dashboardObjs = await createSuperblockDashboardObject(superBlockJsons);
let totalChallenges = getTotalChallengesForSuperblocks(dashboardObjs);
let studentData = await fetchStudentData();
// Temporary check to map/accomodate hard-coded mock student data progress in unselected superblocks by teacher
let studentsAreEnrolledInSuperblocks =
checkIfStudentHasProgressDataForSuperblocksSelectedByTeacher(
studentData,
dashboardObjs
);
studentData.forEach(studentJSON => {
let indexToCheckProgress = studentData.indexOf(studentJSON);
let isStudentEnrolledInAtLeastOneSuperblock =
studentsAreEnrolledInSuperblocks[indexToCheckProgress].some(
val => val === true
);
if (!isStudentEnrolledInAtLeastOneSuperblock) {
studentData[indexToCheckProgress].certifications = [];
} else {
// Filter out certifications that are not selected by the teacher
studentJSON.certifications = studentJSON.certifications.filter(
(certification, certIndex) => {
return studentsAreEnrolledInSuperblocks[indexToCheckProgress][
certIndex
];
}
);
}
});
return {
props: {
userSession,
classroomId: context.params.id,
studentData,
totalChallenges: totalChallenges,
studentsAreEnrolledInSuperblocks
}
};
}
export default function Home({
userSession,
classroomId,
totalChallenges,
studentData,
studentsAreEnrolledInSuperblocks
}) {
return (
<Layout>
<Head>
<title>Create Next App</title>
<meta name='description' content='Generated by create next app' />
<link rel='icon' href='/favicon.ico' />
</Head>
{userSession && (
<>
<Navbar>
<div className='navButton'>
<Link href={'/classes'}>Classes</Link>
</div>
<div className='navButton'>
<Link href={'/'}> Menu</Link>
</div>
</Navbar>
<GlobalDashboardTable
classroomId={classroomId}
totalChallenges={totalChallenges}
studentData={studentData}
studentsAreEnrolledInSuperblocks={studentsAreEnrolledInSuperblocks}
></GlobalDashboardTable>
</>
)}
</Layout>
);
}