-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.tsx
178 lines (162 loc) · 5.04 KB
/
index.tsx
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import ProtectedRoute from "@components/context/ProtectedRoute";
import {
ReviewSetScoresContext,
ReviewSetStageContext,
} from "@components/review/shared/reviewContext";
import { ReviewDriveToLearnStage } from "@components/review/stages/reviewDriveToLearnStage";
import { ReviewEndStage } from "@components/review/stages/reviewEndStage";
import { ReviewEndSuccessStage } from "@components/review/stages/reviewEndSuccessStage";
import { ReviewInfoStage } from "@components/review/stages/reviewInfoStage";
import { ReviewPassionForSocialGoodStage } from "@components/review/stages/reviewPassionForSocialGoodStage";
import { ReviewSkillStage } from "@components/review/stages/reviewSkillStage";
import { ReviewTeamPlayerStage } from "@components/review/stages/reviewTeamPlayerStage";
import { NextPage } from "next";
import { useRouter } from "next/router";
import { useState, useEffect } from "react";
import { fetchGraphql } from "@utils/makegqlrequest";
import ProtectedApplication from "./protectedApplication";
import { queries } from "graphql/queries";
import { ApplicationDTO, AuthStatus } from "types";
export enum ReviewStage {
INFO = "INFO",
PFSG = "PFSG",
TP = "TP",
D2L = "D2L",
SKL = "SKL",
END = "END",
END_SUCCESS = "END_SUCCESS",
}
export const getReviewId = (query: any): number => {
// verify reviewId
const reviewId =
typeof query["reviewId"] === "string"
? parseInt(query["reviewId"])
: (() => {
throw new Error("reviewId must be a String");
})();
if (Number.isNaN(reviewId))
throw Error("reviewId must be parsable into an int");
return reviewId;
};
export const extractShortAnswerData = (shortAnswerJSON: any) => {
const extractedQuestions = shortAnswerJSON.map(
(dict: { [key: string]: string }) => dict.question,
);
const extractedAnswers = shortAnswerJSON.map(
(dict: { [key: string]: string }) => dict.response,
);
return { extractedQuestions, extractedAnswers };
};
const ReviewsPages: NextPage = () => {
const router = useRouter();
const [stage, setStage] = useState<ReviewStage>(ReviewStage.INFO);
const [application, setApplication] = useState<ApplicationDTO>();
const name = application?.firstName + " " + application?.lastName;
const [authStatus, setAuthStatus] = useState<AuthStatus>({
loading: true,
isAuthorized: false,
});
const reviewId = getReviewId(useRouter().query);
const initialScores = new Map<ReviewStage, number>();
initialScores.set(ReviewStage.PFSG, 1);
initialScores.set(ReviewStage.TP, 1);
initialScores.set(ReviewStage.D2L, 1);
initialScores.set(ReviewStage.SKL, 1);
const [scores, setScores] = useState<Map<ReviewStage, number>>(initialScores);
const updateScores = (key: ReviewStage, value: number) => {
setScores((map) => {
if (isNaN(value) || value < 1 || value > 5) {
return map;
}
return new Map(map.set(key, value));
});
};
useEffect(() => {
fetchGraphql(queries.applicationsById, {
id: reviewId,
}).then((result) => {
if (result.data) {
const appInfo = result.data.applicationsById;
setApplication(appInfo);
} else {
setAuthStatus({
loading: false,
isAuthorized: false,
});
}
});
}, [reviewId]);
const getReviewStage = () => {
switch (stage) {
case ReviewStage.INFO:
return (
<ReviewInfoStage
name={name}
application={application}
scores={scores}
/>
);
case ReviewStage.PFSG:
return (
<ReviewPassionForSocialGoodStage
name={name}
application={application}
scores={scores}
/>
);
case ReviewStage.TP:
return (
<ReviewTeamPlayerStage
name={name}
application={application}
scores={scores}
/>
);
case ReviewStage.D2L:
return (
<ReviewDriveToLearnStage
name={name}
application={application}
scores={scores}
/>
);
case ReviewStage.SKL:
return (
<ReviewSkillStage
name={name}
application={application}
scores={scores}
/>
);
case ReviewStage.END:
return (
<ReviewEndStage
name={name}
application={application}
scores={scores}
/>
);
case ReviewStage.END_SUCCESS:
default:
return <ReviewEndSuccessStage />;
}
};
return (
<ReviewSetScoresContext.Provider value={updateScores}>
<ReviewSetStageContext.Provider value={setStage}>
{getReviewStage()}
</ReviewSetStageContext.Provider>
</ReviewSetScoresContext.Provider>
);
};
const Reviews: NextPage = () => {
const router = useRouter();
return (
<ProtectedRoute allowedRoles={["Admin", "User"]}>
<ProtectedApplication headerInformation={router.query}>
<ReviewsPages />
</ProtectedApplication>
</ProtectedRoute>
);
};
export default Reviews;