Last active
September 17, 2016 18:19
-
-
Save lexfrl/14ec43f5e836d0b767fe52bd62b5ae29 to your computer and use it in GitHub Desktop.
An app entry point BEFORE React Router was "invented"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { PropTypes } from "react"; | |
import { isEqual } from "lodash"; | |
import Location from "./components/Location"; | |
import Page from "./components/Page"; | |
import NotFoundPage from "./pages/NotFoundPage"; | |
import ErrorPage from "./pages/ErrorPage"; | |
import LoginPage from "./pages/LoginPage"; | |
import FriendsPage from "./pages/FriendsPage"; | |
import ConnectionPage from "./pages/ConnectionPage"; | |
//import trackPageView from "./utils/trackPageView"; | |
const debug = require("debug")("giftter"); | |
if (process.env.BROWSER) { | |
require("./style/Application.scss"); | |
} | |
const Application = React.createClass({ | |
propTypes: { | |
state: PropTypes.object.isRequired, | |
}, | |
componentDidUpdate(prevProps) { | |
const { documentTitle, route } = this.props; | |
if (prevProps.documentTitle !== documentTitle) { | |
document.title = documentTitle; | |
} | |
if (!isEqual(route, prevProps.route)) { | |
//trackPageView(); | |
} | |
}, | |
render() { | |
const { state } = this.props; | |
return ( | |
<Page> | |
{ | |
[ | |
( | |
state.request && state.request && <Location request={ state.request } /> | |
), | |
this.renderRoute(state) | |
] | |
} | |
</Page> | |
); | |
}, | |
renderRoute(state) { | |
let { route } = state; | |
debug("Rendering route %s", route.name); | |
switch (route.name) { | |
case "login": | |
return <LoginPage />; | |
break; | |
case "users": | |
let user; | |
for (let id in state.users) { | |
user = state.users[id]; | |
} | |
return user && <FriendsPage user={ user } />; | |
break; | |
case "connection": | |
return <ConnectionPage connection={ state.connection } />; | |
break; | |
default: | |
console.warn(`Missing handler for route with name ${route.name}`); | |
return < NotFoundPage />; | |
break; | |
} | |
} | |
}); | |
export default Application; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The 'VIRTUAL' location bar | |
import React, { PropTypes } from "react"; | |
import Logo from "../components/Logo"; | |
import slots from "../slots"; | |
const Location = React.createClass({ | |
getInitialState() { | |
return { | |
pushState: true | |
}; | |
}, | |
componentDidMount() { | |
if (!process.env.BROWSER) { | |
return; | |
} | |
window.onpopstate = (e) => { | |
this.setState({pushState: false}); | |
slots.set("request", { url: window.location.href, method: "GET", body: null }, false).commit(); | |
} | |
}, | |
componentWillReceiveProps(nextProps) { | |
console.log(nextProps); | |
if (!process.env.BROWSER) { | |
return; | |
} | |
if (!this.shouldComponentUpdate(nextProps)) { | |
return; | |
} | |
if (this.state.pushState && nextProps.request.method === "GET") { | |
window.history.pushState(null, null, nextProps.request.url); | |
} | |
this.setState({pushState: true}); | |
}, | |
shouldComponentUpdate(nextProps) { | |
return nextProps.request.url != this.props.request.url; | |
}, | |
render() { | |
return null; | |
} | |
}); | |
export default Location; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment