1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

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-ipfs.txt
artifacts
# IDE
.idea

View File

@ -1,32 +1,16 @@
import React, { PureComponent } from 'react'
import { Logger } from '@oceanprotocol/squid'
import React, { useContext, useEffect, useState } from 'react'
import { DDO, Logger } from '@oceanprotocol/squid'
import { User } from '../../context'
import Spinner from '../atoms/Spinner'
import AssetTeaser from '../molecules/AssetTeaser'
import styles from './AssetsLatest.module.scss'
interface AssetsLatestState {
latestAssets?: any[]
isLoadingLatest?: boolean
}
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 AssetsLatest = () => {
const [latestAssets, setLatestAssets] = useState<DDO[]>([])
const [isLoadingLatest, setIsLoadingLatest] = useState(false)
const { ocean } = useContext(User)
const getLatestAssets = async () => {
const searchQuery = {
offset: 15,
page: 1,
@ -38,18 +22,17 @@ export default class AssetsLatest extends PureComponent<{}, AssetsLatestState> {
try {
const search = await ocean.assets.query(searchQuery)
this.setState({
latestAssets: search.results,
isLoadingLatest: false
})
setLatestAssets(search.results)
setIsLoadingLatest(false)
} catch (error) {
Logger.error(error.message)
this.setState({ isLoadingLatest: false })
setIsLoadingLatest(false)
}
}
public render() {
const { latestAssets, isLoadingLatest } = this.state
useEffect(() => {
getLatestAssets()
}, [])
return (
<>
@ -59,12 +42,8 @@ export default class AssetsLatest extends PureComponent<{}, AssetsLatestState> {
<Spinner message="Loading..." />
) : latestAssets && latestAssets.length ? (
<div className={styles.latestAssets}>
{latestAssets.map((asset: any) => (
<AssetTeaser
key={asset.id}
asset={asset}
minimal
/>
{latestAssets.map(asset => (
<AssetTeaser key={asset.id} asset={asset} minimal />
))}
</div>
) : (
@ -73,7 +52,6 @@ export default class AssetsLatest extends PureComponent<{}, AssetsLatestState> {
</div>
</>
)
}
}
AssetsLatest.contextType = User
export default AssetsLatest

View File

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