Last active
September 7, 2015 12:55
-
-
Save lexfrl/eebb41610f0d97d282ce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export default { | |
relocate(route) { | |
return { | |
type: "relocate", | |
route: route | |
} | |
} | |
} |
This file contains hidden or 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 { relocate } from "./actions"; | |
export default { | |
relocate (state, { route }) { | |
return Object.assign({}, state, { route }); | |
} | |
}; |
This file contains hidden or 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 from "react"; | |
import router from "../../../router"; | |
import { relocate } from "../../actions"; | |
const Link = React.createClass({ | |
render() { | |
let route = {}; | |
if (this.props.href) { | |
let hrefRoute = router.match(this.props.href); | |
if (hrefRoute) { | |
Object.assign(route, hrefRoute); | |
} | |
} | |
if (this.props.route) { | |
Object.assign(route, this.props.route); | |
} | |
if (Object.keys(route).length === 0) { | |
throw new Error ("Route is not found for the link "); | |
} | |
return ( | |
<a onClick={ (e) => { | |
e.preventDefault(); | |
dispatch(relocate(route)) }} | |
href={ this.props.href || router.url(route) }>{ this.props.children }</a> | |
) | |
} | |
}); | |
export default Link; |
This file contains hidden or 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 router from "../../../router" | |
import { relocate } from "../../actions"; | |
const Location = React.createClass({ | |
getInitialState() { | |
return { | |
pushState: true | |
}; | |
}, | |
componentDidMount() { | |
if (typeof window === "undefined") { | |
return; | |
} | |
window.onpopstate = (e) => { | |
this.setState({pushState: false}); | |
dispatch(relocate(router.match(window.location.href))); | |
} | |
}, | |
componentWillReceiveProps(nextProps) { | |
if (typeof window === "undefined") { | |
return; | |
} | |
if (!this.shouldComponentUpdate(nextProps)) { | |
return; | |
} | |
if (this.state.pushState) { | |
window.history.pushState(null, null, router.url(nextProps.route)); | |
} | |
this.setState({pushState: true}); | |
}, | |
shouldComponentUpdate(nextProps) { | |
return router.url(nextProps.route) !== router.url(this.props.route); | |
}, | |
render() { | |
return null; | |
} | |
}); | |
export default Location; |
This file contains hidden or 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 from "react"; | |
import Link from "./components/Link" | |
import Loading from "./components/Loading" | |
import _ from "lodash"; | |
import { isLoading } from "../common"; | |
const Page = React.createClass({ | |
render() { | |
const pages = { | |
"": require("./pages/Login"), | |
"friends": require("./pages/Friends"), | |
"results": require("./pages/Results") | |
}; | |
const { route } = this.props.state; | |
let P = pages[route.name] || pages[""]; | |
let notLoadedFields = P.getRequiredFields().filter(field => isLoading(this.props.state, field)); | |
if (notLoadedFields.length) { | |
P = Loading; | |
}; | |
return ( | |
<div> | |
<ul> | |
<li> | |
<Link route={Object.assign({}, route, { name: "" })}>Login</Link> | |
<Link route={Object.assign({}, route, { name: "friends" })}>Friends</Link> | |
<Link route={Object.assign({}, route, { name: "results" })}>Results</Link> | |
</li> | |
</ul> | |
<P {...this.props} /> | |
</div> | |
) | |
} | |
}); | |
export default Page; |
This file contains hidden or 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 Router from "routr-map"; | |
export default new Router({ | |
"login": { | |
}, | |
"friends": { | |
":id": { | |
} | |
}, | |
"results": { | |
":id": { | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using https://github.com/AlexeyFrolov/routr-map