diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-06-28 13:45:27 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-06-28 13:45:27 +0200 |
commit | 9efc432e160b429a0643c38e28140bcf42af30a7 (patch) | |
tree | 2ff1722ed690dc673c8a83cbbf03b40b6526d9f3 /src/client/react/lib | |
parent | 9e55c4b1a151b434cba4b4425514c3aeac6f22d8 (diff) |
Add history to redux thunk
Diffstat (limited to 'src/client/react/lib')
-rw-r--r-- | src/client/react/lib/getHistory.js | 22 | ||||
-rw-r--r-- | src/client/react/lib/url.js | 10 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/client/react/lib/getHistory.js b/src/client/react/lib/getHistory.js new file mode 100644 index 0000000..642a9a8 --- /dev/null +++ b/src/client/react/lib/getHistory.js @@ -0,0 +1,22 @@ +import { + makeSetUser, + makeSetWeek, + weekFromLocation, + userFromLocation, +} from './url'; + +export default function makeGetHistory(history) { + return function getHistory() { + const user = userFromLocation(history.location); + const week = weekFromLocation(history.location); + const setUser = makeSetUser(history); + const setWeek = makeSetWeek(history); + + return { + user, + week, + setUser, + setWeek, + }; + }; +} diff --git a/src/client/react/lib/url.js b/src/client/react/lib/url.js index 644fd74..fcd3e6a 100644 --- a/src/client/react/lib/url.js +++ b/src/client/react/lib/url.js @@ -33,6 +33,16 @@ export function userFromMatch(match) { return user; } +export function userFromLocation(location) { + const match = location.pathname.match(/^\/([stcr])\/([-0-9a-zA-Z]+)/); + if (!match) return null; + + const user = `${match[1]}/${match[2]}`; + if (!users.allIds.includes(user)) return null; + + return user; +} + export function weekFromLocation(location) { const weekStr = queryString.parse(location.search).week; |