-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
332 lines (310 loc) · 13.3 KB
/
App.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import './App.css';
import questions from "./assets/questions";
import React, {Component, Fragment} from "react";
import Alarms from "./assets/images/alarms.png";
import HDFSArchitecture from "./assets/images/hdfs.png"
import BigTableArchitecture from "./assets/images/bigtable.png";
import KubernatesArchitecture from "./assets/images/kubernetes.png";
import MAPEK from "./assets/images/mapek.png";
import GFSArchitecture from "./assets/images/gfs.png";
class App extends Component {
constructor(props) {
super(props);
this.state = {
quiz: null,
timer_minutes: 40,
timer_seconds: 0,
optionAnswersCheck: false,
score: {},
evaluated: false,
answersSelection: {},
darkMode: true
};
this.questions_number = 35;
this.timer = 0;
this.correct_answer_score = 2;
this.wrong_answer_score = -0.5;
this.maximum_score = this.correct_answer_score * this.questions_number;
this.mapImages = {
"alarms": Alarms,
"bigtable": BigTableArchitecture,
"kubernetes": KubernatesArchitecture,
"mapek": MAPEK,
"hdfs": HDFSArchitecture,
"gfs": GFSArchitecture
}
Object.keys(questions).forEach((key) => {
questions[key]["answersQuizzed"] = [];
for(let i = 0; i < questions[key]["options"].length; i++) {
questions[key]["answersQuizzed"].push(
[questions[key]["options"][i], i == questions[key]["answer"]]
);
}
this.shuffle(questions[key]["answersQuizzed"])
});
}
componentDidMount() {
let quiz = this.get_random_questions();
this.setState({quiz});
this.timer = setInterval(() => {
let {timer_minutes, timer_seconds} = this.state;
if(timer_seconds == 0 && timer_minutes >= 0)
this.setState({timer_seconds: 59, timer_minutes: --timer_minutes});
else if(timer_minutes >= 0)
this.setState({timer_seconds: --timer_seconds});
if(timer_seconds <= 0 && timer_minutes <= 0) {
let inputs = document.getElementsByTagName("INPUT");
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].type === 'radio') {
inputs[i].disabled = true;
}
}
this.timer = clearInterval(this.timer);
this.setState({timer_seconds: 0, timer_minutes: 0})
}
}, 1000);
window.onbeforeunload = () => {
return "Are u sure you want to leave?";
};
if(this.state.darkMode) {
document.body.style.backgroundColor = "#0c0c0c";
}
}
get_random_questions = () => {
let quiz = this.shuffle(questions);
quiz = quiz.slice(0, this.questions_number);
return quiz;
};
reset_question_color = (questionKey, n) => {
for(let i = 0; i < n; i++) {
let answer_element = document.getElementById(`question_${questionKey}_${i}`);
answer_element.style.color = this.state.darkMode ? "#e4e4e4" : "black";
answer_element.style.fontWeight = "normal";
}
};
calculate_correct_idx = (qst) => {
let jk = -1;
for(let i = 0; i < qst.length; i++) {
if(qst[i][1]) {
jk = i;
break;
}
}
return jk;
};
componentDidUpdate(prevProps, prevState, snapshot) {
if(prevState.darkMode != this.state.darkMode && !this.state.darkMode)
{
document.body.style.backgroundColor = "white";
document.body.style.color = "black";
} else if(prevState.darkMode != this.state.darkMode && this.state.darkMode) {
document.body.style.backgroundColor = "#0c0c0c";
document.body.style.color = "#e4e4e4";
}
}
evaluate_question = (question, selected, questionKey, answerObj, fromButton=false) => {
let {optionAnswersCheck, score, answersSelection} = this.state;
if(optionAnswersCheck && !fromButton) {
this.reset_question_color(questionKey, question["options"].length);
let selected_answer_element = document.getElementById(`question_${questionKey}_${selected}`);
if(typeof(answerObj) !== "undefined") {
if(answerObj[1]) {
selected_answer_element.style.color = "green";
selected_answer_element.style.fontWeight = "bold";
score[questionKey] = this.correct_answer_score;
answersSelection[questionKey] = selected;
} else {
selected_answer_element.style.color = "red";
selected_answer_element.style.fontWeight = "bold";
score[questionKey] = this.wrong_answer_score;
answersSelection[questionKey] = selected;
let correct_answer_element = document.getElementById(`question_${questionKey}_${this.calculate_correct_idx(question["answersQuizzed"])}`);
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
}
} else {
score[questionKey] = 0;
answersSelection[questionKey] = null;
let correct_answer_element = document.getElementById(`question_${questionKey}_${this.calculate_correct_idx(question["answersQuizzed"])}`);
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
}
} else {
if(!fromButton) {
if (answerObj[1]) {
score[questionKey] = this.correct_answer_score;
answersSelection[questionKey] = selected;
} else if (answerObj != null && !answerObj[1]) {
score[questionKey] = this.wrong_answer_score;
answersSelection[questionKey] = selected;
} else {
score[questionKey] = 0;
answersSelection[questionKey] = selected;
}
this.setState({score, answersSelection});
} else {
this.timer = clearInterval(this.timer);
this.setState({timer_minutes: 30, timer_seconds: 0, evaluated: true});
let {quiz} = this.state;
Object.keys(this.state.score).map((key) => {
if(score[key] == this.correct_answer_score) {
// corretta
let selected_answer_element = document.getElementById(`question_${key}_${this.calculate_correct_idx(quiz[key]["answersQuizzed"])}`);
if(selected_answer_element !== null) {
selected_answer_element.style.color = "green";
selected_answer_element.style.fontWeight = "bold";
}
} else if(score[key] == this.wrong_answer_score) {
let selected_answer_element = document.getElementById(`question_${key}_${answersSelection[key]}`);
let correct_answer_element = document.getElementById(`question_${key}_${this.calculate_correct_idx(quiz[key]["answersQuizzed"])}`);
if(correct_answer_element != null) {
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
}
if(selected_answer_element != null) {
selected_answer_element.style.color = "red";
selected_answer_element.style.fontWeight = "bold";
}
} else {
let correct_answer_element = document.getElementById(`question_${key}_${this.calculate_correct_idx(quiz[key]["answersQuizzed"])}`);
if(correct_answer_element != null) {
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
}
}
})
}
}
/*if(optionAnswersCheck) {
this.reset_question_color(questionKey, question["options"].length);
let selected_answer_element = document.getElementById(`question_${questionKey}_${selected}`);
if(answerObj[1]) {
selected_answer_element.style.color = "green";
selected_answer_element.style.fontWeight = "bold";
score[questionKey] = 2;
answersSelection[questionKey] = selected;
} else if(selected !== null) {
let correct_answer_element = document.getElementById(`question_${questionKey}_${question["answer"]}`);
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
selected_answer_element.style.color = "red";
selected_answer_element.style.fontWeight = "bold";
score[questionKey] = -1;
answersSelection[questionKey] = selected;
} else {
let correct_answer_element = document.getElementById(`question_${questionKey}_${question["answer"]}`);
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
score[questionKey] = 0;
answersSelection[questionKey] = selected;
}
this.setState({score, answersSelection});
} else {
if(!fromButton) {
if (selected == question["answer"]) {
score[questionKey] = 2;
answersSelection[questionKey] = selected;
} else if (selected != null) {
score[questionKey] = -1;
answersSelection[questionKey] = selected;
} else {
score[questionKey] = 0;
answersSelection[questionKey] = selected;
}
this.setState({score, answersSelection});
} else {
this.timer = clearInterval(this.timer);
this.setState({timer_minutes: 30, timer_seconds: 0, evaluated: true});
let {quiz} = this.state;
Object.keys(this.state.score).map((key) => {
if(score[key] == 2) {
// corretta
let selected_answer_element = document.getElementById(`question_${key}_${[quiz[key]["answer"]]}`);
if(selected_answer_element !== null) {
selected_answer_element.style.color = "green";
selected_answer_element.style.fontWeight = "bold";
}
} else if(score[key] == -1) {
let selected_answer_element = document.getElementById(`question_${key}_${answersSelection[key]}`);
let correct_answer_element = document.getElementById(`question_${key}_${quiz[key]["answer"]}`);
correct_answer_element.style.color = "green";
correct_answer_element.style.fontWeight = "bold";
selected_answer_element.style.color = "red";
selected_answer_element.style.fontWeight = "bold";
score[questionKey] = -1;
}
})
}
} */
};
shuffle = (array) => {
return array.sort(() => Math.random() - 0.5);
};
render() {
let {quiz, timer_minutes, timer_seconds, optionAnswersCheck, score, evaluated, darkMode} = this.state;
let score_point = 0;
if(Object.keys(score).length > 0) {
score_point = Object.values(score).reduce((a, b) => a + b);
}
if(quiz != null) {
return (
<div className={["fullwidth quiz contentcentered", darkMode ? "darkTheme" : ""].join(" ")} >
<div className={"checksolutionoption"}>
<input onChange={() => this.setState({optionAnswersCheck: !optionAnswersCheck})} type={"checkbox"} name={"itsfridaythen"} checked={optionAnswersCheck} />
<label>
Check if you want to see the correct answer every time you select an option.
</label>
</div>
<div className={"questions"}>
{quiz.map((questionObject, key) => {
return (
<div key={key} className={"question"}>
<div className={"questiontext"}>{questionObject["question"]}</div>
{"figure" in questionObject &&
<div style={{maxWidth: "600px", maxHeight: "500px", margin: "5px"}}>
<img src={this.mapImages[questionObject["figure"]]} />
</div>
}
<div className={"answers"}>
{questionObject["answersQuizzed"].map((answer, keyAnswer) => {
return (
<div key={keyAnswer}>
<input onChange={() => this.evaluate_question(questionObject, keyAnswer, key, questionObject["answersQuizzed"][keyAnswer])} value={keyAnswer} type={"radio"} name={`question_${key}`} />
<label id={`question_${key}_${keyAnswer}`}>{answer[0]}</label>
</div>
)
})}
<div>
<input onChange={() => this.evaluate_question(questionObject, null, key)} type={"radio"} name={`question_${key}`} />
<label id={`question_${key}_null`}>No answer</label>
</div>
</div>
</div>
)
})}
</div>
<div className={"score"}>
Score: {optionAnswersCheck ? score_point : evaluated ? score_point : 0}/{this.maximum_score}
</div>
<div className={"nightMode"}>
<label>Night mode</label>
<input type={"checkbox"} checked={darkMode} onChange={() => this.setState({darkMode: !darkMode})} />
</div>
<div className={["timer", darkMode ? "black" : ""].join(" ")}>
{timer_minutes}:{timer_seconds < 10 ? `0${timer_seconds}` : timer_seconds}
</div>
<div style={{marginBottom: 30}}>
<button onClick={() => this.evaluate_question(null, null, null, null, true)} className={"sendQuiz"}>Evaluate quiz</button>
</div>
</div>
);
} else {
return (
<div className={"centered loading fullwidth"}>
Loading quiz..
</div>
)
}
}
}
export default App;