From 15e6ef21bbd9290376380ddbac1b5b76e085ef80 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Tue, 3 Jul 2018 20:51:44 +0200 Subject: client/shiftRoom: Use withinRange --- src/client/react/store/actions.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'src/client') 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)); }; } -- cgit v1.1