1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00
commons/client/src/components/organisms/AssetsUser.tsx

110 lines
3.7 KiB
TypeScript
Raw Normal View History

import React, { PureComponent } from 'react'
import { Link } from 'react-router-dom'
2019-03-25 17:31:32 +01:00
import { Logger } from '@oceanprotocol/squid'
2019-03-20 15:27:00 +01:00
import { User } from '../../context/User'
2019-02-15 17:26:28 +01:00
import Spinner from '../atoms/Spinner'
2019-03-25 18:03:49 +01:00
import Asset from '../molecules/Asset'
2019-04-06 17:55:24 +02:00
import Web3message from './Web3message'
import styles from './AssetsUser.module.scss'
2019-03-25 17:31:32 +01:00
export default class AssetsUser extends PureComponent<
{ list?: boolean; recent?: number },
2019-03-25 17:31:32 +01:00
{ results: any[]; isLoading: boolean }
> {
2019-02-15 17:26:28 +01:00
public state = { results: [], isLoading: true }
public _isMounted: boolean = false
public componentDidMount() {
this._isMounted = true
this._isMounted && this.searchOcean()
}
public componentWillUnmount() {
this._isMounted = false
}
private async searchOcean() {
const { account, ocean } = this.context
if (account) {
ocean.keeper.didRegistry.contract.getPastEvents(
2019-03-28 13:15:26 +01:00
'DIDAttributeRegistered',
{
filter: { _owner: account },
2019-03-28 13:15:26 +01:00
fromBlock: 0,
toBlock: 'latest'
},
async (error: any, events: any) => {
if (error) {
Logger.log('error retrieving', error)
this._isMounted && this.setState({ isLoading: false })
2019-03-28 13:15:26 +01:00
} else {
const results = []
for (const event of events) {
const ddo = await ocean.assets.resolve(
2019-03-28 13:15:26 +01:00
`did:op:${event.returnValues._did.substring(2)}`
)
results.push(ddo)
2019-03-26 11:46:57 +01:00
}
this._isMounted &&
this.setState({ results, isLoading: false })
2019-03-20 15:27:00 +01:00
}
}
2019-03-26 11:46:57 +01:00
)
2019-04-02 14:55:15 +02:00
} else {
this.setState({ isLoading: false })
2019-03-26 11:46:57 +01:00
}
}
public render() {
const { account, isNile } = this.context
return isNile && account ? (
2019-04-08 10:44:03 +02:00
<div className={styles.assetsUser}>
{this.props.recent && (
<h2 className={styles.subTitle}>
Your Latest Published Data Sets
</h2>
)}
2019-04-08 10:44:03 +02:00
{this.state.isLoading ? (
<Spinner />
) : this.state.results.length ? (
<>
{this.state.results
.slice(
0,
this.props.recent
? this.props.recent
: undefined
)
.filter(asset => !!asset)
.map((asset: any) => (
<Asset
list={this.props.list}
key={asset.id}
asset={asset}
/>
))}
{this.props.recent && (
<Link className={styles.link} to={'/history'}>
All Data Sets
</Link>
)}
</>
) : (
<div className={styles.empty}>
<p>No Data Sets Yet.</p>
<Link to="/publish">+ Publish A Data Set</Link>
</div>
)}
</div>
) : (
<Web3message />
)
}
}
2019-03-20 15:27:00 +01:00
AssetsUser.contextType = User