From 7860aecd984d17db4efc5d078773791011b6f5cd Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 6 Jul 2020 12:49:30 +0200 Subject: [PATCH] prototype client-only route for asset details --- gatsby-node.js | 20 +++- ...dex.module.css => AssetContent.module.css} | 16 +-- .../templates/AssetDetails/AssetContent.tsx | 103 +++++++++++++++++ .../templates/AssetDetails/index.tsx | 108 +----------------- src/pages/asset/index.tsx | 56 +++++++++ 5 files changed, 185 insertions(+), 118 deletions(-) rename src/components/templates/AssetDetails/{index.module.css => AssetContent.module.css} (81%) create mode 100644 src/components/templates/AssetDetails/AssetContent.tsx create mode 100644 src/pages/asset/index.tsx diff --git a/gatsby-node.js b/gatsby-node.js index 959f06f5f..b7901aed2 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -29,13 +29,29 @@ exports.createPages = async ({ actions, reporter }) => { const assetDetailsTemplate = path.resolve( `src/components/templates/AssetDetails/index.tsx` ) - assets.forEach((did) => { + + await assets.forEach(async (did) => { const path = `/asset/${did}` - createPage({ + await createPage({ path, component: assetDetailsTemplate, context: { did } }) }) } + +exports.onCreatePage = async ({ page, actions }) => { + const { createPage } = actions + + // page.matchPath is a special key that's used for matching pages + // only on the client. + const handleClientSideOnly = page.path.match(/^\/asset/) + + if (handleClientSideOnly) { + page.matchPath = '/asset/*' + + // Update the page. + createPage(page) + } +} diff --git a/src/components/templates/AssetDetails/index.module.css b/src/components/templates/AssetDetails/AssetContent.module.css similarity index 81% rename from src/components/templates/AssetDetails/index.module.css rename to src/components/templates/AssetDetails/AssetContent.module.css index 7b0079597..a0ef532c1 100644 --- a/src/components/templates/AssetDetails/index.module.css +++ b/src/components/templates/AssetDetails/AssetContent.module.css @@ -17,11 +17,6 @@ } } -.sectionTitle { - font-size: var(--font-size-large); - color: var(--color-secondary); -} - .meta { margin-bottom: var(--spacer); color: var(--color-secondary); @@ -32,14 +27,11 @@ margin-bottom: 0; } -.loaderWrap { - text-align: center; - display: flex; - align-items: center; - justify-content: center; - height: 100%; - min-height: 70vh; +.sectionTitle { + font-size: var(--font-size-large); + color: var(--color-secondary); } + .buttonGroup { margin-top: var(--spacer); margin-bottom: var(--spacer); diff --git a/src/components/templates/AssetDetails/AssetContent.tsx b/src/components/templates/AssetDetails/AssetContent.tsx new file mode 100644 index 000000000..aac2b96c6 --- /dev/null +++ b/src/components/templates/AssetDetails/AssetContent.tsx @@ -0,0 +1,103 @@ +import { MetaDataMarket } from '../../../@types/MetaData' +import React, { ReactElement } from 'react' +import { useOcean } from '@oceanprotocol/react' +import Time from '../../atoms/Time' +import { Link } from 'gatsby' +import Markdown from '../../atoms/Markdown' +import Tags from '../../atoms/Tags' +import MetaFull from './MetaFull' +import Compute from '../../organisms/Compute' +import Consume from '../../organisms/Consume' +import MetaSecondary from './MetaSecondary' +import styles from './AssetContent.module.css' + +export interface AssetContentProps { + metadata: MetaDataMarket + did: string + path?: string +} + +export default function AssetContent({ + metadata, + did +}: AssetContentProps): ReactElement { + const { ocean, balanceInOcean } = useOcean() + const { datePublished } = metadata.main + const { + description, + copyrightHolder, + categories, + tags, + access + } = metadata.additionalInformation + const isCompute = access && access === 'Compute' + + // const { curation } = metadata + + // const { getCuration } = useMetadata() + // const [rating, setRating] = useState(curation ? curation.rating : 0) + // const [numVotes, setNumVotes] = useState( + // curation ? curation.numVotes : 0 + // ) + + // const onVoteUpdate = async () => { + // const { rating, numVotes } = await getCuration(did) + + // setRating(rating) + // setNumVotes(numVotes) + // } + + return ( +
+
+ + +

Summary

+ + + {tags && tags.length > 0 && } + + +
+ {/* */} + {/* */} +
+
+
+
+ {isCompute ? ( + + ) : ( + + )} + + {/* */} + +
+
+
+ ) +} diff --git a/src/components/templates/AssetDetails/index.tsx b/src/components/templates/AssetDetails/index.tsx index c182a0756..75f0e6b73 100644 --- a/src/components/templates/AssetDetails/index.tsx +++ b/src/components/templates/AssetDetails/index.tsx @@ -1,111 +1,13 @@ import React, { useState, ReactElement, useEffect } from 'react' import { Aquarius, Logger } from '@oceanprotocol/squid' -import { Link, PageProps } from 'gatsby' +import { PageProps } from 'gatsby' import { config } from '../../../config/ocean' import Layout from '../../../components/Layout' import { MetaDataMarket, ServiceMetaDataMarket } from '../../../@types/MetaData' -import Time from '../../atoms/Time' -import Markdown from '../../atoms/Markdown' -import Consume from '../../organisms/Consume' -import Tags from '../../atoms/Tags' import { Alert } from '../../atoms/Alert' -import MetaFull from './MetaFull' -import MetaSecondary from './MetaSecondary' -// import Rating from '../../atoms/Rating' -// import RatingAction from './RatingAction' -import styles from './index.module.css' -import { useMetadata, useOcean } from '@oceanprotocol/react' -import Compute from '../../organisms/Compute' -// import DeleteAction from '../../molecules/DeleteAsset' +import AssetContent from './AssetContent' -const AssetDetailsPageMeta = ({ - metadata, - did -}: { - metadata: MetaDataMarket - did: string -}) => { - const { ocean, balanceInOcean } = useOcean() - const { datePublished } = metadata.main - const { - description, - copyrightHolder, - categories, - tags, - access - } = metadata.additionalInformation - const { curation } = metadata - - const { getCuration } = useMetadata() - const [rating, setRating] = useState(curation ? curation.rating : 0) - const [numVotes, setNumVotes] = useState( - curation ? curation.numVotes : 0 - ) - const isCompute = access && access === 'Compute' - - const onVoteUpdate = async () => { - const { rating, numVotes } = await getCuration(did) - - setRating(rating) - setNumVotes(numVotes) - } - - return ( - <> -
- - -

Summary

- - - {tags && tags.length > 0 && } - - -
- {/* */} - {/* */} -
-
-
-
- {isCompute ? ( - - ) : ( - - )} - - {/* */} - -
-
- - ) -} - -export default function AssetDetailsPage(props: PageProps): ReactElement { +export default function AssetDetailsTemplate(props: PageProps): ReactElement { const [metadata, setMetadata] = useState() const [title, setTitle] = useState() const [error, setError] = useState() @@ -145,9 +47,7 @@ export default function AssetDetailsPage(props: PageProps): ReactElement { ) : did && metadata ? ( -
- -
+
) : null } diff --git a/src/pages/asset/index.tsx b/src/pages/asset/index.tsx new file mode 100644 index 000000000..9ec03ffcc --- /dev/null +++ b/src/pages/asset/index.tsx @@ -0,0 +1,56 @@ +import React, { useState, useEffect, ReactElement } from 'react' +import { Router } from '@reach/router' +import AssetContent from '../../components/templates/AssetDetails/AssetContent' +import Layout from '../../components/Layout' +import { PageProps } from 'gatsby' +import { MetaDataMarket, ServiceMetaDataMarket } from '../../@types/MetaData' +import { Aquarius, Logger } from '@oceanprotocol/squid' +import { config } from '../../config/ocean' +import { Alert } from '../../components/atoms/Alert' + +export default function AssetRoute(props: PageProps): ReactElement { + const [metadata, setMetadata] = useState() + const [title, setTitle] = useState() + const [error, setError] = useState() + + const { did } = props.pageContext as { did: string } + + useEffect(() => { + async function init() { + try { + const aquarius = new Aquarius(config.aquariusUri, Logger) + const ddo = await aquarius.retrieveDDO(did) + + if (!ddo) { + setTitle('Could not retrieve asset') + setError('The DDO was not found in Aquarius.') + return + } + + const { attributes }: ServiceMetaDataMarket = ddo.findServiceByType( + 'metadata' + ) + + setTitle(attributes.main.name) + console.log(attributes) + setMetadata(attributes) + } catch (error) { + setTitle('Error retrieving asset') + setError(error.message) + } + } + init() + }, []) + + return error ? ( + + + + ) : did && metadata ? ( + + + + + + ) : null +}