From 9efc432e160b429a0643c38e28140bcf42af30a7 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Thu, 28 Jun 2018 13:45:27 +0200 Subject: Add history to redux thunk --- src/client/react/lib/getHistory.js | 22 ++++++++++++++++++++++ src/client/react/lib/url.js | 10 ++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/client/react/lib/getHistory.js (limited to 'src/client/react/lib') 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; -- cgit v1.1