From 34be8b5ae60276a13c484c6c63c1fef28ed0b089 Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Sat, 18 Mar 2017 16:44:37 -0700 Subject: [PATCH] feat(widgets): refreshing ability --- src/components/Loader.js | 8 +++++++- src/components/Widget.js | 31 ++++++++++++++++++++----------- src/components/widgets/Crypto.js | 18 +++++++++++++++--- src/styles/app.scss | 9 +++++++++ 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/components/Loader.js b/src/components/Loader.js index 0ae61fc..b0d8c61 100644 --- a/src/components/Loader.js +++ b/src/components/Loader.js @@ -2,9 +2,15 @@ import React, { Component } from 'react' class Loader extends Component { + static defaultProps = { + className: '' + } + render () { + const { className } = this.props + return ( -
+
) diff --git a/src/components/Widget.js b/src/components/Widget.js index e0058ae..01be091 100644 --- a/src/components/Widget.js +++ b/src/components/Widget.js @@ -92,16 +92,17 @@ class Widget extends Component { dispatch(fetchWidget(id)) } - toggleEditMode () { + toggleEditMode = () => { this.setState({ edit: !this.state.edit }) } - removeWidget (id) { + removeWidget = () => { + const { id } = this.props this.props.dispatch(removeWidget(id)) this.props.dispatch(save()) } - configureWidget (config, shouldClose) { + configureWidget = (config, shouldClose) => { const { id, dispatch } = this.props dispatch(configWidget({ id, config })) if (shouldClose) { this.setState({ edit: false }) } @@ -116,7 +117,7 @@ class Widget extends Component { connectDragPreview } = this.props const { edit } = this.state - const { loading, loaded, type } = widget + const { loading, loaded, lastFetch, type } = widget const component = widgets[widget.type] const W = widgetsComponents[type] @@ -135,12 +136,12 @@ class Widget extends Component {
{editMode && (
-
+
{moveButton} {component && !!component.config && ( -
+
)} @@ -151,7 +152,19 @@ class Widget extends Component { {component && (
- {loading && ( + {(edit || (loaded || lastFetch)) && ( +
+ + {loading && ()} +
+ )} + + {(loading && !lastFetch) && (
@@ -163,10 +176,6 @@ class Widget extends Component {
)} - {(edit || !loading && loaded) && ( - - )} -
)} diff --git a/src/components/widgets/Crypto.js b/src/components/widgets/Crypto.js index 25a4dfc..539104b 100644 --- a/src/components/widgets/Crypto.js +++ b/src/components/widgets/Crypto.js @@ -11,20 +11,32 @@ class Crypto extends Component { state = { now: new Date() } componentWillMount () { - this._int = setInterval(() => this.setState({ now: new Date() }), 1E3) + this.start() } - componentWillUpdate () { + componentWillUpdate (nextProps) { const { fetchWidget, id } = this.props - if (this.getDiff() > 30) { + + if (this._int && this.getDiff() > 30) { + clearInterval(this._int) + this._int = null fetchWidget(id) } + + if (!this._int && nextProps.loaded) { + this.start() + } } componentWillUnmount () { clearInterval(this._int) } + start = () => { + this.setState({ now: new Date() }) + this._int = setInterval(() => this.setState({ now: new Date() }), 1E3) + } + getDiff = () => { const { now } = this.state const { values: { timestamp } } = this.props.data diff --git a/src/styles/app.scss b/src/styles/app.scss index db80168..99ce01c 100644 --- a/src/styles/app.scss +++ b/src/styles/app.scss @@ -157,6 +157,15 @@ body { background: $cb1; } +.refreshing { + position: absolute; + top: 0; + right: 0; + height: 1rem !important; + width: 1rem !important; + font-size: 1rem !important; +} + .Loader { @extend .z; width: 2.5rem;