1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-02 08:20:22 +01:00
docs/content/tutorials/react-get-use-data-set.md

66 lines
3.1 KiB
Markdown
Raw Normal View History

2018-12-05 12:08:47 +01:00
---
2020-04-29 12:27:29 +02:00
title: Search & Consume a Data Set
description: Tutorial to get and use a data set in a basic React app.
2018-12-05 12:08:47 +01:00
---
2018-12-17 14:02:35 +01:00
## Requirements
This is a continuation of the React App Tutorial. Make sure you already did the previous steps:
2018-12-17 14:02:35 +01:00
1. [React App Setup](/tutorials/react-setup/)
2. [Publish a Data Set](/tutorials/react-publish-data-set/)
2019-08-09 15:34:24 +02:00
Open `src/index.js` from your `marketplace/` folder.
2018-12-17 14:02:35 +01:00
2019-08-09 15:34:24 +02:00
## Search Assets
2018-12-17 14:02:35 +01:00
2019-08-09 15:57:39 +02:00
In the previous tutorial we added asset publishing. We can now search for published assets for consumption.
2018-12-17 14:02:35 +01:00
2019-08-09 15:57:39 +02:00
We will store the search results in the local component state so we have to set its initial state first:
2019-11-20 19:32:42 +01:00
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/e639e9ed4432e8b72ca453d50ed7bdaa36f1efb4/src/index.js jsx 15-19 GITHUB-EMBED
2019-08-09 15:57:39 +02:00
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
2018-12-17 14:02:35 +01:00
2019-08-09 15:34:24 +02:00
Now we need a button to start our search inside the `render()` function, just after the _Register asset_ button:
2018-12-17 14:02:35 +01:00
2019-11-20 19:32:42 +01:00
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 115 GITHUB-EMBED
2018-12-17 14:02:35 +01:00
2019-08-09 15:57:39 +02:00
## Consume Asset
2018-12-17 14:02:35 +01:00
2019-04-17 15:23:00 +02:00
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.
2019-08-09 15:34:24 +02:00
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:
2018-12-17 14:02:35 +01:00
2020-04-29 12:27:29 +02:00
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/index.js jsx 73-95 GITHUB-EMBED
2019-01-30 11:45:59 +01:00
2019-08-09 15:34:24 +02:00
We still need a button to start consumption. In the render function, just after the _Search assets_ button, add:
2019-01-30 11:45:59 +01:00
2019-08-09 15:57:39 +02:00
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/2765a7e6ae9a948d311d3949636cf832d2664900/src/index.js jsx 116-118 GITHUB-EMBED
2019-08-09 15:34:24 +02:00
With all these buttons in place, you should see this:
2018-12-17 14:02:35 +01:00
2019-08-09 15:34:24 +02:00
![React app with all actions in place](images/react-app-06.png)
2019-08-09 15:34:24 +02:00
Go ahead and click the _Search assets_ button, and then the _Consume asset_ button. Approve all the MetaMask dialog boxes.
Have a look into `console.log` to see the various steps of the search and consume process. If you have no errors in your `console.log` and can see your asset files listed, you have a working marketplace.
2019-04-18 16:32:17 +02:00
## Final Result
2018-12-17 14:02:35 +01:00
2019-08-09 13:30:13 +02:00
Here is the full source of `src/index.js` that you should have if you followed this tutorial:
2018-12-17 14:02:35 +01:00
2019-08-09 15:57:39 +02:00
GITHUB-EMBED https://github.com/oceanprotocol/react-tutorial/blob/master/src/index.js jsx GITHUB-EMBED
2019-08-09 15:46:37 +02:00
## Git repository and CodeSandbox
All code snippets in this tutorial are sourced from the [oceanprotocol/react-tutorial](https://github.com/oceanprotocol/react-tutorial) GitHub repository:
<repo name="react-tutorial"></repo>
The final source of this tutorial is also available as a CodeSandbox:
[![Edit react-tutorial](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/oceanprotocol/react-tutorial/tree/master/?fontsize=14)