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
} }
} }
try {
const { ocean } = await provideOcean() const { ocean } = await provideOcean()
this.setState({ this.setState({
isLoading: false, isLoading: false,
ocean 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 />
{!this.state.isLoading ? (
<main className={styles.main}> <main className={styles.main}>
<Routes /> <Routes />
</main> </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,7 +13,9 @@ export default class Web3message extends PureComponent {
return ( return (
<> <>
{/* IF no Web3 */} <User.Consumer>
{states =>
!states.isWeb3 ? (
<div className={styles.message}> <div className={styles.message}>
<span className={styles.indicator} /> No Web3 Browser. For <span className={styles.indicator} /> No Web3 Browser. For
publishing an asset you need to use a Web3-capable plugin or publishing an asset you need to use a Web3-capable plugin or
@ -22,19 +25,16 @@ export default class Web3message extends PureComponent {
</a> </a>
. .
</div> </div>
) : !states.isLogged ? (
{/* IF connected and account locked */}
<div className={styles.message}> <div className={styles.message}>
<span className={styles.indicatorCloseEnough} /> Account <span className={styles.indicatorCloseEnough} /> Account
locked. For publishing an asset you need to unlock your Web3 locked. For publishing an asset you need to unlock your Web3
account. account.
<Button link>Unlock account</Button> <Button link onClick={states.startLogin}>Unlock account</Button>
</div> </div>
) : states.isLogged ? (
{/* IF connected and unlocked */}
<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"
@ -42,6 +42,9 @@ export default class Web3message extends PureComponent {
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,6 +27,7 @@ export default class Details extends Component<DetailsProps, DetailsState> {
} }
private purchaseAsset = async (ddo: any) => { private purchaseAsset = async (ddo: any) => {
try {
const account = await this.context.ocean.getAccounts() const account = await this.context.ocean.getAccounts()
const service = ddo.findServiceByType('Access') const service = ddo.findServiceByType('Access')
const serviceAgreementSignatureResult: any = await this.context.ocean.signServiceAgreement( const serviceAgreementSignatureResult: any = await this.context.ocean.signServiceAgreement(
@ -33,22 +35,25 @@ export default class Details extends Component<DetailsProps, DetailsState> {
service.serviceDefinitionId, service.serviceDefinitionId,
account[0] account[0]
) )
Logger.log(
'serviceAgreementSignatureResult',
serviceAgreementSignatureResult
)
await this.context.ocean.initializeServiceAgreement( await this.context.ocean.initializeServiceAgreement(
ddo.id, ddo.id,
service.serviceDefinitionId, service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementId, serviceAgreementSignatureResult.serviceAgreementId,
serviceAgreementSignatureResult.serviceAgreementSignature, serviceAgreementSignatureResult.serviceAgreementSignature,
(files: any) => (files: any) => {
Logger.log( files.forEach((file: any) => {
`Got files, first files length in bytes: ${files[0].length}` const parsedUrl: any = quertString.parseUrl(file)
), setTimeout(() => {
// eslint-disable-next-line
window.open(parsedUrl.query.url)
}, 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,6 +143,11 @@ class Publish extends Component<{}, PublishState> {
<Route title="Publish"> <Route title="Publish">
<Web3message /> <Web3message />
{this.state.isPublishing ? (
<div>Please sign with ctypto wallet</div>
) : this.state.publishingError ? (
<div>Something went wrong, <a onClick={() => this.tryAgain()}>try again</a></div>
) : (
<Form <Form
title={form.title} title={form.title}
description={form.description} description={form.description}
@ -131,16 +159,18 @@ class Publish extends Component<{}, PublishState> {
{states => {states =>
states.isLogged ? ( states.isLogged ? (
<Button primary> <Button primary>
Register asset (we are logged) Register asset
</Button> </Button>
) : ( ) : (
<Button primary onClick={states.startLogin}> <Button onClick={states.startLogin}>
Register asset (login first) Register asset (login first)
</Button> </Button>
) )
} }
</User.Consumer> </User.Consumer>
</Form> </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