Skip to content
Snippets Groups Projects
index.jsx 1.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • const React = require('react');
    const ReactDOM = require('react-dom');
    
    Ray Schamp's avatar
    Ray Schamp committed
    const GUI = require('./containers/gui.jsx');
    
    const log = require('./lib/log');
    const ProjectLoader = require('./lib/project-loader');
    
    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 location.hash.substring(1);
        }
        updateProject () {
    
    Ray Schamp's avatar
    Ray Schamp committed
            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);
    
    ReactDOM.render(<App basePath={process.env.BASE_PATH} />, appTarget);