-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathQuestions.js
142 lines (123 loc) · 3.37 KB
/
Questions.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
import React from "react";
import {
View,
Text,
ActivityIndicator,
StyleSheet,
Picker,
Button
} from "react-native";
import { Link } from "react-router-native";
import Question from "../components/Question";
export default class Questions extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
questions: [],
current: 0,
correctScore: 5,
totalScore: 50,
results: {
score: 0,
correctAnswers: 0
},
completed: false
};
}
fetchQuestions = async () => {
await this.setState({ loading: true });
const response = await fetch(
`https://opentdb.com/api.php?amount=10&difficulty=medium`
);
const questions = await response.json();
const { results } = questions;
results.forEach(item => {
item.id = Math.floor(Math.random() * 10000);
});
await this.setState({ questions: results, loading: false });
};
reset = () => {
this.setState(
{
questions: [],
current: 0,
results: {
score: 0,
correctAnswers: 0
},
completed: false
},
() => {
this.fetchQuestions();
}
);
};
submitAnswer = (index, answer) => {
const question = this.state.questions[index];
const isCorrect = question.correct_answer === answer;
const results = { ...this.state.results };
results.score = isCorrect ? results.score + 5 : results.score;
results.correctAnswers = isCorrect
? results.correctAnswers + 1
: results.correctAnswers;
this.setState({
current: index + 1,
results,
completed: index === 9 ? true : false
});
};
componentDidMount() {
this.fetchQuestions();
}
render() {
return (
<View style={styles.container}>
{!!this.state.loading && (
<View style={styles.loadingQuestions}>
<ActivityIndicator size="large" color="#00ff00" />
<Text>Please wait while we are loading questions for you</Text>
</View>
)}
{!!this.state.questions.length > 0 &&
this.state.completed === false && (
<Question
onSelect={answer => {
this.submitAnswer(this.state.current, answer);
}}
question={this.state.questions[this.state.current]}
correctPosition={Math.floor(Math.random() * 3)}
current={this.state.current}
/>
)}
<View
style={{ flex: 1, alignItems: "center", justifyContent: "center" }}
>
{this.state.completed === true && (
<View style={{ alignItems: "center" }}>
<Text style={{ fontSize: 25 }}>Quiz Completed</Text>
<Text>Correct Answers: {this.state.results.correctAnswers}</Text>
<Text>
Incorrect Answers: {10 - this.state.results.correctAnswers}
</Text>
<Text>Total Score: {50}</Text>
<Text>Obtained Score: {this.state.results.score}</Text>
<Button title="Restart Quiz" onPress={this.reset} />
</View>
)}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
display: "flex",
height: "100%"
},
loadingQuestions: {
flex: 1,
alignItems: "center",
justifyContent: "center"
}
});