mirror of
https://gitlab.com/MrFry/qmining-page
synced 2025-04-01 20:23:44 +02:00
All questions query fix, question search highlight fix
This commit is contained in:
parent
4bcbaa4cc1
commit
867568d446
2 changed files with 45 additions and 24 deletions
|
@ -1,6 +1,9 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
function highlightText(text, toHighlight) {
|
function highlightText(text, toHighlight) {
|
||||||
|
if (!text) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
const re = new RegExp(toHighlight, 'gi')
|
const re = new RegExp(toHighlight, 'gi')
|
||||||
return text.replace(re, `<mark>${toHighlight}</mark>`)
|
return text.replace(re, `<mark>${toHighlight}</mark>`)
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,21 +83,40 @@ export default function AllQuestions({ router }) {
|
||||||
const questionCount = data ? data.reduce(countReducer, 0) : 0
|
const questionCount = data ? data.reduce(countReducer, 0) : 0
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
router.replace(`${router.asPath.replace('.html', '')}`, undefined, {
|
|
||||||
shallow: true,
|
|
||||||
})
|
|
||||||
// TODO: fix dis
|
|
||||||
const querySearch = router.query.question
|
|
||||||
? decodeURIComponent(router.query.question)
|
|
||||||
: ''
|
|
||||||
|
|
||||||
console.log(querySearch)
|
|
||||||
|
|
||||||
fetchDbs().then((res) => {
|
fetchDbs().then((res) => {
|
||||||
setDbs(res)
|
setDbs(res)
|
||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
router.replace(`${router.asPath.replace('.html', '')}`, undefined, {
|
||||||
|
shallow: true,
|
||||||
|
})
|
||||||
|
const querySearch = router.query.question
|
||||||
|
? decodeURIComponent(router.query.question)
|
||||||
|
: ''
|
||||||
|
const db = router.query.question ? decodeURIComponent(router.query.db) : ''
|
||||||
|
|
||||||
|
setSearchTerm(querySearch)
|
||||||
|
setSelectedDb(db)
|
||||||
|
}, [router.query.question])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (dbs) {
|
||||||
|
if (selectedDb === 'all') {
|
||||||
|
fetchAllData(dbs).then((res) => {
|
||||||
|
setData(mergeData(res))
|
||||||
|
setFetchingData(false)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
fetchData(dbs[selectedDb]).then((res) => {
|
||||||
|
setData(res.data)
|
||||||
|
setFetchingData(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [selectedDb, dbs])
|
||||||
|
|
||||||
const renderDbSelector = () => {
|
const renderDbSelector = () => {
|
||||||
if (dbs) {
|
if (dbs) {
|
||||||
return (
|
return (
|
||||||
|
@ -127,23 +146,20 @@ export default function AllQuestions({ router }) {
|
||||||
<div className={'selectContainer'}>
|
<div className={'selectContainer'}>
|
||||||
<select
|
<select
|
||||||
defaultValue={-1}
|
defaultValue={-1}
|
||||||
|
value={selectedDb}
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
const key = event.target.value
|
const key = event.target.value
|
||||||
setData(null)
|
setData(null)
|
||||||
setFetchingData(true)
|
setFetchingData(true)
|
||||||
if (key === 'all') {
|
|
||||||
setSelectedDb(key)
|
router.replace(
|
||||||
fetchAllData(dbs).then((res) => {
|
`${router.pathname}${event.target.value &&
|
||||||
setData(mergeData(res))
|
'?question='}${encodeURIComponent(searchTerm)}&db=${key}`,
|
||||||
setFetchingData(false)
|
undefined,
|
||||||
})
|
{ shallow: true }
|
||||||
} else {
|
)
|
||||||
setSelectedDb(dbs[key].name)
|
|
||||||
fetchData(dbs[key]).then((res) => {
|
setSelectedDb(key)
|
||||||
setData(res.data)
|
|
||||||
setFetchingData(false)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<option disabled value={-1}>
|
<option disabled value={-1}>
|
||||||
|
@ -233,7 +249,9 @@ export default function AllQuestions({ router }) {
|
||||||
setSearchTerm(event.target.value)
|
setSearchTerm(event.target.value)
|
||||||
router.replace(
|
router.replace(
|
||||||
`${router.pathname}${event.target.value &&
|
`${router.pathname}${event.target.value &&
|
||||||
'?question='}${encodeURIComponent(event.target.value)}`,
|
'?question='}${encodeURIComponent(
|
||||||
|
event.target.value
|
||||||
|
)}&db=${selectedDb}`,
|
||||||
undefined,
|
undefined,
|
||||||
{ shallow: true }
|
{ shallow: true }
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue