Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { message_, question_, alt_, quizForm_, template_ } from './templates.js'
  2.  
  3. export class QuizGame extends window.HTMLElement {
  4.   constructor () {
  5.     super()
  6.  
  7.     this.attachShadow({ mode: 'open' })
  8.     this.shadowRoot.appendChild(template_.content.cloneNode(true))
  9.  
  10.     this._start = this.shadowRoot.querySelector('button')
  11.     this._container = this.shadowRoot.querySelector('.container')
  12.     this._inputField = this.shadowRoot.querySelector('.inputfield')
  13.  
  14.     this.questionURL = 'http://vhost3.lnu.se:20080/question/1'
  15.     this.answerURL = ''
  16.     this.answerParsed = ''
  17.   }
  18.  
  19.   static get observedAttributes () {
  20.     return []
  21.   }
  22.  
  23.   attributeChangedCallback () {
  24.  
  25.   }
  26.  
  27.   connectedCallback () {
  28.     this._start.addEventListener('click', event => {
  29.       this.startQuiz()
  30.     })
  31.   }
  32.  
  33.   disconnectedCallback () {
  34.     this._start.addEventListener('click', event => {
  35.       this.startQuiz()
  36.     })
  37.   }
  38.  
  39.   /**
  40.    * Quiz game logic
  41.    * TODOs:needs a timer
  42.    */
  43.   startQuiz () {
  44.     while (this._container.firstChild) {
  45.       this._container.removeChild(this._container.lastChild)
  46.     }
  47.     this._search(this.questionURL)
  48.   }
  49.  
  50.   /**
  51.    * Gets the response from the server
  52.    * @param {url} questionURL
  53.    */
  54.   async _search (questionURL) {
  55.     let searchResult = await window.fetch(questionURL)
  56.  
  57.     searchResult = await searchResult.json()
  58.  
  59.     this.parser(searchResult)
  60.   }
  61.  
  62.   /**
  63.    * Parses throgh obj and gets entries and values. Creates nodes based on values.
  64.    * @param {json Object} searchResult
  65.    */
  66.   parser (searchResult) {
  67.     console.log(searchResult)
  68.     if (!('alternatives' in searchResult)) {
  69.       this._inputField.appendChild(quizForm_.content.cloneNode(true))
  70.       let inputValue
  71.       const input = this.shadowRoot.querySelector('#answer')
  72.       input.addEventListener('keydown', event => {
  73.         inputValue = input.value + String.fromCharCode(event.which)
  74.         if (event.keyCode === 13) {
  75.           this.answerParsed = inputValue
  76.           console.log(inputValue)
  77.           return inputValue
  78.         }
  79.       })
  80.     }
  81.     for (const [key, value] of Object.entries(searchResult)) {
  82.       switch (key) {
  83.         case 'id':
  84.           break
  85.         case 'message':
  86.           break
  87.         case 'question':
  88.           question_.innerHTML = `<p>${value}</p>`
  89.           this._container.appendChild(question_.content.cloneNode(true))
  90.           break
  91.         case 'alternatives':
  92.           for (const [key, val] of Object.entries(value)) {
  93.             // answer should be 'alt1'
  94.             alt_.innerHTML = `<button>${val}</button>`
  95.             this._container.appendChild(alt_.content.cloneNode(true))
  96.           }
  97.           break
  98.         case 'nextURL':
  99.           // console.log(value)
  100.           //   !! figure out !!
  101.           this.answerURL = value
  102.           break
  103.         default:
  104.           break
  105.       }
  106.     }
  107.     this.sendAnswer(this.answerURL)
  108.   }
  109.  
  110.   /**
  111.    * sends the answer to server and gets response
  112.    * ${inputVal}
  113.    */
  114.   async sendAnswer (answerURL, answerParsed) {
  115.     const newAnswer = JSON.parse(answerParsed)
  116.     console.log(answerParsed)
  117.     const data = { answer: newAnswer }
  118.     const settings = {
  119.       method: 'Post',
  120.       headers: {
  121.         'Content-Type': 'application/json'
  122.       },
  123.       body: JSON.stringify(data)
  124.  
  125.     }
  126.     try {
  127.       console.log(answerURL)
  128.       const fetchResponse = await window.fetch(answerURL, settings)
  129.       const data = await fetchResponse.json()
  130.       console.log(data)
  131.       return data
  132.     } catch (error) {
  133.       console.log(error)
  134.       return error
  135.     }
  136.   }
  137.  
  138.   // _getVal () {
  139.   //   let inputValue
  140.   //   const input = this.shadowRoot.querySelector('#answer')
  141.   //   input.addEventListener('keydown', event => {
  142.   //     inputValue = input.value + String.fromCharCode(event.which)
  143.   //     if (event.keyCode === 13) {
  144.   //       this.answerURL = inputValue
  145.   //       console.log(inputValue)
  146.   //       return inputValue
  147.   //     }
  148.   //   })
  149.   // }
  150.  
  151.   _timer () {
  152.     // todo: timer to use in startQuiz()
  153.   }
  154. }
  155.  
  156. window.customElements.define('x-quiz-game', QuizGame)
  157.  
  158. /**
  159.  * TODO:
  160.  * create a function to get input field answer
  161.  * send the answer to sendAnswer()
  162.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement