Skip to content
Permalink
5ef4de0b3d
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
104 lines (80 sloc) 2.52 KB
import React from 'react';
import './Quiz.css';
import Question from '../question/Question.js';
import QuizHead from './QuizHead.js';
//import equal from 'fast-deep-equal'
class Quiz extends React.Component {
constructor(props) {
super(props);
this.updateQuiz = this.updateQuiz.bind(this);
this.state = {
questions: [],
title: 'Quiz',
instructions: "Answer them",
timeAllowed: "no",
quizNum: this.props.quizNum
}
}
updateQuiz(){
this.setState({
questions: []
})
fetch("http://localhost:3000/api/v1.0/quiz/questions_by_id/" + this.state.quizNum)
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
questions: result
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
fetch("http://localhost:3000/api/v1.0/quiz/quiz_by_id/" + this.state.quizNum)
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
title: result[0]['title'],
instructions: result[0]['instructions'],
timeAllowed: result[0]['timeAllowed']
//questions: result
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
componentDidMount() {
this.updateQuiz();
}
componentDidUpdate(prevProps) {
if(prevProps.quizNum !== this.props.quizNum){
this.setState({
quizNum: this.props.quizNum
});
this.updateQuiz();
}
}
render() {
return (
<div>
<QuizHead title={this.state.title} instructions={this.state.instructions} timeAllowed={this.state.timeAllowed} />
{this.state.questions.map((question, index) => (
<Question data={question} qNum={index + 1} key={index}/>
))}
</div>
)
};
}
export default Quiz;