-
Ray Schamp authored
In preparation to split the sprites from the stage, start managing the target list state in a single place.
Ray Schamp authoredIn preparation to split the sprites from the stage, start managing the target list state in a single place.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
index.jsx 2.13 KiB
const React = require('react');
const ReactDOM = require('react-dom');
const {Provider} = require('react-redux');
const {createStore} = require('redux');
const GUI = require('./containers/gui.jsx');
const log = require('./lib/log');
const ProjectLoader = require('./lib/project-loader');
const reducer = require('./reducers/gui');
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: 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 () {
return (
<GUI
basePath={this.props.basePath}
projectData={this.state.projectData}
/>
);
}
}
App.propTypes = {
basePath: React.PropTypes.string
};
const appTarget = document.createElement('div');
document.body.appendChild(appTarget);
const store = createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
ReactDOM.render((
<Provider store={store}>
<App basePath={process.env.BASE_PATH} />
</Provider>
), appTarget);