Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Network-aware LoadingContainer #9

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/react-components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ For 1.x.x this is how you import the different components:

```
import { newContextComponents } from "@drizzle/react-components";
const { AccountData, ContractData, ContractForm } = newContextComponents;
const { AccountData, ContractData, ContractForm, LoadingContainer } = newContextComponents;
```

`LoadingContainer` is not provided with the new context components currently. Also note that you must pass in `drizzle` and `drizzleState` for each of these components.
Note that you must pass in `drizzle` and `drizzleState` for each of these components.

### Legacy Context Components

Expand All @@ -30,14 +30,16 @@ import {

Refer to the included [test apps](#test-apps) for usage examples.

### LoadingContainer (Legacy only)
### LoadingContainer

This component wraps your entire app (but within the DrizzleProvider) and will show a loading screen until Drizzle, and therefore web3 and your contracts, are initialized.
This component wraps your entire app (but within the DrizzleProvider) and will show a loading screen until Drizzle, and therefore web3 and your contracts, are initialized. If using `networkWhitelist` in your `drizzleOptions`, it will also detect if the provider is connected to a non-whitelisted network and display a warning message.

`loadingComp` (component) The component displayed while Drizzle initializes.

`errorComp` (component) The component displayed if Drizzle initialization fails.

`networkMismatchComp` (component) The component displayed if the provider is connected to a non-whitelisted network.

### AccountData

`accountIndex` (number, required) Index of account from which to retrieve balance.
Expand Down
18 changes: 18 additions & 0 deletions packages/react-components/src/LoadingContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ class LoadingContainer extends Component {
);
}

if (this.props.web3.networkMismatch) {
if (this.props.networkMismatchComp) {
return this.props.networkMismatchComp;
}

return (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section can be re-written as:

return this.props.networkMismatchComp || <main>...</main>

This allows us to avoid the nested if statement and also makes it clear that this.props.networkMismatchComp is a component.

<main className="container network-warning">
<div className="pure-g">
<div className="pure-u-1-1">
<h1>⚠️</h1>
<p>This dapp does not support this network.</p>
</div>
</div>
</main>
);
}

if (
this.props.web3.status === "initialized" &&
Object.keys(this.props.accounts).length === 0
Expand Down Expand Up @@ -81,6 +98,7 @@ LoadingContainer.propTypes = {
web3: PropTypes.object.isRequired,
loadingComp: PropTypes.node,
errorComp: PropTypes.node,
networkMismatchComp: PropTypes.node,
};

/*
Expand Down
2 changes: 2 additions & 0 deletions packages/react-components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import LoadingContainer from "./LoadingContainer.js";
import AccountDataNew from "./new-context-api/AccountData";
import ContractDataNew from "./new-context-api/ContractData";
import ContractFormNew from "./new-context-api/ContractForm";
import LoadingContainerNew from "./new-context-api/LoadingContainer";

const newContextComponents = {
AccountData: AccountDataNew,
ContractData: ContractDataNew,
ContractForm: ContractFormNew,
LoadingContainer: LoadingContainerNew,
};

export {
Expand Down
99 changes: 99 additions & 0 deletions packages/react-components/src/new-context-api/LoadingContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { Children, Component } from "react";
import PropTypes from "prop-types";

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this comment block?

* Create component.
*/

class LoadingContainer extends Component {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component has a ton of if-statements, consider refactoring.

render() {
if (this.props.drizzleState) {
if (this.props.drizzleState.web3.status === "failed") {
if (this.props.errorComp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, this can be written as:

return this.props.errorComp || <main>...</main>

In order to avoid another if-statement.

return this.props.errorComp;
}

return (
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<h1>⚠️</h1>
<p>
This browser has no connection to the Ethereum network. Please
use the Chrome/FireFox extension MetaMask, or dedicated
Ethereum browsers Mist or Parity.
</p>
</div>
</div>
</main>
);
}

if (this.props.drizzleState.web3.networkMismatch) {
if (this.props.networkMismatchComp) {
return this.props.networkMismatchComp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}

return (
<main className="container network-warning">
<div className="pure-g">
<div className="pure-u-1-1">
<h1>⚠️</h1>
<p>This dapp does not support this network.</p>
</div>
</div>
</main>
);
}

if (
this.props.drizzleState.web3.status === "initialized" &&
Object.keys(this.props.drizzleState.accounts).length === 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider de-structuring and using intermediate variables.

) {
return (
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<h1>🦊</h1>
<p>
<strong>{"We can't find any Ethereum accounts!"}</strong>{" "}
Please check and make sure Metamask or your browser are
pointed at the correct network and your account is unlocked.
</p>
</div>
</div>
</main>
);
}

if (this.props.drizzleState.drizzleStatus.initialized) {
return Children.only(this.props.children);
}
}

if (this.props.loadingComp) {
return this.props.loadingComp;
}

return (
<main className="container loading-screen">
<div className="pure-g">
<div className="pure-u-1-1">
<h1>⚙️</h1>
<p>Loading dapp...</p>
</div>
</div>
</main>
);
}
}

LoadingContainer.propTypes = {
drizzleState: PropTypes.object,
children: PropTypes.node,
loadingComp: PropTypes.node,
errorComp: PropTypes.node,
networkMismatchComp: PropTypes.node,
};

export default LoadingContainer;
13 changes: 12 additions & 1 deletion ui-tests/react-context/src/App.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import React, { Component } from "react";
import { Drizzle } from '@drizzle/store';
import { DrizzleContext } from "@drizzle/react-plugin";
import { newContextComponents } from "@drizzle/react-components";

import "./App.css";

import drizzleOptions from "./drizzleOptions";
import MyComponent from "./MyComponent";

const drizzle = new Drizzle(drizzleOptions);
const { LoadingContainer } = newContextComponents;

class App extends Component {
render() {
return (
<DrizzleContext.Provider drizzle={drizzle}>
<MyComponent />
<DrizzleContext.Consumer>
{drizzleContext =>
<LoadingContainer drizzleState={drizzleContext.drizzleState}>
<MyComponent
drizzle={drizzleContext.drizzle}
drizzleState={drizzleContext.drizzleState}
/>
</LoadingContainer>
}
</DrizzleContext.Consumer>
</DrizzleContext.Provider>
);
}
Expand Down
Loading