Skip to content

Commit

Permalink
Added Custom Algorithm to Search Data (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyowen committed Aug 30, 2021
1 parent ab1d2be commit 7ff04a4
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/components/search.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Skeleton } from '@material-ui/lab'
import { initializeApp } from 'firebase/app'
import { Close } from '../lib/icons.component'
import { TextField, IconButton } from '@material-ui/core'
import { getDatabase, ref, onValue } from 'firebase/database'

const Search = ({ properties, config }: any) => {
const items: any = []
Expand All @@ -13,15 +14,27 @@ const Search = ({ properties, config }: any) => {
const [isFetching, setFetching] = useState<boolean>(false)

useEffect(() => {
setData({ ...data, isFetching: true})
if(keyword)
algoliasearch(String(process.env.REACT_APP_ALGOLIA_ID), String(process.env.REACT_APP_ALGOLIA_API_KEY)).initIndex('music').search(keyword)
.then(({ hits }) => setData({ ...data, isFetching: false, result: hits }))
// eslint-disable-next-line
}, [keyword])
onValue(ref(getDatabase(), 'data-dev/'), (snapshot) => setRawData(snapshot.val()))
}, [config])

useEffect(() => {
setFetching(true)
if(keyword) {
var result: any = []
for (let i=0; i<rawData.length; i++) {
console.log(rawData[i].title.includes(keyword))
if(rawData[i].title.includes(keyword)) {
result.push(rawData[i])
}
}
setResult(result)
}
setFetching(false)
// eslint-disable-next-line
}, [keyword])
console.log(results)
if(keyword)
if(data.result?.length === 0 && data.isFetching)
if(results?.length === 0 && isFetching)
for(let i=0; i<4; i++) {
items.push(
<div className="m-10" key={i}>
Expand Down

0 comments on commit 7ff04a4

Please sign in to comment.