aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-07-03 20:51:44 +0200
committerNoah Loomans <noahloomans@gmail.com>2018-07-03 20:51:44 +0200
commit15e6ef21bbd9290376380ddbac1b5b76e085ef80 (patch)
tree28632a9e735c8d64f015445bf0380c7767535dff
parenteddef78b794c62a1a257a742237634fab2648511 (diff)
client/shiftRoom: Use withinRange
-rw-r--r--src/client/react/store/actions.js15
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));
};
}