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

114 lines
3.7 KiB
TypeScript
Raw Normal View History

import React, { ReactElement, useState, useEffect, useCallback } from 'react'
2021-10-13 18:48:59 +02:00
import Permission from '@shared/Permission'
import AssetList from '@shared/AssetList/AssetList'
2020-07-01 18:13:32 +02:00
import queryString from 'query-string'
import Filters from './Filters'
import Sort from './sort'
import { getResults } from './utils'
import { navigate } from 'gatsby'
2021-10-13 18:48:59 +02:00
import { updateQueryStringParameter } from '@utils/index'
import { useUserPreferences } from '@context/UserPreferences'
import { useCancelToken } from '@hooks/useCancelToken'
import styles from './index.module.css'
import { PagedAssets } from '../../../models/PagedAssets'
2020-07-01 18:13:32 +02:00
export default function SearchPage({
location,
setTotalResults,
setTotalPagesNumber
2020-07-01 18:13:32 +02:00
}: {
location: Location
setTotalResults: (totalResults: number) => void
setTotalPagesNumber: (totalPagesNumber: number) => void
2020-07-01 18:13:32 +02:00
}): ReactElement {
const [parsed, setParsed] = useState<queryString.ParsedQuery<string>>()
const { chainIds } = useUserPreferences()
const [queryResult, setQueryResult] = useState<PagedAssets>()
2020-07-15 15:47:25 +02:00
const [loading, setLoading] = useState<boolean>()
const [serviceType, setServiceType] = useState<string>()
const [accessType, setAccessType] = useState<string>()
const [sortType, setSortType] = useState<string>()
const [sortDirection, setSortDirection] = useState<string>()
const newCancelToken = useCancelToken()
2020-07-01 18:13:32 +02:00
useEffect(() => {
const parsed = queryString.parse(location.search)
const { sort, sortOrder, serviceType, accessType } = parsed
setParsed(parsed)
setServiceType(serviceType as string)
setAccessType(accessType as string)
setSortDirection(sortOrder as string)
setSortType(sort as string)
}, [location])
2020-07-01 18:13:32 +02:00
const updatePage = useCallback(
(page: number) => {
const { pathname, search } = location
const newUrl = updateQueryStringParameter(
pathname + search,
'page',
`${page}`
)
return navigate(newUrl)
},
[location]
)
const fetchAssets = useCallback(
async (parsed: queryString.ParsedQuery<string>, chainIds: number[]) => {
setLoading(true)
setTotalResults(undefined)
const queryResult = await getResults(parsed, chainIds, newCancelToken())
setQueryResult(queryResult)
setTotalResults(queryResult.totalResults)
setTotalPagesNumber(queryResult.totalPages)
setLoading(false)
},
[newCancelToken, setTotalPagesNumber, setTotalResults]
)
useEffect(() => {
if (!parsed || !queryResult) return
const { page } = parsed
if (queryResult.totalPages < Number(page)) updatePage(1)
}, [parsed, queryResult, updatePage])
useEffect(() => {
if (!parsed || !chainIds) return
fetchAssets(parsed, chainIds)
}, [parsed, chainIds, newCancelToken, fetchAssets])
2020-07-01 18:13:32 +02:00
return (
Issue 582 market rbac integration (#597) * adding env for RBAC server url to app.config.js * creating util function for requesting auth from the rbac server * fixing typing error * testing rbac request on homepage * removing console logs * importing RBAC url from config file * creating develpment .env file * return true if no rbac url env is set * creating permissions parent component * wrapping homepage content in permission element * wrapping publish in permissions wrapper * wrapping search results in permissions wrapper * wrapping asset actions in permissions element * creating an error alert for permission denied * updating react hook dependency * passing address to rbac component * sedning address to RBAC server * wrapping asset in permission component * removing unused import of Permission component * sending request based on address * chaning default permission case to restrict access * updating eventType as consume * Adding loader icon while waiting form permission response * only sending request to RBAC if address is defined * adding wallet connection info message * changing the env name and checking for undefined * updating .env.development * Check for undefined RBAC_URL in permissions component * removing .env.development and updating .env.example * updating .env.example comment * switching alert messages and reducing return statements * removing console.log message * fixing linting issue * Revert "fixing linting issue" This reverts commit 8bcb80be3d1ae32731b8c5b81b393dd614017fdc. * Fixing linting errors * pull from origin main * Revert "pull from origin main" This reverts commit 9535e41a5f5acfa26d2841942c29695855dd65bc.
2021-06-10 11:06:26 +02:00
<Permission eventType="browse">
<>
<div className={styles.search}>
<div className={styles.row}>
<Filters
serviceType={serviceType}
accessType={accessType}
Issue 582 market rbac integration (#597) * adding env for RBAC server url to app.config.js * creating util function for requesting auth from the rbac server * fixing typing error * testing rbac request on homepage * removing console logs * importing RBAC url from config file * creating develpment .env file * return true if no rbac url env is set * creating permissions parent component * wrapping homepage content in permission element * wrapping publish in permissions wrapper * wrapping search results in permissions wrapper * wrapping asset actions in permissions element * creating an error alert for permission denied * updating react hook dependency * passing address to rbac component * sedning address to RBAC server * wrapping asset in permission component * removing unused import of Permission component * sending request based on address * chaning default permission case to restrict access * updating eventType as consume * Adding loader icon while waiting form permission response * only sending request to RBAC if address is defined * adding wallet connection info message * changing the env name and checking for undefined * updating .env.development * Check for undefined RBAC_URL in permissions component * removing .env.development and updating .env.example * updating .env.example comment * switching alert messages and reducing return statements * removing console.log message * fixing linting issue * Revert "fixing linting issue" This reverts commit 8bcb80be3d1ae32731b8c5b81b393dd614017fdc. * Fixing linting errors * pull from origin main * Revert "pull from origin main" This reverts commit 9535e41a5f5acfa26d2841942c29695855dd65bc.
2021-06-10 11:06:26 +02:00
setServiceType={setServiceType}
setAccessType={setAccessType}
addFiltersToUrl
Issue 582 market rbac integration (#597) * adding env for RBAC server url to app.config.js * creating util function for requesting auth from the rbac server * fixing typing error * testing rbac request on homepage * removing console logs * importing RBAC url from config file * creating develpment .env file * return true if no rbac url env is set * creating permissions parent component * wrapping homepage content in permission element * wrapping publish in permissions wrapper * wrapping search results in permissions wrapper * wrapping asset actions in permissions element * creating an error alert for permission denied * updating react hook dependency * passing address to rbac component * sedning address to RBAC server * wrapping asset in permission component * removing unused import of Permission component * sending request based on address * chaning default permission case to restrict access * updating eventType as consume * Adding loader icon while waiting form permission response * only sending request to RBAC if address is defined * adding wallet connection info message * changing the env name and checking for undefined * updating .env.development * Check for undefined RBAC_URL in permissions component * removing .env.development and updating .env.example * updating .env.example comment * switching alert messages and reducing return statements * removing console.log message * fixing linting issue * Revert "fixing linting issue" This reverts commit 8bcb80be3d1ae32731b8c5b81b393dd614017fdc. * Fixing linting errors * pull from origin main * Revert "pull from origin main" This reverts commit 9535e41a5f5acfa26d2841942c29695855dd65bc.
2021-06-10 11:06:26 +02:00
/>
<Sort
sortType={sortType}
sortDirection={sortDirection}
setSortType={setSortType}
setSortDirection={setSortDirection}
/>
</div>
</div>
<div className={styles.results}>
<AssetList
assets={queryResult?.results}
showPagination
isLoading={loading}
page={queryResult?.page}
totalPages={queryResult?.totalPages}
onPageChange={updatePage}
/>
</div>
Issue 582 market rbac integration (#597) * adding env for RBAC server url to app.config.js * creating util function for requesting auth from the rbac server * fixing typing error * testing rbac request on homepage * removing console logs * importing RBAC url from config file * creating develpment .env file * return true if no rbac url env is set * creating permissions parent component * wrapping homepage content in permission element * wrapping publish in permissions wrapper * wrapping search results in permissions wrapper * wrapping asset actions in permissions element * creating an error alert for permission denied * updating react hook dependency * passing address to rbac component * sedning address to RBAC server * wrapping asset in permission component * removing unused import of Permission component * sending request based on address * chaning default permission case to restrict access * updating eventType as consume * Adding loader icon while waiting form permission response * only sending request to RBAC if address is defined * adding wallet connection info message * changing the env name and checking for undefined * updating .env.development * Check for undefined RBAC_URL in permissions component * removing .env.development and updating .env.example * updating .env.example comment * switching alert messages and reducing return statements * removing console.log message * fixing linting issue * Revert "fixing linting issue" This reverts commit 8bcb80be3d1ae32731b8c5b81b393dd614017fdc. * Fixing linting errors * pull from origin main * Revert "pull from origin main" This reverts commit 9535e41a5f5acfa26d2841942c29695855dd65bc.
2021-06-10 11:06:26 +02:00
</>
</Permission>
2020-07-01 18:13:32 +02:00
)
}