|
| 1 | +import React, { ChangeEvent, FormEvent, useState } from 'react' |
| 2 | +import { Socket } from 'socket.io-client' |
| 3 | + |
| 4 | +interface Round { |
| 5 | + round: number |
| 6 | +} |
| 7 | +interface Sock{ |
| 8 | + socket: Socket; |
| 9 | + toggle: () => void; |
| 10 | +} |
| 11 | +const numberOfAnswers = [5, 1, 4, 12, 10]; |
| 12 | +export function NewQuestions (props: Sock): JSX.Element { |
| 13 | + |
| 14 | + const [state, setState]= useState({ |
| 15 | + question: "", |
| 16 | + round: -1, |
| 17 | + answer: [], |
| 18 | + correctAnswer: [], |
| 19 | + socket: props.socket, |
| 20 | + error: "", |
| 21 | + }); |
| 22 | + |
| 23 | + state.socket.on('db', (data) => { |
| 24 | + if(data.succeed){ |
| 25 | + setState({...state, question: "", round: -1, answer: []}) |
| 26 | + }else{ |
| 27 | + console.log("Something happend", data.error); |
| 28 | + setState({...state, error: data.error}); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + function handleQuestionChange(event: ChangeEvent<HTMLInputElement>): void { |
| 33 | + setState({...state, question: event.target.value}) |
| 34 | + } |
| 35 | + |
| 36 | + function handleRoundChange(event: ChangeEvent<HTMLSelectElement>): void { |
| 37 | + const copy = []; |
| 38 | + const round = parseInt(event.target.value); |
| 39 | + copy.fill("", numberOfAnswers[round]); |
| 40 | + setState({...state, round: round, answer: copy}); |
| 41 | + } |
| 42 | + |
| 43 | + function handleAnswerChange(event: ChangeEvent<HTMLInputElement>): void{ |
| 44 | + const name = event.target.name; |
| 45 | + const id: number = parseInt(name.slice(1))-1; |
| 46 | + const copy: any[] = state.answer; |
| 47 | + copy[id] = event.target.value; |
| 48 | + setState({...state, answer: copy}) |
| 49 | + } |
| 50 | + |
| 51 | + async function handleQuestionSubmit(event: FormEvent<HTMLFormElement>): Promise<any> { |
| 52 | + event.preventDefault(); |
| 53 | + state.socket.emit('newQ', { |
| 54 | + round: state.round, |
| 55 | + question: state.question, |
| 56 | + answer: state.answer |
| 57 | + }) |
| 58 | + setState({...state, round: -1, error: "", answer: [], question: ""}); |
| 59 | + props.toggle(); |
| 60 | + } |
| 61 | + |
| 62 | + function AnswerQuestion(props: Round): JSX.Element{ |
| 63 | + if(props.round >= 0) { |
| 64 | + const times = numberOfAnswers[props.round]; |
| 65 | + const qInput = []; |
| 66 | + let col = "border-transparent"; |
| 67 | + for(let i=1; i<= times; i++){ |
| 68 | + if(props.round === 3){ |
| 69 | + if(i <= 3){ |
| 70 | + col = "border-red-500"; |
| 71 | + }else if(i > 3 && i <= 6 ){ |
| 72 | + col = "border-blue-500"; |
| 73 | + }else{ |
| 74 | + col = "border-yellow-500"; |
| 75 | + } |
| 76 | + } |
| 77 | + qInput.push( |
| 78 | + <input |
| 79 | + key={i} |
| 80 | + name={"a" + i} |
| 81 | + type="text" |
| 82 | + placeholder={"answer " + i} |
| 83 | + id={"a" + i} |
| 84 | + className={"mt-3 form-control rounded-full py-3 px-6 border-4 " + col} |
| 85 | + onChange={handleAnswerChange} |
| 86 | + value={state.answer[i-1]} |
| 87 | + /> |
| 88 | + ) |
| 89 | + } |
| 90 | + return (<div className={"flex flex-col"}>{qInput}</div>); |
| 91 | + } else{ |
| 92 | + return (<></>) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <form |
| 99 | + className={"mt-10 flex flex-row"} |
| 100 | + style={{height: "0%"}} |
| 101 | + id="newQuestion" |
| 102 | + onSubmit={handleQuestionSubmit}> |
| 103 | + <div className={"px-2"}> |
| 104 | + <label className={"text-white flex-none self-start text-xl"} htmlFor="username">Round</label> |
| 105 | + <br /> |
| 106 | + <select className={"flex-none self-start rounded-full py-3 px-6 mt-3"} id="round" name="Round" onChange={handleRoundChange} > |
| 107 | + <option value="1">1</option> |
| 108 | + <option value="2">2</option> |
| 109 | + <option value="3">3</option> |
| 110 | + <option value="4">4</option> |
| 111 | + <option value="5">5</option> |
| 112 | + <option value="0">Finale</option> |
| 113 | + </select> |
| 114 | + </div> |
| 115 | + {state.round >= 0 && |
| 116 | + <> |
| 117 | + <div className={"px-2"}> |
| 118 | + <label className={"text-white text-xl flex-none self-start"} htmlFor="question">Question</label> |
| 119 | + <br /> |
| 120 | + <input |
| 121 | + name="question" |
| 122 | + type="text" |
| 123 | + placeholder="question" |
| 124 | + id="question" |
| 125 | + className={"mt-3 form-control rounded-full flex-none self-start py-3 px-6"} |
| 126 | + onChange={handleQuestionChange} |
| 127 | + value={state.question} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + <div className={"px-2"}> |
| 131 | + <label className={"text-white text-xl flex-none self-start"} htmlFor="question">Answers</label> |
| 132 | + <br /> |
| 133 | + {AnswerQuestion({round: state.round})} |
| 134 | + </div> |
| 135 | + <div className={"px-2"}> |
| 136 | + <button |
| 137 | + id="submitButton" |
| 138 | + type="submit" |
| 139 | + className="mt-10 bg-dsm flex-none self-start p-5 text-white rounded-full py-3 px-6" |
| 140 | + > |
| 141 | + Submit |
| 142 | + </button> |
| 143 | + </div> |
| 144 | + </> |
| 145 | + } |
| 146 | + </form> |
| 147 | + </> |
| 148 | + |
| 149 | + ); |
| 150 | +} |
0 commit comments