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 }, text: { color: colors.white, fontSize: 20, textAlign: "center", fontWeight: "600", paddingVertical: 20 }, timer: { color: colors.white, fontSize: 30, textAlign: "center", fontWeight: "600", paddingVertical: 10, marginTop: 10, }, safearea: { flex: 1, marginTop: 20, paddingHorizontal: 20, justifyContent: "space-between" } }) let interval = null const maxTime = 1800 class Exam extends React.Component { state = { correctCount: 0, pointsCount: 0, totalPoints: 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, results: false, timer: maxTime } answer = (correct, id, points) => { this.setState( state => { const nextState = { answered: true, clickedId: id, totalPoints: state.totalPoints + parseInt(points)} if (correct) { nextState.correctCount = state.correctCount + 1 nextState.pointsCount = state.pointsCount + parseInt(points) nextState.answerCorrect = true } else { nextState.wrongCount = state.wrongCount + 1 nextState.answerCorrect = false } return nextState }, () => { if(this.state.timer > 1 || (this.state.correctCount+this.state.wrongCount) < this.state.totalCount) { setTimeout(() => this.nextQuestion(), correct ? 750 : 3500) } } ) } nextQuestion = () => { this.setState( (state) => { const updatedIndexes = state.availableIds.filter( item => item != state.activeQuestionId) const nextId = updatedIndexes[Math.floor(Math.random() * updatedIndexes.length)] let resultsShow = (this.state.timer <= 1 || (this.state.correctCount+this.state.wrongCount) == this.state.totalCount) ? true : false if (!updatedIndexes.length) { resultsShow = true setTimeout( () => { this.props.navigation.popToTop() }, 10000) } return { availableIds: updatedIndexes, activeQuestionId: nextId, answered: false, results: resultsShow } }) } componentWillUnmount(){ clearInterval(interval) } render() { const questions = this.props.navigation.getParam("questions", []) const question = questions.filter(item => item.id == this.state.activeQuestionId)[0] || questions[0] if(this.state.timer==maxTime) { interval = setInterval( () => { this.setState( (state) => { return { timer: this.state.timer-1, results: this.state.timer <= 1 || false } }) }, 1000) } if(this.state.timer < 1 || (this.state.correctCount+this.state.wrongCount) == this.state.totalCount) { clearInterval(interval) setTimeout( () => { this.props.navigation.popToTop() }, 10000) } return ( {!this.state.results ? {new Date(this.state.timer * 1000).toISOString().substr(11, 8)} {question.question} {question.answers.map(answer => (