Files
old-kitabcitab-frontend/kitabcitab/components/Result.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-02-16 00:05:13 +01:00
import { React, useEffect, useState} from 'react'
import { useRouter } from 'next/router'
2023-01-03 13:31:47 +01:00
2023-02-16 00:05:13 +01:00
const Result = ({result, term}) => {
2023-01-03 13:31:47 +01:00
const router = useRouter()
2023-02-16 00:05:13 +01:00
const {book, writer, text} = result._source
const [title, setTitle] = useState()
let sentences
const checkForMatches = (props) => {
2023-03-08 12:08:29 +01:00
if(props.length === 1) return setTitle(props)
if(props.length > 1 ){
return sentences.map((sentence) => {
if(sentence.toLowerCase().includes(term.toLowerCase())) setTitle(sentence)
else setTitle(props[0])
2023-02-16 00:05:13 +01:00
})
2023-03-08 12:08:29 +01:00
}
2023-02-16 00:05:13 +01:00
}
const splitText = () => {
try{
return text.match( /[^\.!\?]+[\.!\?]+/g)
}catch{err => console.log(err)}
}
useEffect(() => {
sentences = splitText()
checkForMatches(sentences)
2023-03-08 12:08:29 +01:00
}, [])
2023-02-16 00:05:13 +01:00
// ako term nije pronadjen u textu onda
// treba provjeriti duzinu niza, ako niz ima jednu recenicu, recenica treba biti title, ako ne, naci medju recenicama onu koja ima term u sebi
2023-01-03 13:31:47 +01:00
return (
<div onClick={() => (router.push({pathname: `/${result._type}/${result._id}`}))} className='
2023-01-03 13:31:47 +01:00
self-end
bg-[#202124]
w-[100%]
2023-01-04 02:30:44 +01:00
laptop:max-w-[50vw]
2023-01-03 13:31:47 +01:00
p-3 m-auto
border-4
border-[#303134]
font-serif text-base
text-lg
tablet:text-2xl text-white
laptop:text-lg
'
>
2023-02-16 00:05:13 +01:00
<p className='m-3'>{title}</p>
<p className='m-3'>"{book}" - {writer} - str. {result._id}.</p>
</div>
2023-01-03 13:31:47 +01:00
)
}
export default Result