mirror of
https://github.com/oceanprotocol/docs.git
synced 2024-11-26 19:49:26 +01:00
snippet fixes
This commit is contained in:
parent
1170dfe94e
commit
8e6131cb94
@ -14,25 +14,31 @@ Open `src/index.js` from your `marketplace/` folder.
|
||||
|
||||
## Search Assets
|
||||
|
||||
In the previous tutorial we added asset publishing. We can now search for published assets for consumption. Just after the `registerAsset()` function we can add a new `searchAssets` function that will handle search:
|
||||
In the previous tutorial we added asset publishing. We can now search for published assets for consumption.
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 54-67 GITHUB-EMBED
|
||||
We will store the search results in the local component state so we have to set its initial state first:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 15-18 GITHUB-EMBED
|
||||
|
||||
Just after the `registerAsset()` function we add a new `searchAssets` function that will handle search:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 54-67 GITHUB-EMBED
|
||||
|
||||
Now we need a button to start our search inside the `render()` function, just after the _Register asset_ button:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 114-115 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 114-115 GITHUB-EMBED
|
||||
|
||||
## Consume Assets
|
||||
## Consume Asset
|
||||
|
||||
Consuming means downloading one or multiple files attached to an asset. During that process the initial `url` value we added during the publish process for each file will be decrpyted and the file can be downloaded.
|
||||
|
||||
With the following code we start the consume process with the first search result, then go on to download its first attached file. Put it after the `searchAssets()` function:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 69-95 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 69-95 GITHUB-EMBED
|
||||
|
||||
We still need a button to start consumption. In the render function, just after the _Search assets_ button, add:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 116-118 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 116-118 GITHUB-EMBED
|
||||
|
||||
With all these buttons in place, you should see this:
|
||||
|
||||
@ -46,7 +52,7 @@ Have a look into `console.log` to see the various steps of the search and consum
|
||||
|
||||
Here is the full source of `src/index.js` that you should have if you followed this tutorial:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/master/src/index.js js GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/master/src/index.js jsx GITHUB-EMBED
|
||||
|
||||
## Git repository and CodeSandbox
|
||||
|
||||
|
@ -23,17 +23,17 @@ GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a94
|
||||
|
||||
Then import this asset definition at the top of `src/index.js`:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 5 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 5 GITHUB-EMBED
|
||||
|
||||
## Handle Asset Publishing
|
||||
|
||||
Now that we have an asset to submit, we need a function to handle it. Just before `render() {` let's add this `registerAsset` function:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 40-52 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 40-52 GITHUB-EMBED
|
||||
|
||||
The last thing we need is a button to start our registration inside the `render()` function:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 111-113 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 111-113 GITHUB-EMBED
|
||||
|
||||
Note how we disable the button when Web3 is not available to reduce user confusion. Within the Ocean Protocol flow of registering, searching, and consuming, only searching is possible without Web3.
|
||||
|
||||
@ -51,6 +51,6 @@ Have a look into `console.log` to see the various steps of the register process.
|
||||
|
||||
Here is the full source of `src/index.js` that you should have if you followed this tutorial:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 1-5,6-16,18-27,34-38,96-113,119-124 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 1-5,6-16,18-27,34-52,96-113,119-124 GITHUB-EMBED
|
||||
|
||||
**Move on to [Get & Use a Data Set](/tutorials/react-get-use-data-set/).**
|
||||
|
@ -58,7 +58,7 @@ GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a94
|
||||
|
||||
Create a new folder `src/` and within that a `index.js` file with the following content as our base, where we already import squid-js and web3.js:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 1-4,6,14,97-108,119-124 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 1-4,6,14,97-108,119-124 GITHUB-EMBED
|
||||
|
||||
At this point you can start up the app and see the result in your browser:
|
||||
|
||||
@ -76,15 +76,15 @@ We already are importing web3.js but we still need to enable account access for
|
||||
|
||||
To do that we add a simple check at top of `src/index.js` and then enable account access with:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 7-12 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 7-12 GITHUB-EMBED
|
||||
|
||||
And let's also output some warning for non-Web3 browsers within our `render()` function:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 109 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 109 GITHUB-EMBED
|
||||
|
||||
This should give you the following markup:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 1-4,6-14,97-109,119-124 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 1-4,6-14,97-109,119-124 GITHUB-EMBED
|
||||
|
||||
After those steps go to your browser. You should see MetaMask asking you to allow access to your account:
|
||||
|
||||
@ -98,7 +98,7 @@ Now that we are successfully connected with Web3, we can set up our Ocean instan
|
||||
|
||||
At the beginning of your component, create a new Ocean instance with all required endpoint configurations within the `componentDidMount` lifecycle method. All Ocean Protocol operations can be executed from this Ocean instance.
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 15-16,18-27,34-38 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 15-16,18-27,34-38 GITHUB-EMBED
|
||||
|
||||
This will initiate a connection to all Ocean components in Nile, load the contracts, and finally store the Ocean object in the local component state for reuse.
|
||||
|
||||
@ -112,7 +112,7 @@ That's it, if you have no errors in your `console.log` then you have successfull
|
||||
|
||||
Here is the full source of `src/index.js` that you should have if you followed this tutorial:
|
||||
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js js 1-4,6-16,18-27,34-38,96-109,119-124 GITHUB-EMBED
|
||||
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 1-4,6-16,18-27,34-38,96-109,119-124 GITHUB-EMBED
|
||||
|
||||
**Move on to [Publish a Data Set](/tutorials/react-publish-data-set/).**
|
||||
|
||||
@ -145,7 +145,7 @@ curl --data '{"address": "<YOUR ADDRESS>", "agent": "curl"}' -H "Content-Type: a
|
||||
|
||||
Finally, move back to your marketplace React app and modify the Ocean instance config in `src/index.js` to use the Spree endpoints:
|
||||
|
||||
```js
|
||||
```jsx
|
||||
const ocean = await new Ocean.getInstance({
|
||||
web3Provider: web3,
|
||||
nodeUri: 'http://localhost:8545',
|
||||
@ -162,3 +162,5 @@ Then start up the app as usual:
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
**Move on to [Publish a Data Set](/tutorials/react-publish-data-set/).**
|
||||
|
Loading…
Reference in New Issue
Block a user