1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-07-01 06:11:43 +02:00
market/src/components/templates/Search/index.tsx

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-07-01 18:13:32 +02:00
import React, { ReactElement, useState, useEffect } from 'react'
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
2020-07-01 18:13:32 +02:00
import SearchBar from '../../molecules/SearchBar'
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'
import { useOcean } from '@oceanprotocol/react'
2020-07-01 18:13:32 +02:00
export default function SearchPage({
location
}: {
location: Location
}): ReactElement {
const { config } = useOcean()
2020-07-01 18:13:32 +02:00
const parsed = queryString.parse(location.search)
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(() => {
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)
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()
}, [text, owner, tags, page, config.metadataCacheUri])
2020-07-01 18:13:32 +02:00
return (
<section className={styles.grid}>
<div className={styles.search}>
{(text || owner) && (
<SearchBar initialValue={(text || owner) as string} />
)}
2020-07-01 18:13:32 +02:00
</div>
<div className={styles.results}>
{loading ? <Loader /> : <AssetQueryList queryResult={queryResult} />}
2020-07-01 18:13:32 +02:00
</div>
</section>
)
}