Moved AssetsLatest to useContext hook

Moving all components to functional components style and using hooks for
state, context and component lifecycles

Signed-off-by: Pablo F. Mescher <pfmescher@gmail.com>
This commit is contained in:
Pablo F. Mescher 2020-01-24 11:54:48 +01:00
parent b9f83021c7
commit fd2cfb07e4
3 changed files with 38 additions and 55 deletions

3
.gitignore vendored
View File

@ -28,3 +28,6 @@ cypress/videos
cypress/fixtures/did.txt cypress/fixtures/did.txt
cypress/fixtures/did-ipfs.txt cypress/fixtures/did-ipfs.txt
artifacts artifacts
# IDE
.idea

View File

@ -1,32 +1,16 @@
import React, { PureComponent } from 'react' import React, { useContext, useEffect, useState } from 'react'
import { Logger } from '@oceanprotocol/squid' import { DDO, Logger } from '@oceanprotocol/squid'
import { User } from '../../context' import { User } from '../../context'
import Spinner from '../atoms/Spinner' import Spinner from '../atoms/Spinner'
import AssetTeaser from '../molecules/AssetTeaser' import AssetTeaser from '../molecules/AssetTeaser'
import styles from './AssetsLatest.module.scss' import styles from './AssetsLatest.module.scss'
interface AssetsLatestState { const AssetsLatest = () => {
latestAssets?: any[] const [latestAssets, setLatestAssets] = useState<DDO[]>([])
isLoadingLatest?: boolean const [isLoadingLatest, setIsLoadingLatest] = useState(false)
} const { ocean } = useContext(User)
export default class AssetsLatest extends PureComponent<{}, AssetsLatestState> {
public state = { latestAssets: [], isLoadingLatest: true }
public _isMounted = false
public componentDidMount() {
this._isMounted = true
this._isMounted && this.getLatestAssets()
}
public componentWillUnmount() {
this._isMounted = false
}
private getLatestAssets = async () => {
const { ocean } = this.context
const getLatestAssets = async () => {
const searchQuery = { const searchQuery = {
offset: 15, offset: 15,
page: 1, page: 1,
@ -38,42 +22,36 @@ export default class AssetsLatest extends PureComponent<{}, AssetsLatestState> {
try { try {
const search = await ocean.assets.query(searchQuery) const search = await ocean.assets.query(searchQuery)
this.setState({ setLatestAssets(search.results)
latestAssets: search.results, setIsLoadingLatest(false)
isLoadingLatest: false
})
} catch (error) { } catch (error) {
Logger.error(error.message) Logger.error(error.message)
this.setState({ isLoadingLatest: false }) setIsLoadingLatest(false)
} }
} }
public render() { useEffect(() => {
const { latestAssets, isLoadingLatest } = this.state getLatestAssets()
}, [])
return ( return (
<> <>
<h2 className={styles.title}>Latest published assets</h2> <h2 className={styles.title}>Latest published assets</h2>
<div className={styles.latestAssetsWrap}> <div className={styles.latestAssetsWrap}>
{isLoadingLatest ? ( {isLoadingLatest ? (
<Spinner message="Loading..." /> <Spinner message="Loading..." />
) : latestAssets && latestAssets.length ? ( ) : latestAssets && latestAssets.length ? (
<div className={styles.latestAssets}> <div className={styles.latestAssets}>
{latestAssets.map((asset: any) => ( {latestAssets.map(asset => (
<AssetTeaser <AssetTeaser key={asset.id} asset={asset} minimal />
key={asset.id} ))}
asset={asset} </div>
minimal ) : (
/> <div>No data sets found.</div>
))} )}
</div> </div>
) : ( </>
<div>No data sets found.</div> )
)}
</div>
</>
)
}
} }
AssetsLatest.contextType = User export default AssetsLatest

View File

@ -1,4 +1,5 @@
import React from 'react' import React from 'react'
import { Ocean } from '@oceanprotocol/squid'
export const User = React.createContext({ export const User = React.createContext({
isLogged: false, isLogged: false,
@ -7,7 +8,8 @@ export const User = React.createContext({
isLoading: false, isLoading: false,
account: '', account: '',
web3: {}, web3: {},
ocean: {}, // eslint-disable-next-line
ocean: {} as Ocean,
balance: { balance: {
eth: 0, eth: 0,
ocn: 0 ocn: 0