kepas

Untitled

Feb 28th, 2019
111
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. import {Link} from "react-router-dom";
  8. class Question extends Component {
  9.  
  10.  
  11.     render() {
  12.         return (
  13.             <div>
  14.                 {this.props.question.question}
  15.  
  16.             </div>
  17.         );
  18.     }
  19. }
  20.  
  21. class TxtAnswers extends Component {
  22.  
  23.  
  24.     render() {
  25.         console.log(this.props.question);
  26.         const t = this.props.question.txtAnswers.map(s => <div key={s} id={"txtanswers"}><input type="checkbox" value={s} name="test"/> {s}</div>);
  27.         return (
  28.             <div className="grid-text">
  29.                 {t}
  30.             </div>
  31.         );
  32.     }
  33. }
  34.  
  35. class ImgAnswers extends Component {
  36.     render() {
  37.         console.log(this.props.question);
  38.         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>);
  39.         {console.log(HTTP_SERVER_PORT_PICTURES + s)}
  40.         return (
  41.             <div className="grid-img">
  42.                 {t}
  43.             </div>
  44.         );
  45.     }
  46. }
  47.  
  48. class Quizzpage extends Component {
  49.  
  50.     constructor(props) {
  51.         super(props);
  52.  
  53.         this.quizz = quizzes.filter(q=> q._id == this.props.match.params.id)[0];
  54.  
  55.         this.maxScore = this.quizz.questions.map(q=>q.points).reduce((acc,current)=>acc+current);
  56.         const miScore = this.maxScore/2;
  57.         console.log("maxScore=",this.maxScore);
  58.         console.log(miScore)
  59.         this.state = {current : 0, score : 0,quizzes:quizzes};
  60.    
  61.     }
  62.  
  63.     async loadData() {
  64.         const quizzes = (await axios.get(HTTP_SERVER_PORT + 'quizz')).data;  // We need to wait for the response.
  65.         this.setState({quizzes: quizzes});
  66.     }
  67.  
  68.     componentDidMount(){
  69.         this.loadData()
  70.     }
  71.  
  72.     reponse(e)  {
  73.         e.preventDefault();
  74.         const question = this.quizz.questions[this.state.current];
  75.         let choices = [];
  76.         let verification = false;
  77.         for(let i = 0; i < e.target.elements.length; i++) {
  78.             if(e.target.elements[i].checked)
  79.                 choices.push(i);
  80.         }
  81.         // choices == question.solutions
  82.         if(choices.length == question.solutions.length){
  83.             let ok=true;
  84.             for(let i = 0; ok && i<choices.length; i++){
  85.                 if(choices[i]!==question.solutions[i]){
  86.                     ok=false;
  87.                 }
  88.             }
  89.             if(ok){
  90.                 this.state.score = this.state.score + question.points;
  91.             }
  92.         }
  93.  
  94.         this.setState({current : this.state.current + 1 });
  95.     }
  96.  
  97.     render() {
  98.         if(this.state.current == this.quizz.questions.length) {
  99.             let c = null;
  100.  
  101.             if (this.state.score == this.maxScore)
  102.                 c = "Youpi";
  103.  
  104.             else if(this.state.score<this.maxScore/2)
  105.                 c = "Peut mieux faire";
  106.  
  107.             return (
  108.                 <div>C fini {this.state.score} sur {this.maxScore} {c}
  109.                     <Link id="Home_btn" to={'/'}>Go Home</Link>
  110.                 </div>
  111.             );
  112.         }
  113.         const question = this.quizz.questions[this.state.current];
  114.         return (
  115.             <div id={"Quizzpage"}>
  116.  
  117.                 <h4>{this.quizz.name}</h4>
  118.                 <div id="img">
  119.                     <img id="icon" src={HTTP_SERVER_PORT_PICTURES + this.quizz.icon}/>
  120.                 </div>
  121.                 <br/>
  122.                 <div id="question">
  123.                     <Question question = {question} />
  124.                 </div>
  125.                 <br/>
  126.                 <form id="responseForm" onSubmit={(e)=> this.reponse(e)}>
  127.                     <TxtAnswers question={question} />
  128.                     <ImgAnswers question={question}/>
  129.  
  130.                     <div id="submit">
  131.                     <input id="button" type="submit"/>
  132.                     </div>
  133.                 </form>
  134.             </div>
  135.         );
  136.     }
  137. }
  138.  
  139. export default Quizzpage;
  140.  
  141.  
  142.  
  143. MONGOSE
  144. const mongoose = require('mongoose');
  145. const Schema = mongoose.Schema;
  146.  
  147. // db connexion
  148. mongoose.connect('mongodb://localhost/quizz',{ useNewUrlParser: true });
  149.  
  150. const db = mongoose.connection;
  151. db.on('error', console.error.bind(console, 'connection error:'));
  152. db.once('open', () => {
  153.   console.log("connected to", db.client.s.url);
  154. });
  155.  
  156. // database collections
  157. const usersSchema = Schema({
  158.   name: String,
  159.   password: String
  160. });
  161.  
  162. mongoose.Schema.Types.ObjectId
  163. // const QuestionSchema = Schema({
  164. //   question:String
  165. // })
  166.  
  167. // const QuestionSchema = new Schema({
  168.  
  169. //   title: {
  170. //       type: String,
  171.      
  172. //   },
  173. //   questions:[q]
  174. //   content: {
  175. //       type: String,
  176.      
  177. //   },
  178. //   image: {
  179. //       type: String
  180. //   },
  181.  
  182.  
  183. // },
  184. // { versionKey: false });
  185.  
  186.  
  187. const QuestionSchema = Schema({
  188.   question:String
  189. })
  190.  
  191. // const QuizzSchema = new Schema({
  192. //
  193. //  name:String,
  194. //  icon:String,
  195. //  keywords:[String],
  196. //   questions:[
  197. //     {
  198. //      question:String,
  199. //      image:String,
  200. //      answer1:String,
  201. //      answer2:String,
  202. //      answer3:String,
  203. //      answer4:String,
  204. //      score:Number,
  205. //     },
  206. //
  207. //
  208. //   ],
  209. //   published:Boolean,
  210. //
  211. //
  212. //
  213. // },
  214. // { versionKey: false });
  215.  
  216. const QuizzSchema = new Schema({
  217.         name:String,
  218.         icon:String,
  219.         keywords:[String],
  220.         questions:[{
  221.           img:String,
  222.           question:String,
  223.           video:String,
  224.           txtAnswers:[String],
  225.           imgAnswers:[String],
  226.           solutions:[String],
  227.           points:Number
  228.         }
  229.         ],
  230.  
  231.         //published:Boolean
  232.     },
  233.     { versionKey: false });
  234.  
  235.  
  236.  
  237. /*
  238.  
  239.  name:String,
  240.  icon:String,
  241.  keywords:String,
  242.   question:String,
  243.   answer1:String,
  244.   answer2:String,
  245.   answer3:String,
  246.   answer4:String,
  247.   published:Boolean,
  248.  
  249.   scores:Number*/
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. // exports
  259. const Users = mongoose.model('Users', usersSchema);
  260. const Question = mongoose.model('Questions', QuestionSchema);
  261. const Quizz = mongoose.model('Quizzes',QuizzSchema)
  262.  
  263. module.exports = {};
  264. module.exports.users = Users;
  265. module.exports.questions = Question;
  266. module.exports.quizzSchema = Quizz;
  267.  
  268.  
  269.  
  270.  
  271. //// home
  272.  
  273.  
  274. import React, {Component} from 'react';
  275.  
  276. import {quizzes, users} from './examples';
  277. import {HTTP_SERVER_PORT_PICTURES} from './constants.js';
  278. import {Link} from 'react-router-dom';
  279. import axios from "axios";
  280.  
  281. class Thumbnails extends Component {
  282.     render (){
  283.         const divStyle = {
  284.             backgroundImage: 'url(' + HTTP_SERVER_PORT_PICTURES + this.props.quizz.icon + ')',
  285.             backgroundPosition: 'center',
  286.             backgroundSize: 'cover',
  287.         };
  288.  
  289.         return (
  290.             <Link className="quizItem" style={divStyle} to={'/quizz/' + this.props.quizz._id}>
  291.                 <span>
  292.                     <h1 className='quizName'>{this.props.quizz.name}</h1>
  293.                 </span>
  294.                 <img className='quizPicture' src={HTTP_SERVER_PORT_PICTURES + this.props.quizz.icon}/>
  295.             </Link>
  296.         )
  297.     }
  298. }
  299.  
  300. class Home extends Component {
  301.     render() {
  302.         return (
  303.             <div>
  304.                 <h1 id="quizCategories">Quizz categories : </h1>
  305.                 <div className='quizList'>
  306.                     {quizzes.map(q => <Thumbnails quizz={q}/>)}
  307.                 </div>
  308.             </div>
  309.         );
  310.     }
  311. }
  312.  
  313. export default Home;
Advertisement
Add Comment
Please, Sign In to add comment