From ed57702ff3f0ed9687169c3421d40509c2ce0c2c Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 18 Nov 2020 20:01:33 +0100 Subject: [PATCH] refactor root components (#263) * refactor layout * refactor * root components reordering * type-check fix --- .../{Layout.module.css => App.module.css} | 12 ++- src/components/App.tsx | 46 ++++++++++ src/components/Layout.tsx | 84 ------------------- .../organisms/AssetContent/index.tsx | 1 - src/components/templates/Page.tsx | 39 +++++++++ ...{AssetDetails.tsx => PageAssetDetails.tsx} | 45 ++++++---- src/components/templates/PageMarkdown.tsx | 11 +-- src/helpers/NetworkMonitor.tsx | 1 - src/helpers/wrapPageElement.tsx | 9 +- src/helpers/wrapRootElement.tsx | 1 - src/pages/asset/index.tsx | 2 +- src/pages/history.tsx | 6 +- src/pages/index.tsx | 6 +- src/pages/publish.tsx | 6 +- src/pages/search.tsx | 6 +- .../{Layout.test.tsx => Page.test.tsx} | 8 +- 16 files changed, 149 insertions(+), 134 deletions(-) rename src/components/{Layout.module.css => App.module.css} (59%) create mode 100644 src/components/App.tsx delete mode 100644 src/components/Layout.tsx create mode 100644 src/components/templates/Page.tsx rename src/components/templates/{AssetDetails.tsx => PageAssetDetails.tsx} (62%) rename tests/unit/components/{Layout.test.tsx => Page.test.tsx} (67%) diff --git a/src/components/Layout.module.css b/src/components/App.module.css similarity index 59% rename from src/components/Layout.module.css rename to src/components/App.module.css index 1e640ee14..3cc090b9d 100644 --- a/src/components/Layout.module.css +++ b/src/components/App.module.css @@ -1,7 +1,7 @@ .app { height: 100%; background: url('../../node_modules/@oceanprotocol/art/waves/waves.svg') - no-repeat center 11rem; + no-repeat center 13.5rem; /* sticky footer technique */ display: flex; @@ -20,8 +20,16 @@ } .main { - padding: calc(var(--spacer) * 2) 0; + padding: calc(var(--spacer) * 1.5) 0 calc(var(--spacer) * 2) 0; /* sticky footer technique */ flex: 1; } + +.main > div[class*='Alert']:first-child { + margin-top: -2rem; +} + +.main > div[class*='Alert'] { + margin-bottom: calc(var(--spacer) / 1.5); +} diff --git a/src/components/App.tsx b/src/components/App.tsx new file mode 100644 index 000000000..538dbd605 --- /dev/null +++ b/src/components/App.tsx @@ -0,0 +1,46 @@ +import React, { ReactElement } from 'react' +import Footer from './organisms/Footer' +import Header from './organisms/Header' +import Styles from '../global/Styles' +import styles from './App.module.css' +import { useSiteMetadata } from '../hooks/useSiteMetadata' +import { useOcean } from '@oceanprotocol/react' +import Alert from './atoms/Alert' +import { PageProps } from 'gatsby' + +export default function App({ + children, + ...props +}: { + children: ReactElement +}): ReactElement { + const { warning } = useSiteMetadata() + const { + isInPurgatory: isAccountInPurgatory, + purgatoryData: accountPurgatory + } = useOcean() + + return ( + +
+
+ + {(props as PageProps).uri === '/' && ( + + )} + + {isAccountInPurgatory && accountPurgatory && ( + + )} + +
{children}
+
+
+
+ ) +} diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx deleted file mode 100644 index 7f2b4dbd8..000000000 --- a/src/components/Layout.tsx +++ /dev/null @@ -1,84 +0,0 @@ -import React, { ReactNode, ReactElement, useEffect } from 'react' -import Header from './organisms/Header' -import Footer from './organisms/Footer' -import PageHeader from './molecules/PageHeader' -import styles from './Layout.module.css' -import Seo from './atoms/Seo' -import Container from './atoms/Container' -import Alert from './atoms/Alert' -import { useSiteMetadata } from '../hooks/useSiteMetadata' -import { useAsset, useOcean } from '@oceanprotocol/react' -import { Logger } from '@oceanprotocol/lib' - -export interface LayoutProps { - children: ReactNode - title: string - uri: string - description?: string - noPageHeader?: boolean - headerCenter?: boolean -} - -export default function Layout({ - children, - title, - uri, - description, - noPageHeader, - headerCenter -}: LayoutProps): ReactElement { - const { warning } = useSiteMetadata() - const { isInPurgatory, purgatoryData } = useAsset() - const { - isInPurgatory: isAccountInPurgatory, - purgatoryData: accountPurgatory - } = useOcean() - - useEffect(() => { - Logger.log('isInPurgatory', isInPurgatory, purgatoryData) - }, [isInPurgatory, purgatoryData]) - - return ( -
- - -
- - {uri === '/' && ( - - )} - - {isAccountInPurgatory && accountPurgatory && ( - - )} - - {isInPurgatory && purgatoryData && ( - - )} - -
- - {title && !noPageHeader && ( - - )} - {children} - -
-
-
- ) -} diff --git a/src/components/organisms/AssetContent/index.tsx b/src/components/organisms/AssetContent/index.tsx index cd5ca4f8c..96ae071c8 100644 --- a/src/components/organisms/AssetContent/index.tsx +++ b/src/components/organisms/AssetContent/index.tsx @@ -13,7 +13,6 @@ import { useMetadata, useOcean, usePricing } from '@oceanprotocol/react' import EtherscanLink from '../../atoms/EtherscanLink' import Bookmark from './Bookmark' import Byline from './Byline' -import Alert from '../../atoms/Alert' export interface AssetContentProps { metadata: MetadataMarket diff --git a/src/components/templates/Page.tsx b/src/components/templates/Page.tsx new file mode 100644 index 000000000..12a5a3c00 --- /dev/null +++ b/src/components/templates/Page.tsx @@ -0,0 +1,39 @@ +import React, { ReactNode, ReactElement } from 'react' +import PageHeader from '../molecules/PageHeader' +import Seo from '../atoms/Seo' +import Container from '../atoms/Container' + +export interface PageProps { + children: ReactNode + title: string + uri: string + description?: string + noPageHeader?: boolean + headerCenter?: boolean +} + +export default function Page({ + children, + title, + uri, + description, + noPageHeader, + headerCenter +}: PageProps): ReactElement { + return ( + <> + + + + {title && !noPageHeader && ( + + )} + {children} + + + ) +} diff --git a/src/components/templates/AssetDetails.tsx b/src/components/templates/PageAssetDetails.tsx similarity index 62% rename from src/components/templates/AssetDetails.tsx rename to src/components/templates/PageAssetDetails.tsx index 9444bacd4..777588443 100644 --- a/src/components/templates/AssetDetails.tsx +++ b/src/components/templates/PageAssetDetails.tsx @@ -1,12 +1,12 @@ import React, { useState, useEffect, ReactElement } from 'react' import { Router } from '@reach/router' -import AssetContent from '../../components/organisms/AssetContent' -import Layout from '../../components/Layout' +import AssetContent from '../organisms/AssetContent' +import Page from './Page' import { MetadataMarket } from '../../@types/MetaData' import { MetadataCache, Logger, DDO } from '@oceanprotocol/lib' -import Alert from '../../components/atoms/Alert' -import Loader from '../../components/atoms/Loader' -import { useOcean } from '@oceanprotocol/react' +import Alert from '../atoms/Alert' +import Loader from '../atoms/Loader' +import { useAsset, useOcean } from '@oceanprotocol/react' export default function PageTemplateAssetDetails({ did, @@ -16,6 +16,7 @@ export default function PageTemplateAssetDetails({ uri: string }): ReactElement { const { config } = useOcean() + const { isInPurgatory, purgatoryData } = useAsset() const [metadata, setMetadata] = useState() const [title, setTitle] = useState() const [error, setError] = useState() @@ -57,22 +58,32 @@ export default function PageTemplateAssetDetails({ }, [ddo, did, config.metadataCacheUri]) return did && metadata ? ( - - - + {isInPurgatory && purgatoryData && ( + - - + )} + + + + + + ) : error ? ( - + - + ) : ( - + - + ) } diff --git a/src/components/templates/PageMarkdown.tsx b/src/components/templates/PageMarkdown.tsx index c551d0810..489ea5789 100644 --- a/src/components/templates/PageMarkdown.tsx +++ b/src/components/templates/PageMarkdown.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react' import { graphql, PageProps } from 'gatsby' -import Layout from '../Layout' +import Page from './Page' import styles from './PageMarkdown.module.css' import Container from '../atoms/Container' @@ -9,19 +9,14 @@ export default function PageTemplateMarkdown(props: PageProps): ReactElement { const { title, description } = frontmatter return ( - +
- + ) } diff --git a/src/helpers/NetworkMonitor.tsx b/src/helpers/NetworkMonitor.tsx index 6675b003a..c9fb75bd1 100644 --- a/src/helpers/NetworkMonitor.tsx +++ b/src/helpers/NetworkMonitor.tsx @@ -3,7 +3,6 @@ import { useOcean } from '@oceanprotocol/react' import { getOceanConfig } from './wrapRootElement' import { Logger } from '@oceanprotocol/lib' import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper' -import { LogLevel } from '@oceanprotocol/lib/dist/node/utils' export function NetworkMonitor(): ReactElement { const { connect, web3Provider, web3, networkId, config } = useOcean() diff --git a/src/helpers/wrapPageElement.tsx b/src/helpers/wrapPageElement.tsx index c705ce85f..b2911c248 100644 --- a/src/helpers/wrapPageElement.tsx +++ b/src/helpers/wrapPageElement.tsx @@ -1,10 +1,13 @@ +import { PageProps } from 'gatsby' import React, { ReactElement } from 'react' -import Styles from '../global/Styles' +import App from '../components/App' const wrapPageElement = ({ - element + element, + props }: { element: ReactElement -}): ReactElement => {element} + props: PageProps +}): ReactElement => {element} export default wrapPageElement diff --git a/src/helpers/wrapRootElement.tsx b/src/helpers/wrapRootElement.tsx index 0dd4c4c72..b99bbcc49 100644 --- a/src/helpers/wrapRootElement.tsx +++ b/src/helpers/wrapRootElement.tsx @@ -10,7 +10,6 @@ import { } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper' import { UserPreferencesProvider } from '../providers/UserPreferences' import PricesProvider from '../providers/Prices' -import { LogLevel } from '@oceanprotocol/lib/dist/node/utils' export function getOceanConfig( network: ConfigHelperNetworkName | ConfigHelperNetworkId diff --git a/src/pages/asset/index.tsx b/src/pages/asset/index.tsx index c1d0ce3fe..77f3da519 100644 --- a/src/pages/asset/index.tsx +++ b/src/pages/asset/index.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react' import { PageProps } from 'gatsby' -import PageTemplateAssetDetails from '../../components/templates/AssetDetails' +import PageTemplateAssetDetails from '../../components/templates/PageAssetDetails' import { AssetProvider } from '@oceanprotocol/react' export default function PageGatsbyAssetDetails(props: PageProps): ReactElement { diff --git a/src/pages/history.tsx b/src/pages/history.tsx index b22607685..bd9896014 100644 --- a/src/pages/history.tsx +++ b/src/pages/history.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react' import PageHistory from '../components/pages/History' -import Layout from '../components/Layout' +import Page from '../components/templates/Page' import { graphql, PageProps } from 'gatsby' export default function PageGatsbyHistory(props: PageProps): ReactElement { @@ -8,9 +8,9 @@ export default function PageGatsbyHistory(props: PageProps): ReactElement { const { title, description } = content return ( - + - + ) } diff --git a/src/pages/index.tsx b/src/pages/index.tsx index ac4490d5c..192e8b2b7 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -2,19 +2,19 @@ import React, { ReactElement } from 'react' import { PageProps } from 'gatsby' import PageHome from '../components/pages/Home' import { useSiteMetadata } from '../hooks/useSiteMetadata' -import Layout from '../components/Layout' +import Page from '../components/templates/Page' export default function PageGatsbyHome(props: PageProps): ReactElement { const { siteTitle, siteTagline } = useSiteMetadata() return ( - - + ) } diff --git a/src/pages/publish.tsx b/src/pages/publish.tsx index 94c387af3..a372c256b 100644 --- a/src/pages/publish.tsx +++ b/src/pages/publish.tsx @@ -1,6 +1,6 @@ import React, { ReactElement } from 'react' import PagePublish from '../components/pages/Publish' -import Layout from '../components/Layout' +import Page from '../components/templates/Page' import { graphql, PageProps } from 'gatsby' export default function PageGatsbyPublish(props: PageProps): ReactElement { @@ -8,9 +8,9 @@ export default function PageGatsbyPublish(props: PageProps): ReactElement { const { title, description } = content return ( - + - + ) } diff --git a/src/pages/search.tsx b/src/pages/search.tsx index 1ae37eb13..817d93d81 100644 --- a/src/pages/search.tsx +++ b/src/pages/search.tsx @@ -1,7 +1,7 @@ import React, { ReactElement } from 'react' import PageSearch from '../components/templates/Search' import { PageProps } from 'gatsby' -import Layout from '../components/Layout' +import Page from '../components/templates/Page' import queryString from 'query-string' import { accountTruncate } from '../utils/wallet' import ethereumAddress from 'ethereum-address' @@ -20,8 +20,8 @@ export default function PageGatsbySearch(props: PageProps): ReactElement { : `Search for ${searchValue || 'all data sets'}` return ( - + - + ) } diff --git a/tests/unit/components/Layout.test.tsx b/tests/unit/components/Page.test.tsx similarity index 67% rename from tests/unit/components/Layout.test.tsx rename to tests/unit/components/Page.test.tsx index 1f59d3df0..67d2eadc8 100644 --- a/tests/unit/components/Layout.test.tsx +++ b/tests/unit/components/Page.test.tsx @@ -1,20 +1,20 @@ import React from 'react' import testRender from '../testRender' -import Layout from '../../../src/components/Layout' +import Page from '../../../src/components/templates/Page' import { createHistory, createMemorySource, LocationProvider } from '@reach/router' -describe('Layout', () => { +describe('Page', () => { const history = createHistory(createMemorySource('/')) testRender( - + Hello - + ) })