const PropTypes = require('prop-types'); const React = require('react'); const ReactDOM = require('react-dom'); const {Provider} = require('react-redux'); const {createStore, applyMiddleware} = require('redux'); const throttle = require('redux-throttle').default; const GUI = require('./containers/gui.jsx'); const log = require('./lib/log'); const ProjectLoader = require('./lib/project-loader'); const reducer = require('./reducers/gui'); const styles = require('./index.css'); class App extends React.Component { constructor (props) { super(props); this.fetchProjectId = this.fetchProjectId.bind(this); this.updateProject = this.updateProject.bind(this); this.state = { projectId: null, projectData: this.fetchProjectId().length ? null : JSON.stringify(ProjectLoader.DEFAULT_PROJECT_DATA) }; } componentDidMount () { window.addEventListener('hashchange', this.updateProject); this.updateProject(); } componentWillUnmount () { window.removeEventListener('hashchange', this.updateProject); } fetchProjectId () { return window.location.hash.substring(1); } updateProject () { const projectId = this.fetchProjectId(); if (projectId !== this.state.projectId) { if (projectId.length < 1) { return this.setState({ projectId: projectId, projectData: JSON.stringify(ProjectLoader.DEFAULT_PROJECT_DATA) }); } ProjectLoader.load(projectId, (err, body) => { if (err) return log.error(err); this.setState({projectData: body}); }); this.setState({projectId: projectId}); } } render () { if (this.state.projectData === null) return null; return ( <GUI basePath={this.props.basePath} projectData={this.state.projectData} /> ); } } App.propTypes = { basePath: PropTypes.string }; const appTarget = document.createElement('div'); appTarget.className = styles.app; document.body.appendChild(appTarget); const store = applyMiddleware( throttle(300, {leading: true, trailing: true}) )(createStore)( reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() ); ReactDOM.render(( <Provider store={store}> <App basePath={process.env.BASE_PATH} /> </Provider> ), appTarget);