mirror of
https://gitlab.com/MrFry/qmining-page
synced 2025-04-01 20:23:44 +02:00
Question searcher / subject browser improving
This commit is contained in:
parent
748a2d826c
commit
e317ac08b7
6 changed files with 179 additions and 3 deletions
73
src/components/QuestionSearchResult.js
Normal file
73
src/components/QuestionSearchResult.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
import React, { PureComponent } from 'react'
|
||||
|
||||
import Questions from './Questions.js'
|
||||
|
||||
import constants from '../constants.json'
|
||||
|
||||
class QuestionSearchResult extends PureComponent {
|
||||
render () {
|
||||
const { data, searchTerm } = this.props
|
||||
|
||||
let subjs = []
|
||||
let results = -1
|
||||
|
||||
const countReducer = (acc, subj) => {
|
||||
return acc + subj.Questions.length
|
||||
}
|
||||
|
||||
if (searchTerm) {
|
||||
subjs = data.Subjects.reduce((acc, subj) => {
|
||||
const resultQuestions = subj.Questions.reduce((qacc, question) => {
|
||||
const keys = [ 'Q', 'A', 'data' ]
|
||||
keys.some((key) => {
|
||||
if (typeof question[key] !== 'string') {
|
||||
return false
|
||||
}
|
||||
if (question[key] && question[key].toLowerCase().includes(searchTerm.toLowerCase())) {
|
||||
qacc.push(question)
|
||||
return true
|
||||
}
|
||||
})
|
||||
return qacc
|
||||
}, [])
|
||||
if (resultQuestions.length > 0) {
|
||||
acc.push({
|
||||
Name: subj.Name,
|
||||
Questions: resultQuestions
|
||||
})
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
results = subjs.reduce(countReducer, 0)
|
||||
} else {
|
||||
results = data.Subjects.reduce(countReducer, 0)
|
||||
}
|
||||
|
||||
const renderCount = () => {
|
||||
return (
|
||||
<div>
|
||||
{searchTerm ? '' : 'Kezdj el írni kereséshez!'} {results} {searchTerm ? 'találat' : 'kérdés' } {searchTerm ? subjs.length : data.Subjects.length} tárgy
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (results > constants.maxQuestionsToRender) {
|
||||
return renderCount()
|
||||
} else {
|
||||
return (
|
||||
<div >
|
||||
<div>
|
||||
{renderCount()}
|
||||
</div>
|
||||
<div>
|
||||
<Questions
|
||||
subjs={subjs}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default QuestionSearchResult
|
Loading…
Add table
Add a link
Reference in a new issue