diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-07-03 20:51:44 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-07-03 20:51:44 +0200 |
commit | 15e6ef21bbd9290376380ddbac1b5b76e085ef80 (patch) | |
tree | 28632a9e735c8d64f015445bf0380c7767535dff /src/client/react/store | |
parent | eddef78b794c62a1a257a742237634fab2648511 (diff) |
client/shiftRoom: Use withinRange
Diffstat (limited to 'src/client/react/store')
-rw-r--r-- | src/client/react/store/actions.js | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/src/client/react/store/actions.js b/src/client/react/store/actions.js index 96256e4..36dc748 100644 --- a/src/client/react/store/actions.js +++ b/src/client/react/store/actions.js @@ -1,5 +1,6 @@ import users from '../users'; import purifyWeek from '../lib/purifyWeek'; +import withinRange from '../lib/withinRange'; export function setUser(newUser) { return (dispatch, getState, { getHistory }) => { @@ -27,16 +28,12 @@ export function shiftRoom(shift) { const currentRoom = user; const currentRoomIndex = allRoomIds.indexOf(currentRoom); - - let nextRoomIndex = currentRoomIndex + shift; - - if (nextRoomIndex < 0) { - nextRoomIndex = allRoomIds.length - 1; - } else if (nextRoomIndex > allRoomIds.length - 1) { - nextRoomIndex = 0; - } - + const nextRoomIndex = withinRange( + currentRoomIndex + shift, + allRoomIds.length - 1, + ); const nextRoom = allRoomIds[nextRoomIndex]; + dispatch(setUser(nextRoom)); }; } |