Skip to content

Instantly share code, notes, and snippets.

@oki
Created March 21, 2018 09:39
Show Gist options
  • Select an option

  • Save oki/1d635eb3089c8ec9fefa9cddac0f26bb to your computer and use it in GitHub Desktop.

Select an option

Save oki/1d635eb3089c8ec9fefa9cddac0f26bb to your computer and use it in GitHub Desktop.
import React from 'react'
import ReactDOM from 'react-dom'
import isEmpty from 'lodash/isEmpty'
import request from 'superagent'
import { objectToQueryString, removeHashFromUrl, locationHashParams } from './utils';
import FactoryWidget from './components/factory_widget'
import './stylesheets/style.scss';
class AppWidgets extends React.Component {
constructor(props) {
super(props);
this.state = {
widgetData: []
};
}
componentDidMount() {
this.loadData();
}
loadData(data) {
request
.get('/admin/api/dashboard.json')
.query({ "widgets[]": this.widgets() })
.then(response => {
let data = JSON.parse(response.text);
console.log("Loaded data");
console.log(data);
this.setState({widgetData: data});
});
}
widgets() {
if (_.isEmpty(this._widgets)) {
const { attributes } = this.props;
this._widgets = _.filter(attributes, (attribute) => {
return attribute.name.startsWith("data-");
}).map((attribute) => {
return attribute.value.split(/,\s*/);
})
}
return this._widgets[0];
}
shouldComponentUpdate(nextProps, nextState) {
return false;
}
render() {
console.log(`Rendering mothfkr!!`);
console.log(this.state);
let widgetNames = _.intersection(Object.keys(this.state.widgetData), this.widgets());
return(
<div>
{widgetNames.map((widgetType) => {
console.log(`Rendering ${widgetType}`);
return(<FactoryWidget
key={widgetType}
widget={widgetType}
widgetData={this.state.widgetData[widgetType]}
handleChange={this.handleChange}
/>)
})}
</div>
)
}
}
let rootElement = document.getElementById('dasboard-widgets');
ReactDOM.render(
<AppWidgets attributes={rootElement.attributes} />,
rootElement
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment