2020-07-01 18:13:32 +02:00
|
|
|
import React, { ReactElement, useState, useEffect } from 'react'
|
2020-10-13 17:05:57 +02:00
|
|
|
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
2020-07-01 18:13:32 +02:00
|
|
|
import SearchBar from '../../molecules/SearchBar'
|
2020-10-29 15:12:05 +01:00
|
|
|
import AssetQueryList from '../../organisms/AssetQueryList'
|
2020-07-01 18:13:32 +02:00
|
|
|
import styles from './index.module.css'
|
|
|
|
import queryString from 'query-string'
|
|
|
|
import { getResults } from './utils'
|
2020-07-15 15:33:45 +02:00
|
|
|
import Loader from '../../atoms/Loader'
|
2020-08-11 15:56:13 +02:00
|
|
|
import { useOcean } from '@oceanprotocol/react'
|
2020-07-01 18:13:32 +02:00
|
|
|
|
|
|
|
export default function SearchPage({
|
|
|
|
location
|
|
|
|
}: {
|
|
|
|
location: Location
|
|
|
|
}): ReactElement {
|
2020-08-11 15:56:13 +02:00
|
|
|
const { config } = useOcean()
|
2020-07-01 18:13:32 +02:00
|
|
|
const parsed = queryString.parse(location.search)
|
2020-11-03 14:57:40 +01:00
|
|
|
const { text, owner, tags, page } = parsed
|
2020-07-01 18:13:32 +02:00
|
|
|
const [queryResult, setQueryResult] = useState<QueryResult>()
|
2020-07-15 15:47:25 +02:00
|
|
|
const [loading, setLoading] = useState<boolean>()
|
2020-07-01 18:13:32 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-11-17 14:21:29 +01:00
|
|
|
if (!config?.metadataCacheUri) return
|
|
|
|
|
2020-07-01 18:13:32 +02:00
|
|
|
async function initSearch() {
|
2020-07-15 15:47:25 +02:00
|
|
|
setLoading(true)
|
2020-10-13 17:05:57 +02:00
|
|
|
const queryResult = await getResults(parsed, config.metadataCacheUri)
|
2020-07-07 23:00:16 +02:00
|
|
|
setQueryResult(queryResult)
|
2020-07-15 15:33:45 +02:00
|
|
|
setLoading(false)
|
2020-07-01 18:13:32 +02:00
|
|
|
}
|
|
|
|
initSearch()
|
2020-11-03 14:57:40 +01:00
|
|
|
}, [text, owner, tags, page, config.metadataCacheUri])
|
2020-07-01 18:13:32 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<section className={styles.grid}>
|
|
|
|
<div className={styles.search}>
|
2020-11-03 14:57:40 +01:00
|
|
|
{(text || owner) && (
|
|
|
|
<SearchBar initialValue={(text || owner) as string} />
|
|
|
|
)}
|
2020-07-01 18:13:32 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={styles.results}>
|
2020-10-29 15:12:05 +01:00
|
|
|
{loading ? <Loader /> : <AssetQueryList queryResult={queryResult} />}
|
2020-07-01 18:13:32 +02:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
)
|
|
|
|
}
|