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

isWeb3 state, App loading, publish states, consume flow

This commit is contained in:
Jernej Pregelj 2019-02-11 16:53:55 +01:00
parent 80d2a07f44
commit 767c4afcc5
6 changed files with 143 additions and 92 deletions

View File

@ -21,6 +21,7 @@ declare global {
interface AppState { interface AppState {
isLogged: boolean isLogged: boolean
isLoading: boolean isLoading: boolean
isWeb3: boolean
web3: Web3 web3: Web3
ocean: {} ocean: {}
startLogin: () => void startLogin: () => void
@ -37,6 +38,7 @@ class App extends Component<{}, AppState> {
public state = { public state = {
isLogged: false, isLogged: false,
isLoading: true, isLoading: true,
isWeb3: false,
web3: new Web3( web3: new Web3(
new Web3.providers.HttpProvider( new Web3.providers.HttpProvider(
`${nodeScheme}://${nodeHost}:${nodePort}` `${nodeScheme}://${nodeHost}:${nodePort}`
@ -51,7 +53,6 @@ class App extends Component<{}, AppState> {
} }
private startLoginProcess = async () => { private startLoginProcess = async () => {
this.setState({ isLoading: true })
if (window.web3) { if (window.web3) {
const web3 = new Web3(window.web3.currentProvider) const web3 = new Web3(window.web3.currentProvider)
try { try {
@ -59,6 +60,7 @@ class App extends Component<{}, AppState> {
if (accounts.length > 0) { if (accounts.length > 0) {
this.setState({ this.setState({
isLogged: true, isLogged: true,
isWeb3: true,
web3 web3
}) })
} else { } else {
@ -66,6 +68,7 @@ class App extends Component<{}, AppState> {
await window.ethereum.enable() await window.ethereum.enable()
this.setState({ this.setState({
isLogged: true, isLogged: true,
isWeb3: true,
web3 web3
}) })
} }
@ -76,11 +79,11 @@ class App extends Component<{}, AppState> {
} else { } else {
// no metamask/mist, show installation guide! // no metamask/mist, show installation guide!
} }
this.setState({ isLoading: false })
} }
private bootstrap = async () => { private bootstrap = async () => {
if (window.web3) { if (window.web3) {
this.setState({ isWeb3: true })
const web3 = new Web3(window.web3.currentProvider) const web3 = new Web3(window.web3.currentProvider)
try { try {
const accounts = await web3.eth.getAccounts() const accounts = await web3.eth.getAccounts()
@ -94,11 +97,18 @@ class App extends Component<{}, AppState> {
// continue with default // continue with default
} }
} }
const { ocean } = await provideOcean() try {
this.setState({ const { ocean } = await provideOcean()
isLoading: false, this.setState({
ocean isLoading: false,
}) ocean
})
} catch (e) {
// show loading error / unable to initialize ocean
this.setState({
isLoading: false
})
}
} }
public render() { public render() {
@ -109,9 +119,11 @@ class App extends Component<{}, AppState> {
<> <>
<Header /> <Header />
<main className={styles.main}> {!this.state.isLoading ? (
<Routes /> <main className={styles.main}>
</main> <Routes />
</main>
) : null}
<Footer /> <Footer />
</> </>

View File

@ -1,6 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import Button from '../components/atoms/Button' import Button from '../components/atoms/Button'
import styles from './Web3message.module.scss' import styles from './Web3message.module.scss'
import { User } from '../context/User'
export default class Web3message extends PureComponent { export default class Web3message extends PureComponent {
public render() { public render() {
@ -12,36 +13,38 @@ export default class Web3message extends PureComponent {
return ( return (
<> <>
{/* IF no Web3 */} <User.Consumer>
<div className={styles.message}> {states =>
<span className={styles.indicator} /> No Web3 Browser. For !states.isWeb3 ? (
publishing an asset you need to use a Web3-capable plugin or <div className={styles.message}>
browser, like{' '} <span className={styles.indicator} /> No Web3 Browser. For
<a href="https://docs.oceanprotocol.com/tutorials/wallets/#how-to-setup-metamask"> publishing an asset you need to use a Web3-capable plugin or
MetaMask browser, like{' '}
</a> <a href="https://docs.oceanprotocol.com/tutorials/wallets/#how-to-setup-metamask">
. MetaMask
</div> </a>
.
{/* IF connected and account locked */} </div>
<div className={styles.message}> ) : !states.isLogged ? (
<span className={styles.indicatorCloseEnough} /> Account <div className={styles.message}>
locked. For publishing an asset you need to unlock your Web3 <span className={styles.indicatorCloseEnough} /> Account
account. locked. For publishing an asset you need to unlock your Web3
<Button link>Unlock account</Button> account.
</div> <Button link onClick={states.startLogin}>Unlock account</Button>
</div>
{/* IF connected and unlocked */} ) : states.isLogged ? (
<div className={styles.message}> <div className={styles.message}>
<span className={styles.indicatorActive} /> Connected with <span className={styles.indicatorActive} /> Connected with account
account <span
<span className={styles.account}
className={styles.account} title="0xfehz2u89nfewhji432ntio43huof42huifewhnuefwo"
title="0xfehz2u89nfewhji432ntio43huof42huifewhnuefwo" >
> 0xfehz2u89n...
0xfehz2u89n... </span>
</span> </div>
</div> ) : null
}
</User.Consumer>
</> </>
) )
} }

View File

@ -3,6 +3,7 @@ import React from 'react'
export const User = React.createContext({ export const User = React.createContext({
isLogged: false, isLogged: false,
isLoading: false, isLoading: false,
isWeb3: false,
web3: {}, web3: {},
ocean: {}, ocean: {},
startLogin: () => { startLogin: () => {

View File

@ -3,6 +3,7 @@ import React, { Component } from 'react'
import Route from '../components/templates/Route' import Route from '../components/templates/Route'
import Button from '../components/atoms/Button' import Button from '../components/atoms/Button'
import { User } from '../context/User' import { User } from '../context/User'
import quertString from 'query-string'
interface DetailsState { interface DetailsState {
ddo: any ddo: any
@ -26,29 +27,33 @@ export default class Details extends Component<DetailsProps, DetailsState> {
} }
private purchaseAsset = async (ddo: any) => { private purchaseAsset = async (ddo: any) => {
const account = await this.context.ocean.getAccounts() try {
const service = ddo.findServiceByType('Access') const account = await this.context.ocean.getAccounts()
const serviceAgreementSignatureResult: any = await this.context.ocean.signServiceAgreement( const service = ddo.findServiceByType('Access')
ddo.id, const serviceAgreementSignatureResult: any = await this.context.ocean.signServiceAgreement(
service.serviceDefinitionId, ddo.id,
account[0] service.serviceDefinitionId,
) account[0]
Logger.log( )
'serviceAgreementSignatureResult', await this.context.ocean.initializeServiceAgreement(
serviceAgreementSignatureResult ddo.id,
) service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementId,
await this.context.ocean.initializeServiceAgreement( serviceAgreementSignatureResult.serviceAgreementSignature,
ddo.id, (files: any) => {
service.serviceDefinitionId, files.forEach((file: any) => {
serviceAgreementSignatureResult.serviceAgreementId, const parsedUrl: any = quertString.parseUrl(file)
serviceAgreementSignatureResult.serviceAgreementSignature, setTimeout(() => {
(files: any) => // eslint-disable-next-line
Logger.log( window.open(parsedUrl.query.url)
`Got files, first files length in bytes: ${files[0].length}` }, 100)
), })
account[0] },
) account[0]
)
} catch (e) {
Logger.log('error', e)
}
} }
private showDetails = (ddo: any) => { private showDetails = (ddo: any) => {
@ -56,7 +61,7 @@ export default class Details extends Component<DetailsProps, DetailsState> {
<> <>
<div>{JSON.stringify(this.state.metadata)}</div> <div>{JSON.stringify(this.state.metadata)}</div>
<Button onClick={this.purchaseAsset(ddo)}> <Button onClick={() => this.purchaseAsset(ddo)}>
Purchase asset Purchase asset
</Button> </Button>
</> </>

View File

@ -1,4 +1,5 @@
import React, { ChangeEvent, Component, FormEvent } from 'react' import React, { ChangeEvent, Component, FormEvent } from 'react'
import { Logger } from '@oceanprotocol/squid'
import Route from '../components/templates/Route' import Route from '../components/templates/Route'
import Button from '../components/atoms/Button' import Button from '../components/atoms/Button'
import Form from '../components/atoms/Form/Form' import Form from '../components/atoms/Form/Form'
@ -22,7 +23,9 @@ interface PublishState {
license?: string license?: string
copyrightHolder?: string copyrightHolder?: string
categories?: string[] categories?: string[]
tags?: string[] tags?: string[],
isPublishing?: boolean,
publishingError?: string
} }
class Publish extends Component<{}, PublishState> { class Publish extends Component<{}, PublishState> {
@ -36,7 +39,9 @@ class Publish extends Component<{}, PublishState> {
type: 'dataset' as AssetType, type: 'dataset' as AssetType,
license: '', license: '',
copyrightHolder: '', copyrightHolder: '',
categories: [''] categories: [''],
isPublishing: false,
publishingError: ''
} }
public formFields = (entries: any[]) => public formFields = (entries: any[]) =>
@ -80,8 +85,16 @@ class Publish extends Component<{}, PublishState> {
}) })
} }
private tryAgain = () => {
this.setState({ publishingError: '' })
}
private registerAsset = async (event: FormEvent<HTMLFormElement>) => { private registerAsset = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault() event.preventDefault()
this.setState({
publishingError: '',
isPublishing: true
})
const account = await this.context.ocean.getAccounts() const account = await this.context.ocean.getAccounts()
const newAsset = { const newAsset = {
// OEP-08 Attributes // OEP-08 Attributes
@ -109,8 +122,18 @@ class Publish extends Component<{}, PublishState> {
AssetModel.additionalInformation AssetModel.additionalInformation
) )
} }
try {
await this.context.ocean.registerAsset(newAsset, account[0]) await this.context.ocean.registerAsset(newAsset, account[0])
} catch (e) {
// make readable errors
Logger.log('error:', e)
this.setState({
publishingError: e
})
}
this.setState({
isPublishing: false
})
} }
public render() { public render() {
@ -120,27 +143,34 @@ class Publish extends Component<{}, PublishState> {
<Route title="Publish"> <Route title="Publish">
<Web3message /> <Web3message />
<Form {this.state.isPublishing ? (
title={form.title} <div>Please sign with ctypto wallet</div>
description={form.description} ) : this.state.publishingError ? (
onSubmit={this.registerAsset} <div>Something went wrong, <a onClick={() => this.tryAgain()}>try again</a></div>
> ) : (
{this.formFields(entries)} <Form
title={form.title}
description={form.description}
onSubmit={this.registerAsset}
>
{this.formFields(entries)}
<User.Consumer>
{states =>
states.isLogged ? (
<Button primary>
Register asset
</Button>
) : (
<Button onClick={states.startLogin}>
Register asset (login first)
</Button>
)
}
</User.Consumer>
</Form>
)}
<User.Consumer>
{states =>
states.isLogged ? (
<Button primary>
Register asset (we are logged)
</Button>
) : (
<Button primary onClick={states.startLogin}>
Register asset (login first)
</Button>
)
}
</User.Consumer>
</Form>
</Route> </Route>
) )
} }

View File

@ -2,7 +2,7 @@ import queryString from 'query-string'
import React, { Component } from 'react' import React, { Component } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import Route from '../components/templates/Route' import Route from '../components/templates/Route'
import { provideOcean } from '../ocean' import { User } from '../context/User'
interface SearchState { interface SearchState {
results: any[] results: any[]
@ -17,8 +17,6 @@ export default class Search extends Component<SearchProps, SearchState> {
public state = { results: [] } public state = { results: [] }
public async componentDidMount() { public async componentDidMount() {
// temporary ocean init and asset retrieval
const { ocean } = await provideOcean()
const searchParams = queryString.parse(this.props.location.search) const searchParams = queryString.parse(this.props.location.search)
const queryRequest: any = { const queryRequest: any = {
offset: 100, offset: 100,
@ -29,7 +27,7 @@ export default class Search extends Component<SearchProps, SearchState> {
} }
} }
} }
const assets = await ocean.searchAssets(queryRequest) const assets = await this.context.ocean.searchAssets(queryRequest)
this.setState({ results: assets }) this.setState({ results: assets })
} }
@ -56,3 +54,5 @@ export default class Search extends Component<SearchProps, SearchState> {
) )
} }
} }
Search.contextType = User