mirror of
https://github.com/oceanprotocol/docs.git
synced 2024-11-26 19:49:26 +01:00
restructure React tutorials, add screenshots, code examples consistency
This commit is contained in:
parent
b4e6d395c7
commit
32cb354ccb
BIN
content/tutorials/images/react-app-01.png
Normal file
BIN
content/tutorials/images/react-app-01.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 456 KiB |
BIN
content/tutorials/images/react-app-02.png
Normal file
BIN
content/tutorials/images/react-app-02.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 399 KiB |
BIN
content/tutorials/images/react-app-03.png
Normal file
BIN
content/tutorials/images/react-app-03.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 420 KiB |
BIN
content/tutorials/images/react-app-04.png
Normal file
BIN
content/tutorials/images/react-app-04.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 553 KiB |
@ -7,13 +7,16 @@ description: Tutorial to add dataset publishing capabilities to a basic React ap
|
||||
|
||||
This is a continuation of the [React App Setup](/tutorials/react-setup) tutorial, so make sure you have done all the steps described in there.
|
||||
|
||||
## Adding Publishing
|
||||
Open `src/App.js` in your marketplace app from the [React App Setup](/tutorials/react-setup) tutorial.
|
||||
|
||||
1. Open `src/App.js` in your marketplace app from the [React App Setup](/tutorials/react-setup) tutorial.
|
||||
2. First, let's add the asset that we want to publish. To do that, we need to add the following code after `window.ethereum.enable()` line.
|
||||
## Define Asset
|
||||
|
||||
```javascript
|
||||
const asset = {
|
||||
First, let's add the asset that we want to publish.
|
||||
|
||||
To do that, we need to add the following code after `window.ethereum.enable()` line, defining our asset based on the [OEP-08](https://github.com/oceanprotocol/OEPs/tree/master/8) metadata structure:
|
||||
|
||||
```js
|
||||
const asset = {
|
||||
base: {
|
||||
name: 'Office Humidity',
|
||||
description:
|
||||
@ -47,24 +50,26 @@ This is a continuation of the [React App Setup](/tutorials/react-setup) tutorial
|
||||
additionalInformation: {
|
||||
updateFrequency: 'yearly'
|
||||
}
|
||||
}
|
||||
```
|
||||
}
|
||||
```
|
||||
|
||||
3. Now that we have an asset to submit, we need function to handle it. Just before `render() {` let's add:
|
||||
## Handle Asset Publishing
|
||||
|
||||
```javascript
|
||||
async submitAsset(){
|
||||
Now that we have an asset to submit, we need a function to handle it. Just before `render() {` let's add this function:
|
||||
|
||||
```js
|
||||
async submitAsset() {
|
||||
const accounts = await this.ocean.getAccounts()
|
||||
const ddo = await this.ocean.registerAsset(asset, accounts[0])
|
||||
alert("Asset successfully submited:", JSON.stringify(ddo))
|
||||
}
|
||||
```
|
||||
alert('Asset successfully submited: ', JSON.stringify(ddo))
|
||||
}
|
||||
```
|
||||
|
||||
4. The last thing we need is a button to start our registration inside the render function just after `<h1>Marketplace app</h1>`:
|
||||
The last thing we need is a button to start our registration inside the render function just after `<h1>Marketplace app</h1>`:
|
||||
|
||||
```jsx
|
||||
<button onClick={() => this.submitAsset()}>Register asset</button>
|
||||
```
|
||||
```jsx
|
||||
<button onClick={() => this.submitAsset()}>Register asset</button>
|
||||
```
|
||||
|
||||
## Final Result
|
||||
|
||||
@ -135,12 +140,12 @@ class App extends Component {
|
||||
async submitAsset() {
|
||||
const accounts = await this.ocean.getAccounts()
|
||||
const ddo = await this.ocean.registerAsset(asset, accounts[0])
|
||||
alert('Asset successfully submited:', JSON.stringify(ddo))
|
||||
alert('Asset successfully submited: ', JSON.stringify(ddo))
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className="App">
|
||||
<div className="App App-header">
|
||||
<h1>Marketplace app</h1>
|
||||
<button onClick={() => this.submitAsset()}>Register asset</button>
|
||||
</div>
|
||||
|
@ -13,44 +13,69 @@ description: This tutorial shows how you can build a basic [React](https://react
|
||||
- Edit the `brizo.env` file and set all `AZURE_`... values.
|
||||
- Run `./start_ocean.sh --no-pleuston --local-pond-node`. This runs all services locally, including a local Parity Ethereum node.
|
||||
|
||||
## Tutorial Steps
|
||||
## New Create React App
|
||||
|
||||
1. Run `npx create-react-app marketplace` in you terminal. This will create a folder named `marketplace` with a boilerplate React app.
|
||||
2. Move to your app directory with `cd marketplace` and run `npm install @oceanprotocol/squid web3`. This adds the Ocean Protocol JavaScript library and Web3 packages to the app.
|
||||
3. At this point you can already run `npm start` which starts the app in your browser at [localhost:3000](http://localhost:3000).
|
||||
4. To clear the React spinning icon, open `src/App.js` and modify the source to:
|
||||
First, kick start your new React app by creating a boilerplate with Create React App:
|
||||
|
||||
```jsx
|
||||
import React, { Component } from 'react'
|
||||
import './App.css'
|
||||
```bash
|
||||
npx create-react-app marketplace
|
||||
```
|
||||
|
||||
class App extends Component {
|
||||
This will create a folder named `marketplace` with a boilerplate React app. Go into that new folder and add the Ocean Protocol JavaScript library and Web3 packages to the app's dependencies:
|
||||
|
||||
```bash
|
||||
cd marketplace/
|
||||
npm install @oceanprotocol/squid web3
|
||||
```
|
||||
|
||||
At this point you can already run `npm start` which starts the app in your browser at [localhost:3000](http://localhost:3000):
|
||||
|
||||

|
||||
|
||||
## Add Markup & Web3
|
||||
|
||||
Let's make it ours, open `src/App.js` and replace the whole source with:
|
||||
|
||||
```jsx
|
||||
import React, { Component } from 'react'
|
||||
import './App.css'
|
||||
|
||||
class App extends Component {
|
||||
render() {
|
||||
return (
|
||||
<div className="App">
|
||||
<div className="App App-header">
|
||||
<h1>Marketplace app</h1>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default App
|
||||
```
|
||||
export default App
|
||||
```
|
||||
|
||||
5. Below the `import './App.css'` line, let's import the packages we installed, set up web3 and unlock MetaMask accounts (if locked):
|
||||
Below the `import './App.css'` line, let's import the packages we installed, set up web3 and unlock MetaMask accounts (if locked):
|
||||
|
||||
```javascript
|
||||
import { Ocean } from '@oceanprotocol/squid'
|
||||
import * as Web3 from 'web3'
|
||||
```js
|
||||
import { Ocean } from '@oceanprotocol/squid'
|
||||
import * as Web3 from 'web3'
|
||||
|
||||
const web3 = new Web3(window.web3.currentProvider)
|
||||
window.ethereum.enable()
|
||||
```
|
||||
const web3 = new Web3(window.web3.currentProvider)
|
||||
window.ethereum.enable()
|
||||
```
|
||||
|
||||
6. At the beginning of your component, create a new Ocean instance with all configuration within the `componentDidMount` lifecycle method. All Ocean Protocol operations can be executed from this Ocean instance.
|
||||
After those steps you should see this, and MetaMask should have asked you to allow access to your account:
|
||||
|
||||
```javascript
|
||||
async componentDidMount() {
|
||||

|
||||

|
||||
|
||||
## Create Ocean instance
|
||||
|
||||
Now that we are successfully connected with Web3, we can setup our Ocean instance.
|
||||
|
||||
At the beginning of your component, create a new Ocean instance with all configuration within the `componentDidMount` lifecycle method. All Ocean Protocol operations can be executed from this Ocean instance.
|
||||
|
||||
```js
|
||||
async componentDidMount() {
|
||||
this.ocean = await new Ocean.getInstance({
|
||||
web3Provider: web3,
|
||||
nodeUri: "http://localhost:8545",
|
||||
@ -63,12 +88,14 @@ description: This tutorial shows how you can build a basic [React](https://react
|
||||
address: "0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0",
|
||||
})
|
||||
console.log("Finished loading contracts!")
|
||||
}
|
||||
```
|
||||
}
|
||||
```
|
||||
|
||||
## Final Result
|
||||
|
||||
That's it, if you have no errors in your `console.log` then you have successfully initialized an Ocean instance in you brand new React app and you are ready for the next steps in this tutorial.
|
||||
That's it, if you have no errors in your `console.log` then you have successfully initialized an Ocean instance in your brand new React app and you are ready for the [next part of this tutorial](/tutorials/react-publish-data-set/).
|
||||
|
||||

|
||||
|
||||
Here is the full source of `src/App.js` that you should have if you followed this tutorial:
|
||||
|
||||
@ -99,7 +126,7 @@ class App extends Component {
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className="App">
|
||||
<div className="App App-header">
|
||||
<h1>Marketplace app</h1>
|
||||
</div>
|
||||
)
|
||||
@ -108,3 +135,5 @@ class App extends Component {
|
||||
|
||||
export default App
|
||||
```
|
||||
|
||||
Move on to [Publish a Data Set](/tutorials/react-publish-data-set/).
|
||||
|
Loading…
x
Reference in New Issue
Block a user