-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
242 lines (228 loc) · 7.23 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
import React, { Component } from 'react';
import { StyleSheet, Text, View, ScrollView, Modal } from 'react-native';
import { Button, Radio, Spinner, ListItem, Toast } from 'native-base';
import axios from 'axios';
import moment from 'moment'
const initialState = {
questions: [],
isQuizStarted: false,
isQuizFinished: false,
answersExtras: [],
quizStartTime: '',
quizEndTime: '',
totalScore: 0,
}
export default class App extends Component {
constructor() {
super();
this.state = { ...initialState }
}
componentDidMount() {
this.getQuestions();
}
getQuestions = () => {
axios.get('https://opentdb.com/api.php?amount=10&category=18&difficulty=easy')
.then((response) => {
let { results } = response.data;
results = results.map((question) => {
const allAnswer = [...question.incorrect_answers];
allAnswer.splice(Math.floor((Math.random() * allAnswer.length) + 1), 0, question.correct_answer);
question['allAnswer'] = allAnswer;
return question;
})
this.setState({ questions: results })
})
.catch((error) => {
console.log(error);
});
}
submitQuiz = () => {
const endTime = new Date();
let isCompleted = true;
let totalScore = 0;
const { questions } = this.state;
questions.forEach(element => {
if (!element.hasOwnProperty('userSelectedAnswer')) {
isCompleted = false;
Toast.show({
text: 'Please Answer all questions',
buttonText: 'Okay'
})
} else if (element.userSelectedAnswer == element.correct_answer) {
++totalScore;
}
});
if (isCompleted) {
this.setState({
quizEndTime: endTime,
totalScore,
isQuizFinished: true
});
}
}
startQuiz = () => {
const startTime = new Date();
this.setState({
isQuizStarted: true,
quizStartTime: startTime
});
}
checkedCheckbox = (answer, ind) => {
const { questions } = this.state;
let answers = { ...questions };
answers[ind].userSelectedAnswer = answer;
this.setState({ answersExtras: answers });
}
openModal = () => {
const { totalScore, quizEndTime, quizStartTime, questions } = this.state;
return (
<Modal
animationType="slide"
transparent={false}
style={{ flex: 1 }}
>
<View style={{ flex: 1.4, justifyContent: "center", alignItems: 'center', marginBottom: 20 }}>
<View style={{ flex: 1, justifyContent: "center", alignItems: 'center' }}>
<Text style={{ fontSize: 25 }}>Test Result</Text>
</View>
<View style={{ flex: 1.2, justifyContent: "center", alignItems: 'center' }}>
<Text style={{ fontSize: 20 }}>Start Time: {moment(quizStartTime).format("HH:mm:ss")}</Text>
<Text style={{ fontSize: 20 }}>End Time: {moment(quizEndTime).format("HH:mm:ss")}</Text>
<Text style={{ fontSize: 20 }}>Total Score: {totalScore} out of {questions.length}</Text>
</View>
<View stye={{ flex: 2, justifyContent: "center", alignItems: 'center', paddingBottom: 10 }}>
<Button full style={styles.SubmitButton} onPress={this.resetQuiz}>
<Text style={{ letterSpacing: 2 }}>Play Again</Text>
</Button>
<Button full style={styles.SubmitButton} onPress={this.cancelQuiz}>
<Text style={{ letterSpacing: 2 }}>Close</Text>
</Button>
</View>
</View>
</Modal>
)
}
resetQuiz = () => {
this.setState({
...initialState,
isQuizStarted: true,
isQuizFinished: false
})
this.getQuestions()
}
cancelQuiz = () => {
this.setState({
...initialState,
})
this.getQuestions()
}
render() {
const { questions, isQuizStarted, answersExtras, totalScore, isQuizFinished } = this.state;
if (!isQuizStarted) {
return (
<View style={styles.container}>
<View style={styles.QuizHeader}>
<Text style={styles.QuizHeaderText}> Trivia Quiz App</Text>
</View>
<View style={styles.QuizHeader}>
<Button full style={styles.Button} onPress={this.startQuiz}><Text style={styles.letterSpacing}>Play Quiz</Text></Button>
</View>
</View>
)
}
if (questions && questions.length) {
return (
<ScrollView>
{isQuizFinished ? this.openModal() : null}
{questions.map((question, ind) => {
return (
<View key={ind} style={styles.QuizHeader}>
<ListItem>
<View style={styles.QuestionView}>
<View style={styles.QuestionParent}>
<View style={styles.FirstText}><Text>Q {ind + 1}</Text></View>
<View style={styles.QuestionText}><Text>{question.question}</Text></View>
</View>
<View>
{question.allAnswer.map((answer, index) => {
return (
<View style={styles.SelectionParent} key={index}>
<View style={styles.FirstText}>
<Radio
onPress={() => this.checkedCheckbox(answer, ind)}
color={"#f0ad4e"}
selectedColor={"#5cb85c"}
selected={question.userSelectedAnswer && question.userSelectedAnswer == answer ? true : false}
/>
</View>
<View><Text>{answer}</Text></View>
</View>)
})}
</View>
</View>
</ListItem>
</View>
)
})
}
<View style={styles.ButtonSubmit}>
<Button full style={styles.SubmitButton} onPress={this.submitQuiz}><Text style={{ letterSpacing: 2 }}>Submit</Text></Button>
</View>
</ScrollView >
)
}
return (
<View style={styles.container}>
<Spinner color="white" />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#80CBC4',
},
Button: {
marginTop: 10,
alignSelf: "center",
backgroundColor: "white",
height: 60,
width: 350,
borderRadius: 10,
},
QuizHeader: { flex: 1, justifyContent: 'center', alignItems: 'center' },
FirstText:{ width: "10%" },
QuizHeaderText: {
letterSpacing: 2, fontSize: 25
},
SelectionParent:{ flexDirection: 'row', padding: 10 },
ButtonSubmit:{ paddingVertical: 10 },
QustionText:{ justifyContent: 'space-evenly' },
QuestionView:{ paddingVertical: 5, width: "98%", paddingRight: 5 },
letterSpacing:{
letterSpacing: 2,
},
QuestionParent:{ flexDirection: "row" },
SubmitButton: {
marginTop: 10,
alignSelf: "center",
backgroundColor: "white",
height: 60,
backgroundColor: '#80CBC4',
width: 350,
borderRadius: 10,
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});