SHARE
TWEET

Untitled

a guest Jun 26th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import {Switch} from  'react-router-dom'
  3. import {Route} from 'react-router-dom'
  4. import {TeamSelection} from "./TeamSelection";
  5. import {CategorySelection} from "./CategorySelection";
  6. import {ReviewAnswer} from "./ReviewAnswer"
  7. import {SelectQuestion} from "./SelectQuestion";
  8. import {StartCurrentQuestion} from "./StartCurrentQuestion";
  9. import {RoundOver} from "./RoundOver";
  10. import {Start} from './Start'
  11.  
  12. export class QuizzmasterApp extends React.Component{
  13.     constructor(props){
  14.         super(props)
  15.         this.state = {
  16.             quizId: "",
  17.             refreshTeams: false,
  18.             teams:[]
  19.         }
  20.         this.onReceiveQuizzId = this.onReceiveQuizzId.bind(this)
  21.     }
  22.  
  23.     async getTeams() {
  24.         const response = await fetch(`http://localhost:8081/game/${this.state.quizId}/team`)
  25.         const responseJson = await response.json()
  26.         if (!response.ok) {
  27.             throw new Error(`HTTP request went wrong: got "${response.statusText}" `)
  28.         }
  29.         else {
  30.             let teams = responseJson.map((x) => x.name)
  31.             this.setState({teams: teams})
  32.         }
  33.     }
  34.  
  35.  
  36.     async handleTeamMessage() {
  37.         let teams = await this.getTeams();
  38.         this.setState({teams:teams})
  39.     };
  40.  
  41.     onReceiveQuizzId(quizId){
  42.         localStorage.setItem('quizId', quizId)
  43.         this.setState({quizId: quizId})
  44.     }
  45.  
  46.     componentDidMount() {
  47.         this.setupWebsocket()
  48.     }
  49.  
  50.     setupWebsocket() {
  51.         let wsConnection = new WebSocket("ws://localhost:8081");
  52.         wsConnection.onopen = () => {
  53.             wsConnection.send(JSON.stringify({messageType: "startQuiz"}))
  54.         }
  55.         wsConnection.onmessage = (eventInfo) => {
  56.             let message = JSON.parse(eventInfo.data)
  57.             switch (message.messageType) {
  58.                 case "newGame":
  59.                     let quizId = message.id
  60.                     this.setState({quizId: quizId})
  61.                     this.onReceiveQuizzId(quizId)
  62.                     break;
  63.                 case "refreshTeams":
  64.                     console.log("refresh teams");
  65.                     this.handleTeamMessage();
  66.                 break
  67.             }
  68.         }
  69.         this.setState({websocket: wsConnection})
  70.     }
  71.  
  72.     render() {
  73.         return(
  74.             <Switch>
  75.                 <Route path ="/applications" render={() => <TeamSelection teams={this.state.teams}/>}/>
  76.                 <Route path ="/selectCategories" render={() => <CategorySelection />}/>
  77.                 <Route path ="/reviewAnswers" render={() => <ReviewAnswer/> }/>
  78.                 <Route path ="/selectQuestion" render={() => <SelectQuestion /> }/>
  79.                 <Route path ="/startNextQuestion" render={() => <StartCurrentQuestion />}/>
  80.                 <Route path ="/roundOver" render={() => <RoundOver />}/>
  81.                 <Route exact path="/" render={() => <Start/>}/>
  82.             </Switch>
  83.         )
  84.     }
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top