From f5c7fc1c6aa985151cf9f151d2f3423772d6fc09 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Tue, 3 Jul 2018 20:09:24 +0200 Subject: client/RoomFinder: Refactor --- src/client/react/store/actions.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/client/react/store') diff --git a/src/client/react/store/actions.js b/src/client/react/store/actions.js index b123a8b..96256e4 100644 --- a/src/client/react/store/actions.js +++ b/src/client/react/store/actions.js @@ -18,6 +18,29 @@ export function setUser(newUser) { }; } +export function shiftRoom(shift) { + return (dispatch, getState, { getHistory }) => { + const { user } = getHistory(); + const { allRoomIds } = users; + + if (users.byId[user].type !== 'r') throw new Error('User must be a room'); + + 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 nextRoom = allRoomIds[nextRoomIndex]; + dispatch(setUser(nextRoom)); + }; +} + export function setWeek(newWeek) { return (dispatch, getState, { getHistory, moment }) => { const { updateQuery } = getHistory(); -- cgit v1.1