Skip to content

Commit

Permalink
Fixed data fetching: Mount *all* the widgets at once
Browse files Browse the repository at this point in the history
  • Loading branch information
juhamust committed Oct 12, 2016
1 parent 88bd88d commit 81e3a64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mozaik-ext-switcher",
"version": "0.1.0",
"version": "0.2.0",
"description": "Mozaïk extension for switching between widgets within same dashboard",
"author": "Juha Mustonen",
"license": "MIT",
Expand All @@ -21,6 +21,7 @@
"babelify": "7.2.0",
"bluebird": "3.3.5",
"chalk": "^1.1.3",
"classnames": "^2.2.5",
"convict": "^0.6.1",
"json5": "^0.5.0",
"lodash": "^4.10.0",
Expand Down
25 changes: 18 additions & 7 deletions src/components/Widgets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';
import React, { Component, PropTypes } from 'react';
import ComponentRegistry from 'mozaik/src/browser/component-registry';
import Widget from 'mozaik/src/browser/components/Widget.jsx';
import classNames from 'classnames';


class Widgets extends Component {
Expand All @@ -19,7 +20,6 @@ class Widgets extends Component {
nextWidgetIndex = 0;
}

console.log('Next widdget', nextWidgetIndex);
if (this.mounted) {
this.setState({
widgetIndex: nextWidgetIndex
Expand All @@ -37,16 +37,27 @@ class Widgets extends Component {
}

render() {
const childProps = this.props.widgets[this.state.widgetIndex];
const { type } = this.props.widgets[this.state.widgetIndex];
const widget = React.createElement(ComponentRegistry.get(type), childProps);
// NOTE: Render all the elements to mount them: Also triggers the data fetching
const widgetElements = this.props.widgets.map((widgetProps, index) => {
const widget = React.createElement(ComponentRegistry.get(widgetProps.type), widgetProps);
const typeClass = widgetProps.type.replace('_', '-').replace('.', '__');

// Set class according to component type
const cssClass = `widget ${ type.replace('_', '-').replace('.', '__') }`;
let widgetClass = classNames({
'widget': true,
[typeClass]: true,
'switcher__widgets--hidden': this.state.widgetIndex !== index
});

return (
<div className="widget__wrapper">
<div className={widgetClass}>{widget}</div>
</div>
);
});

return (
<div className="widget__wrapper">
<div className={cssClass}>{widget}</div>
{widgetElements}
</div>
);
}
Expand Down
4 changes: 4 additions & 0 deletions styl/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
height: 100%;
}
}

.switcher__widgets--hidden {
display: none;
}

0 comments on commit 81e3a64

Please sign in to comment.