From 71e7a429d76948dedb539b52e9ffda9c023829cb Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Wed, 4 Jan 2017 15:15:25 +0100 Subject: [PATCH] Only fetch App when necessary (#4023) * Only fetch App when necessary. Show loadings + 404 #3914 * PR Grumble --- js/src/views/Dapp/dapp.css | 20 +++++++++++ js/src/views/Dapp/dapp.js | 59 +++++++++++++++++++++++++++++--- js/src/views/Dapps/dapps.js | 4 +++ js/src/views/Dapps/dappsStore.js | 36 +++++++++++++++---- 4 files changed, 109 insertions(+), 10 deletions(-) diff --git a/js/src/views/Dapp/dapp.css b/js/src/views/Dapp/dapp.css index b0f8f06e6..70b39ce08 100644 --- a/js/src/views/Dapp/dapp.css +++ b/js/src/views/Dapp/dapp.css @@ -20,3 +20,23 @@ height: 100%; width: 100%; } + +.full { + width: 100vw; + height: 100vh; + margin: 0; + padding: 0; + background: white; + font-family: 'Roboto', sans-serif; + font-size: 16px; + font-weight: 300; + + .text { + text-align: center; + padding: 5em; + font-size: 2em; + color: #999; + overflow: hidden; + text-overflow: ellipsis; + } +} diff --git a/js/src/views/Dapp/dapp.js b/js/src/views/Dapp/dapp.js index 87245ca72..7f1416392 100644 --- a/js/src/views/Dapp/dapp.js +++ b/js/src/views/Dapp/dapp.js @@ -16,6 +16,7 @@ import React, { Component, PropTypes } from 'react'; import { observer } from 'mobx-react'; +import { FormattedMessage } from 'react-intl'; import DappsStore from '../Dapps/dappsStore'; @@ -25,21 +26,71 @@ import styles from './dapp.css'; export default class Dapp extends Component { static contextTypes = { api: PropTypes.object.isRequired - } + }; static propTypes = { params: PropTypes.object }; + state = { + app: null, + loading: true + }; + store = DappsStore.get(this.context.api); + componentWillMount () { + const { id } = this.props.params; + this.loadApp(id); + } + + componentWillReceiveProps (nextProps) { + if (nextProps.params.id !== this.props.params.id) { + this.loadApp(nextProps.params.id); + } + } + + loadApp (id) { + this.setState({ loading: true }); + + this.store + .loadApp(id) + .then((app) => { + this.setState({ loading: false, app }); + }) + .catch(() => { + this.setState({ loading: false }); + }); + } + render () { const { dappsUrl } = this.context.api; - const { id } = this.props.params; - const app = this.store.apps.find((app) => app.id === id); + const { app, loading } = this.state; + + if (loading) { + return ( +
+
+ +
+
+ ); + } if (!app) { - return null; + return ( +
+
+ +
+
+ ); } let src = null; diff --git a/js/src/views/Dapps/dapps.js b/js/src/views/Dapps/dapps.js index e800263e4..fa7c44878 100644 --- a/js/src/views/Dapps/dapps.js +++ b/js/src/views/Dapps/dapps.js @@ -45,6 +45,10 @@ class Dapps extends Component { store = DappsStore.get(this.context.api); permissionStore = new PermissionStore(this.context.api); + componentWillMount () { + this.store.loadAllApps(); + } + render () { let externalOverlay = null; if (this.store.externalOverlayVisible) { diff --git a/js/src/views/Dapps/dappsStore.js b/js/src/views/Dapps/dappsStore.js index 42342aff3..8cca4d3f7 100644 --- a/js/src/views/Dapps/dappsStore.js +++ b/js/src/views/Dapps/dappsStore.js @@ -48,17 +48,43 @@ export default class DappsStore { this.readDisplayApps(); this.loadExternalOverlay(); - this.loadApps(); this.subscribeToChanges(); } - loadApps () { + /** + * Try to find the app from the local (local or builtin) + * apps, else fetch from the node + */ + loadApp (id) { const { dappReg } = Contracts.get(); - Promise + return this + .loadLocalApps() + .then(() => { + const app = this.apps.find((app) => app.id === id); + + if (app) { + return app; + } + + return this.fetchRegistryApp(dappReg, id, true); + }); + } + + loadLocalApps () { + return Promise .all([ this.fetchBuiltinApps().then((apps) => this.addApps(apps)), - this.fetchLocalApps().then((apps) => this.addApps(apps)), + this.fetchLocalApps().then((apps) => this.addApps(apps)) + ]); + } + + loadAllApps () { + const { dappReg } = Contracts.get(); + + return Promise + .all([ + this.loadLocalApps(), this.fetchRegistryApps(dappReg).then((apps) => this.addApps(apps)) ]) .then(this.writeDisplayApps); @@ -67,8 +93,6 @@ export default class DappsStore { static get (api) { if (!instance) { instance = new DappsStore(api); - } else { - instance.loadApps(); } return instance;