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

DRY refactor, use AssetList component

This commit is contained in:
Matthias Kretschmann 2020-07-15 13:48:43 +02:00
parent c533194392
commit 532d84426f
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 46 additions and 57 deletions

View File

@ -10,3 +10,8 @@
gap: var(--spacer); gap: var(--spacer);
} }
} }
.empty {
color: var(--color-secondary);
font-size: var(--font-size-small);
}

View File

@ -1,18 +1,20 @@
import AssetTeaser from '../molecules/AssetTeaser' import AssetTeaser from '../molecules/AssetTeaser'
import React from 'react' import React from 'react'
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore' import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
import shortid from 'shortid'
import Pagination from '../molecules/Pagination' import Pagination from '../molecules/Pagination'
import { updateQueryStringParameter } from '../../utils' 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 { oceanConfig } from '../../../app.config'
declare type AssetListProps = { declare type AssetListProps = {
queryResult: QueryResult queryResult: QueryResult
} }
const AssetList: React.FC<AssetListProps> = ({ queryResult }) => { const AssetList: React.FC<AssetListProps> = ({ queryResult }) => {
// TODO: restore Pagination behavior
// 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) {
@ -38,20 +40,21 @@ const AssetList: React.FC<AssetListProps> = ({ queryResult }) => {
return ( return (
<> <>
<div className={styles.assetList}> <div className={styles.assetList}>
{queryResult && {queryResult && queryResult.totalResults > 0 ? (
queryResult.results.map((ddo: DDO) => { queryResult.results.map((ddo: DDO) => {
const { attributes }: MetaDataMarket = new DDO( const { attributes }: MetaDataMarket = new DDO(
ddo ddo
).findServiceByType('metadata') ).findServiceByType('metadata')
return ( return (
<AssetTeaser <AssetTeaser did={ddo.id} metadata={attributes} key={ddo.id} />
did={ddo.id}
metadata={attributes}
key={shortid.generate()}
/>
) )
})} })
) : (
<div className={styles.empty}>
No data sets found in {oceanConfig.metadataStoreUri}
</div>
)}
</div> </div>
{/* <Pagination {/* <Pagination
totalPages={queryResult.totalPages} totalPages={queryResult.totalPages}

View File

@ -1,9 +1 @@
.grid {
composes: assetList from '../organisms/AssetList.module.css';
margin-top: calc(var(--spacer) * 2);
}
.empty {
color: var(--color-secondary);
font-size: var(--font-size-small);
}

View File

@ -4,57 +4,46 @@ import { ServiceMetaDataMarket } from '../../@types/MetaData'
import AssetTeaser from '../molecules/AssetTeaser' import AssetTeaser from '../molecules/AssetTeaser'
import styles from './Home.module.css' import styles from './Home.module.css'
import { oceanConfig } from '../../../app.config' import { oceanConfig } from '../../../app.config'
import { DDO, MetadataStore, Logger } from '@oceanprotocol/lib' import { MetadataStore, Logger } from '@oceanprotocol/lib'
import AssetList from '../organisms/AssetList'
import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
async function getLatestAssets() {
try {
const metadataStore = new MetadataStore(
oceanConfig.metadataStoreUri,
Logger
)
const result = await metadataStore.queryMetadata({
page: 1,
offset: 10,
query: {},
sort: { created: -1 }
})
return result
} catch (error) {
console.error(error.message)
}
}
export default function HomePage(): ReactElement { export default function HomePage(): ReactElement {
const [assets, setAssets] = useState<DDO[]>() const [queryResult, setQueryResult] = useState<QueryResult>()
useEffect(() => { useEffect(() => {
async function getLatestAssets() { async function init() {
try { const results = await getLatestAssets()
const metadataStore = new MetadataStore( setQueryResult(results)
oceanConfig.metadataStoreUri,
Logger
)
const result = await metadataStore.queryMetadata({
page: 1,
offset: 10,
query: {},
sort: { created: -1 }
})
result && result.results && setAssets(result.results)
} catch (error) {
console.error(error.message)
}
} }
getLatestAssets() init()
}, []) }, [])
return ( return (
<> <>
<SearchBar large /> <SearchBar large />
<h3>Latest Data Sets</h3>
{assets && ( {queryResult && <AssetList queryResult={queryResult} />}
<div className={styles.grid}>
{assets.length ? (
assets.map((ddo: DDO) => {
const {
attributes
}: ServiceMetaDataMarket = ddo.findServiceByType('metadata')
return (
<AssetTeaser key={ddo.id} did={ddo.id} metadata={attributes} />
)
})
) : (
<div className={styles.empty}>
No data sets found in {oceanConfig.metadataStoreUri}
</div>
)}
</div>
)}
</> </>
) )
} }