From 2b3f3da9d8d7a9d92652e9d8a01bca18dd421c8b Mon Sep 17 00:00:00 2001 From: Dslak Date: Sun, 20 Oct 2019 20:16:22 +0200 Subject: [PATCH] add timer --- vds-app/App/components/Results.js | 2 +- vds-app/App/screens/Exam.js | 36 ++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/vds-app/App/components/Results.js b/vds-app/App/components/Results.js index 30c8053..28d8099 100644 --- a/vds-app/App/components/Results.js +++ b/vds-app/App/components/Results.js @@ -72,7 +72,7 @@ export const Results = ({ correct, wrong, visible }) => { if (!visible) return null const total = wrong+correct - const percentage = (100/total) * correct + const percentage = total ? (100/total) * correct : 0 const boxStyle = percentage >= 80 ? percentage >= 85 ? styles.box : styles.boxUnsafe : styles.boxWrong const percentStyle = percentage >= 80 ? percentage >= 85 ? styles.correct : styles.unsafe : styles.wrong diff --git a/vds-app/App/screens/Exam.js b/vds-app/App/screens/Exam.js index ada9393..3afe47b 100644 --- a/vds-app/App/screens/Exam.js +++ b/vds-app/App/screens/Exam.js @@ -20,6 +20,14 @@ const styles = StyleSheet.create({ paddingVertical: 20, marginTop: 20, }, + timer: { + color: colors.white, + fontSize: 30, + textAlign: "center", + fontWeight: "600", + paddingVertical: 10, + marginTop: 10, + }, safearea: { flex: 1, marginTop: 20, @@ -27,6 +35,9 @@ const styles = StyleSheet.create({ } }) +let interval = null +const maxTime = 10 + class Exam extends React.Component { state = { @@ -39,7 +50,8 @@ class Exam extends React.Component { ].id, answered: false, answerCorrect: false, - results: false + results: false, + timer: 0 } answer = correct => { @@ -67,7 +79,7 @@ class Exam extends React.Component { this.setState( (state) => { const updatedIndexes = state.availableIds.filter( item => item != state.activeQuestionId) const nextId = updatedIndexes[Math.floor(Math.random() * updatedIndexes.length)] - let resultsShow = false + let resultsShow = this.state.timer >= maxTime-1 || false if (!updatedIndexes.length) { resultsShow = true @@ -77,7 +89,6 @@ class Exam extends React.Component { } return { - //totalCount: updatedIndexes.length, availableIds: updatedIndexes, activeQuestionId: nextId, answered: false, @@ -90,6 +101,24 @@ class Exam extends React.Component { const questions = this.props.navigation.getParam("questions", []) const question = questions.filter(item => item.id == this.state.activeQuestionId)[0] || questions[0] + if(this.state.timer==0) { + interval = setInterval( () => { + this.setState( (state) => { + return { + timer: this.state.timer+1, + results: this.state.timer >= maxTime-1 || false + } + }) + }, 1000) + } + + if(this.state.timer >= maxTime) { + clearInterval(interval) + setTimeout( () => { + this.props.navigation.popToTop() + }, 5000) + } + return ( + {new Date(this.state.timer * 1000).toISOString().substr(11, 8)} {question.question}