kepas

Untitled

Feb 28th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react';
  2.  
  3. import {quizzes, users} from './examples';
  4. import {HTTP_SERVER_PORT_PICTURES} from './constants.js';
  5. import {HTTP_SERVER_PORT} from './constants.js';
  6. import axios from 'axios';
  7. class Question extends Component {
  8.  
  9.  
  10.     render() {
  11.         return (
  12.             <div>
  13.                 {this.props.question.question}
  14.  
  15.             </div>
  16.         );
  17.     }
  18. }
  19.  
  20. class TxtAnswers extends Component {
  21.  
  22.  
  23.     render() {
  24.         console.log(this.props.question);
  25.         const t = this.props.question.txtAnswers.map(s => <div key={s} id={"txtanswers"}><input type="checkbox" value={s} name="test"/> {s}</div>);
  26.         return (
  27.             <div id="grid-text">
  28.                 {t}
  29.             </div>
  30.         );
  31.     }
  32. }
  33.  
  34. class ImgAnswers extends Component {
  35.     render() {
  36.         console.log(this.props.question);
  37.         const t = this.props.question.imgAnswers.map(s => <div key={s}><input type="checkbox" value={s} name="test"/><img src={HTTP_SERVER_PORT_PICTURES + s} /></div>);
  38.         return (
  39.             <div>
  40.                 {t}
  41.             </div>
  42.         );
  43.     }
  44. }
  45.  
  46. class Quizzpage extends Component {
  47.  
  48.     constructor(props) {
  49.         super(props);
  50.  
  51.         this.quizz = quizzes.filter(q=> q._id == this.props.match.params.id)[0];
  52.  
  53.         this.maxScore = this.quizz.questions.map(q=>q.points).reduce((acc,current)=>acc+current);
  54.         const miScore = this.maxScore/2;
  55.         console.log("maxScore=",this.maxScore);
  56.         console.log(miScore)
  57.         this.state = {current : 0, score : 0,quizzes:quizzes};
  58.    
  59.     }
  60.  
  61.     async loadData() {
  62.         const quizzes = (await axios.get(HTTP_SERVER_PORT + 'quizz')).data;  // We need to wait for the response.
  63.         this.setState({quizzes: quizzes});
  64.       }
  65.  
  66.       componentDidMount(){
  67.           this.loadData()
  68.       }
  69.     reponse(e)  {
  70.         e.preventDefault();
  71.         const question = this.quizz.questions[this.state.current];
  72.         let choices = [];
  73.         let verification = false;
  74.         for(let i = 0; i < e.target.elements.length; i++) {
  75.             if(e.target.elements[i].checked)
  76.                 choices.push(i);
  77.         }
  78.         // choices == question.solutions
  79.             if(choices.length == question.solutions.length){
  80.                 let ok=true;
  81.                 for(let i = 0; ok && i<choices.length; i++){
  82.                     if(choices[i]!==question.solutions[i]){
  83.                         ok=false;
  84.                     }
  85.                 }
  86.                 if(ok){
  87.                     this.state.score = this.state.score + question.points;
  88.                 }
  89.             }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.         this.setState({current : this.state.current + 1 });
  96.  
  97.  
  98.     }
  99.  
  100.     render() {
  101.         if(this.state.current == this.quizz.questions.length) {
  102.             let c = null;
  103.  
  104.             if (this.state.score == this.maxScore)
  105.                 c = "<b>Youpi</b>";
  106.             return (
  107.                 <div>C fini {this.state.score} sur {this.maxScore} {c}</div>
  108.             );
  109.         }
  110.         const question = this.quizz.questions[this.state.current];
  111.         return (
  112.             <div id={"Quizzpage"}>
  113.  
  114.                 <h4>{this.quizz.name}</h4>
  115.                 <div id="img">
  116.                 <img id="icon" src={HTTP_SERVER_PORT_PICTURES + this.quizz.icon}/>
  117.                 </div>
  118.                     <br/>
  119.                 <div id="question">
  120.                 <Question question = {question} />
  121.                 </div>
  122.                 <br/>
  123.                 <form onSubmit={(e)=> this.reponse(e)}>
  124.                 <TxtAnswers question={question} />
  125.                 <ImgAnswers question={question}/>
  126.  
  127.                 <div id="submit">
  128.                 <input id="button" type="submit"/>
  129.                 </div>
  130.                 </form>
  131.             </div>
  132.         );
  133.     }
  134. }
  135.  
  136. export default Quizzpage;
  137.  
  138.  
  139.  
  140. /// HOME
  141.  
  142. import React, {Component} from 'react';
  143.  
  144. import {quizzes, users} from './examples';
  145. import {HTTP_SERVER_PORT_PICTURES} from './constants.js';
  146.  
  147. class Thumbnails extends Component {
  148.     render (){
  149.         const divStyle = {
  150.             backgroundImage: 'url(' + HTTP_SERVER_PORT_PICTURES + this.props.quizz.icon + ')',
  151.             backgroundPosition: 'center',
  152.             backgroundSize: 'cover',
  153.         };
  154.  
  155.         return (
  156.  
  157.             <div className="quizItem" style={divStyle}>
  158.                 <span>
  159.                     <h1 className='quizName'>{this.props.quizz.name}</h1>
  160.                 </span>
  161.                 <img className='quizPicture' src={HTTP_SERVER_PORT_PICTURES + this.props.quizz.icon}/>
  162.             </div>
  163.  
  164.         )
  165.     }
  166. }
  167.  
  168. class Home extends Component {
  169.     render() {
  170.         return (
  171.             <div>
  172.                 <h1 id="quizCategories">Quizz categories : </h1>
  173.                 <div className='quizList'>
  174.                     {quizzes.map(q => <Thumbnails quizz={q}/>)}
  175.                 </div>
  176.             </div>
  177.         );
  178.     }
  179. }
  180.  
  181. export default Home;
Advertisement
Add Comment
Please, Sign In to add comment