1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

get config dynamically from useOcean in all components

This commit is contained in:
Matthias Kretschmann 2020-08-11 15:56:13 +02:00
parent dce3b10f01
commit a7dc94f219
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 17 additions and 15 deletions

View File

@ -7,18 +7,17 @@ import { updateQueryStringParameter } from '../../utils'
import styles from './AssetList.module.css' import styles from './AssetList.module.css'
import { MetadataMarket } from '../../@types/Metadata' import { MetadataMarket } from '../../@types/Metadata'
import { DDO } from '@oceanprotocol/lib' import { DDO } from '@oceanprotocol/lib'
import { getDefaultOceanConfig } from '../../../app.config' import { useOcean } from '@oceanprotocol/react'
declare type AssetListProps = { declare type AssetListProps = {
queryResult: QueryResult queryResult: QueryResult
} }
const AssetList: React.FC<AssetListProps> = ({ queryResult }) => { const AssetList: React.FC<AssetListProps> = ({ queryResult }) => {
const { config } = useOcean()
const location = useLocation() const location = useLocation()
const navigate = useNavigate() const navigate = useNavigate()
const { metadataStoreUri } = getDefaultOceanConfig()
// Construct the urls on the pagination links. This is only for UX, // Construct the urls on the pagination links. This is only for UX,
// since the links are no <Link> they will not work by itself. // since the links are no <Link> they will not work by itself.
function hrefBuilder(pageIndex: number) { function hrefBuilder(pageIndex: number) {
@ -54,7 +53,7 @@ const AssetList: React.FC<AssetListProps> = ({ queryResult }) => {
}) })
) : ( ) : (
<div className={styles.empty}> <div className={styles.empty}>
No results found in {metadataStoreUri} No results found in {config.metadataStoreUri}
</div> </div>
)} )}
</div> </div>

View File

@ -6,11 +6,10 @@ import AssetList from '../organisms/AssetList'
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore' import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
import Container from '../atoms/Container' import Container from '../atoms/Container'
import Loader from '../atoms/Loader' import Loader from '../atoms/Loader'
import { getDefaultOceanConfig } from '../../../app.config' import { useOcean } from '@oceanprotocol/react'
async function getLatestAssets() { async function getLatestAssets(metadataStoreUri: string) {
try { try {
const { metadataStoreUri } = getDefaultOceanConfig()
const metadataStore = new MetadataStore(metadataStoreUri, Logger) const metadataStore = new MetadataStore(metadataStoreUri, Logger)
const result = await metadataStore.queryMetadata({ const result = await metadataStore.queryMetadata({
@ -27,12 +26,13 @@ async function getLatestAssets() {
} }
export default function HomePage(): ReactElement { export default function HomePage(): ReactElement {
const { config } = useOcean()
const [queryResult, setQueryResult] = useState<QueryResult>() const [queryResult, setQueryResult] = useState<QueryResult>()
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
useEffect(() => { useEffect(() => {
async function init() { async function init() {
const results = await getLatestAssets() const results = await getLatestAssets(config.metadataStoreUri)
setQueryResult(results) setQueryResult(results)
setLoading(false) setLoading(false)
} }

View File

@ -6,7 +6,7 @@ import { MetadataMarket, ServiceMetadataMarket } from '../../@types/Metadata'
import { MetadataStore, Logger, DDO } from '@oceanprotocol/lib' import { MetadataStore, Logger, DDO } from '@oceanprotocol/lib'
import Alert from '../../components/atoms/Alert' import Alert from '../../components/atoms/Alert'
import Loader from '../../components/atoms/Loader' import Loader from '../../components/atoms/Loader'
import { getDefaultOceanConfig } from '../../../app.config' import { useOcean } from '@oceanprotocol/react'
export default function PageTemplateAssetDetails({ export default function PageTemplateAssetDetails({
did, did,
@ -15,6 +15,7 @@ export default function PageTemplateAssetDetails({
did: string did: string
uri: string uri: string
}): ReactElement { }): ReactElement {
const { config } = useOcean()
const [metadata, setMetadata] = useState<MetadataMarket>() const [metadata, setMetadata] = useState<MetadataMarket>()
const [title, setTitle] = useState<string>() const [title, setTitle] = useState<string>()
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
@ -23,8 +24,7 @@ export default function PageTemplateAssetDetails({
useEffect(() => { useEffect(() => {
async function init() { async function init() {
try { try {
const { metadataStoreUri } = getDefaultOceanConfig() const metadataStore = new MetadataStore(config.metadataStoreUri, Logger)
const metadataStore = new MetadataStore(metadataStoreUri, Logger)
const ddo = await metadataStore.retrieveDDO(did) const ddo = await metadataStore.retrieveDDO(did)
setDdo(ddo) setDdo(ddo)

View File

@ -7,6 +7,7 @@ import styles from './index.module.css'
import queryString from 'query-string' import queryString from 'query-string'
import { getResults } from './utils' import { getResults } from './utils'
import Loader from '../../atoms/Loader' import Loader from '../../atoms/Loader'
import { useOcean } from '@oceanprotocol/react'
export declare type SearchPageProps = { export declare type SearchPageProps = {
text: string | string[] text: string | string[]
@ -19,6 +20,7 @@ export default function SearchPage({
}: { }: {
location: Location location: Location
}): ReactElement { }): ReactElement {
const { config } = useOcean()
const parsed = queryString.parse(location.search) const parsed = queryString.parse(location.search)
const { text, tag, page } = parsed const { text, tag, page } = parsed
const [queryResult, setQueryResult] = useState<QueryResult>() const [queryResult, setQueryResult] = useState<QueryResult>()
@ -27,7 +29,7 @@ export default function SearchPage({
useEffect(() => { useEffect(() => {
async function initSearch() { async function initSearch() {
setLoading(true) setLoading(true)
const queryResult = await getResults(parsed) const queryResult = await getResults(parsed, config.metadataStoreUri)
setQueryResult(queryResult) setQueryResult(queryResult)
setLoading(false) setLoading(false)
} }

View File

@ -4,7 +4,6 @@ import {
} from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore' } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
import { priceQueryParamToWei } from '../../../utils' import { priceQueryParamToWei } from '../../../utils'
import { MetadataStore, Logger } from '@oceanprotocol/lib' import { MetadataStore, Logger } from '@oceanprotocol/lib'
import { getDefaultOceanConfig } from '../../../../app.config'
export function getSearchQuery( export function getSearchQuery(
page?: string | string[], page?: string | string[],
@ -32,7 +31,10 @@ export function getSearchQuery(
} as SearchQuery } as SearchQuery
} }
export async function getResults(params: any): Promise<QueryResult> { export async function getResults(
params: any,
metadataStoreUri: string
): Promise<QueryResult> {
const { text, tag, page, offset, minPrice, maxPrice } = params const { text, tag, page, offset, minPrice, maxPrice } = params
const minPriceParsed = priceQueryParamToWei( const minPriceParsed = priceQueryParamToWei(
@ -52,7 +54,6 @@ export async function getResults(params: any): Promise<QueryResult> {
]) ])
: undefined : undefined
const { metadataStoreUri } = getDefaultOceanConfig()
const metadataStore = new MetadataStore(metadataStoreUri, Logger) const metadataStore = new MetadataStore(metadataStoreUri, Logger)
const queryResult = await metadataStore.queryMetadata( const queryResult = await metadataStore.queryMetadata(
getSearchQuery(page, offset, text, tag, priceQuery) getSearchQuery(page, offset, text, tag, priceQuery)