Added user questions

This commit is contained in:
MrFry 2020-03-08 12:01:10 +01:00
parent 261e04a0eb
commit b7dcc360bc
4 changed files with 116 additions and 139 deletions

View file

@ -30,7 +30,12 @@ export default function Layout (props) {
<Link href='/allQuestions' > <Link href='/allQuestions' >
<a <a
className={currPageName === 'allQuestions' ? 'active' : ''} className={currPageName === 'allQuestions' ? 'active' : ''}
>manual</a> >AllQuestions</a>
</Link>
<Link href='/userQuestions' >
<a
className={currPageName === 'userQuestions' ? 'active' : ''}
>userQuestions</a>
</Link> </Link>
</div> </div>
<div className='content'> <div className='content'>

View file

@ -1,54 +1,34 @@
import React, { PureComponent } from 'react' import React, { useState, useEffect } from 'react'
import fetch from 'unfetch' import fetch from 'unfetch'
import LoadingIndicator from '../components/LoadingIndicator.js' import LoadingIndicator from '../components/LoadingIndicator.js'
import Subject from '../components/Subject.js' import Subject from '../components/Subject.js'
import SubjectSelector from '../components/SubjectSelector.js' import SubjectSelector from '../components/SubjectSelector.js'
import constants from '../constants.json'
import Layout from '../components/layout' import Layout from '../components/layout'
class AllQuestions extends PureComponent { import constants from '../constants.json'
constructor (props) {
super(props)
this.state = { export default function AllQuestions (props) {
loaded: false, const [data, setData] = useState(null)
activeSubjName: '', const [loaded, setLoaded] = useState(false)
searchTerm: '' const [activeSubjName, setActiveSubjName] = useState('')
} const [searchTerm, setSearchTerm] = useState('')
// TODO: fetches twice
useEffect(() => {
console.info('Fetching data') console.info('Fetching data')
fetch(`${constants.serverUrl}data.json`) // eslint-disable-line fetch(`${constants.serverUrl}data.json`)
.then((resp) => { .then((resp) => {
return resp.json() return resp.json()
}) })
.then((data) => { .then((data) => {
this.data = data setData(data)
this.setState({ setLoaded(true)
loaded: true
}) })
}) }, [loaded])
}
onSubjSelect (name) {
this.setState({
activeSubjName: name
})
}
searchBarOnChange (e) {
let text = e.target.value
this.setState({
searchTerm: text
})
}
render () {
const { loaded, activeSubjName, searchTerm } = this.state
if (loaded) { if (loaded) {
let currSubj = this.data.Subjects.find((subj) => { let currSubj = data.Subjects.find((subj) => {
return subj.Name === activeSubjName return subj.Name === activeSubjName
}) })
@ -61,17 +41,15 @@ class AllQuestions extends PureComponent {
className='searchBar' className='searchBar'
type='text' type='text'
value={searchTerm} value={searchTerm}
onChange={this.searchBarOnChange.bind(this)} onChange={(e) => { setSearchTerm(e.target.value) }}
/> />
</div> </div>
<SubjectSelector <SubjectSelector
data={this.data} data={data}
activeSubjName={activeSubjName} activeSubjName={activeSubjName}
searchTerm={searchTerm} searchTerm={searchTerm}
onSubjSelect={this.onSubjSelect.bind(this)} onSubjSelect={(subjName) => { setActiveSubjName(subjName) }}
/> />
<hr /> <hr />
<div> <div>
<Subject <Subject
@ -89,6 +67,3 @@ class AllQuestions extends PureComponent {
) )
} }
} }
}
export default AllQuestions

View file

@ -63,6 +63,8 @@ másnak a legfrissebb adatok állnak rendelkezésre.</b>
</center> </center>
<hr /> <hr />
<table style={{ tableLayout: 'fixed', verticalAlign: 'top', width: '100%' }}> <table style={{ tableLayout: 'fixed', verticalAlign: 'top', width: '100%' }}>
<tbody>
<tr>
<td> <td>
<p /> Először is tölts le egy userscript futtató kiegészítőt a böngésződhöz. Én <a <p /> Először is tölts le egy userscript futtató kiegészítőt a böngésződhöz. Én <a
href='https://www.tampermonkey.net/'>Tampermonkeyt</a> használok, és ezzel van tesztelve a href='https://www.tampermonkey.net/'>Tampermonkeyt</a> használok, és ezzel van tesztelve a
@ -114,6 +116,8 @@ másnak a legfrissebb adatok állnak rendelkezésre.</b>
<td width='20%'> <td width='20%'>
<img style={{ maxWidth: '100%', maxHeight: '100%' }} src='img/rtfm.jpg' alt='img' /> <img style={{ maxWidth: '100%', maxHeight: '100%' }} src='img/rtfm.jpg' alt='img' />
</td> </td>
</tr>
</tbody>
</table> </table>
<center> <center>
<h3>Eddigi teszt kérdések:</h3> <h3>Eddigi teszt kérdések:</h3>

View file

@ -1,37 +1,29 @@
import React, { PureComponent } from 'react' import React, { useState, useEffect } from 'react'
import LoadingIndicator from '../LoadingIndicator' import LoadingIndicator from '../components/LoadingIndicator'
import Layout from '../components/layout'
import constants from '../../constants.json' import constants from '../constants.json'
class UserQuestions extends PureComponent { export default function UserQuestions (props) {
constructor (props) { const [loaded, setLoaded] = useState(false)
super(props) const [qa, setQa] = useState(null)
this.state = {
loaded: false
}
useEffect(() => {
console.info('Fetching qa.json') console.info('Fetching qa.json')
fetch(`${constants.serverUrl}qa.json`) // eslint-disable-line fetch(`${constants.serverUrl}qa.json`) // eslint-disable-line
.then((resp) => { .then((resp) => {
return resp.json() return resp.json()
}) })
.then((data) => { .then((data) => {
this.qa = data setQa(data)
setLoaded(true)
this.setState({
loaded: true
}) })
}) }, [loaded])
}
render () {
const { loaded } = this.state
if (loaded) { if (loaded) {
let questions = Object.keys(this.qa).map((key, i) => { let questions = Object.keys(qa).map((key, i) => {
let q = this.qa[key] let q = qa[key]
return ( return (
<div key={key} className='uquestioncontainer'> <div key={key} className='uquestioncontainer'>
<div > <div >
@ -50,16 +42,17 @@ class UserQuestions extends PureComponent {
}).reverse() }).reverse()
return ( return (
<Layout currPageName='UserQuestions' >
<div className='uquestionscontainer'> <div className='uquestionscontainer'>
{questions} {questions}
</div> </div>
</Layout>
) )
} else { } else {
return ( return (
<Layout currPageName='UserQuestions' >
<LoadingIndicator /> <LoadingIndicator />
</Layout>
) )
} }
} }
}
export default UserQuestions