You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.3 KiB

import React from "react"
import { View, ScrollView, StyleSheet, StatusBar, Text, SafeAreaView } from "react-native"
import { Button, ButtonContainer } from "../components/Button"
import { Alert } from "../components/Alert"
import { Results } from "../components/Results"
import { colors } from "../components/Variables"
const styles = StyleSheet.create({
container: {
backgroundColor: colors.blue,
flex: 1,
paddingHorizontal: 20
},
text: {
color: colors.white,
fontSize: 20,
textAlign: "center",
fontWeight: "600",
paddingVertical: 20,
marginTop: 20,
},
6 years ago
timer: {
color: colors.white,
fontSize: 30,
textAlign: "center",
fontWeight: "600",
paddingVertical: 10,
marginTop: 10,
},
safearea: {
flex: 1,
marginTop: 20,
justifyContent: "space-between"
}
})
6 years ago
let interval = null
6 years ago
const maxTime = 10 // 1800
6 years ago
class Exam extends React.Component {
state = {
correctCount: 0,
wrongCount: 0,
totalCount: this.props.navigation.getParam("questions", []).length,
availableIds: this.props.navigation.getParam("questions", []).map(a => a.id),
activeQuestionId: this.props.navigation.getParam("questions", [])[
Math.floor(Math.random() * this.props.navigation.getParam("questions", []).length)
].id,
answered: false,
answerCorrect: false,
6 years ago
results: false,
6 years ago
timer: maxTime
}
answer = correct => {
this.setState(
state => {
const nextState = { answered: true }
if (correct) {
nextState.correctCount = state.correctCount + 1
nextState.answerCorrect = true
} else {
nextState.wrongCount = state.wrongCount + 1
nextState.answerCorrect = false
}
return nextState
},
() => {
setTimeout(() => this.nextQuestion(), 750)
}
)
}
nextQuestion = () => {
this.setState( (state) => {
const updatedIndexes = state.availableIds.filter( item => item != state.activeQuestionId)
const nextId = updatedIndexes[Math.floor(Math.random() * updatedIndexes.length)]
6 years ago
let resultsShow = this.state.timer <= 1 || false
if (!updatedIndexes.length) {
resultsShow = true
setTimeout( () => {
this.props.navigation.popToTop()
}, 5000)
}
return {
availableIds: updatedIndexes,
activeQuestionId: nextId,
answered: false,
results: resultsShow
}
})
}
render() {
const questions = this.props.navigation.getParam("questions", [])
const question = questions.filter(item => item.id == this.state.activeQuestionId)[0] || questions[0]
6 years ago
if(this.state.timer==maxTime) {
6 years ago
interval = setInterval( () => {
this.setState( (state) => {
return {
6 years ago
timer: this.state.timer-1,
results: this.state.timer <= 1 || false
6 years ago
}
})
}, 1000)
}
6 years ago
if(this.state.timer < 1) {
6 years ago
clearInterval(interval)
setTimeout( () => {
this.props.navigation.popToTop()
}, 5000)
}
return (
<ScrollView style={[
styles.container,
{ backgroundColor: this.props.navigation.getParam("color") }
]}
>
<StatusBar barStyle="light-content" />
<SafeAreaView style={styles.safearea}>
6 years ago
<Text style={styles.timer}>{new Date(this.state.timer * 1000).toISOString().substr(11, 8)}</Text>
<View>
<Text style={styles.text}>{question.question}</Text>
<ButtonContainer>
{question.answers.map(answer => (
<Button
key={answer.id}
text={answer.text}
onPress={() => this.answer(answer.correct)}
/>
))}
</ButtonContainer>
</View>
<Text style={styles.text}>
{`${this.state.correctCount+this.state.wrongCount}/${this.state.totalCount}`}
</Text>
</SafeAreaView>
<Alert
correct={this.state.answerCorrect}
visible={this.state.answered}
/>
<Results
6 years ago
total={this.state.totalCount}
correct={this.state.correctCount}
wrong={this.state.wrongCount}
visible={this.state.results}
/>
</ScrollView>
)
}
}
export default Exam