aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2018-03-20 16:12:36 +0100
committerNoah Loomans <noahloomans@gmail.com>2018-03-20 16:12:36 +0100
commit9470394383bb0105053937a149472189940016df (patch)
tree94bc7df1596f765dea1233facbf157982e750787
parentff2b3eb8f479065431d0a56b21fa931dac26880c (diff)
Refactor RoomFinder
-rw-r--r--src/client/react/components/container/RoomFinder.js31
-rw-r--r--src/client/react/lib/url.js13
2 files changed, 24 insertions, 20 deletions
diff --git a/src/client/react/components/container/RoomFinder.js b/src/client/react/components/container/RoomFinder.js
index 94257f7..5f25fd7 100644
--- a/src/client/react/components/container/RoomFinder.js
+++ b/src/client/react/components/container/RoomFinder.js
@@ -24,7 +24,7 @@ import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { Button, ButtonIcon } from 'rmwc/Button';
import users from '../../users';
-import { userFromMatch } from '../../lib/url';
+import { setUser, userFromMatch } from '../../lib/url';
class HelpBox extends React.Component {
static propTypes = {
@@ -34,12 +34,13 @@ class HelpBox extends React.Component {
// react-router
match: PropTypes.object.isRequired,
+ location: PropTypes.object.isRequired,
+ history: PropTypes.object.isRequired,
}
constructor(props) {
super(props);
- this.setRoom = this.setRoom.bind(this);
this.changeRoom = this.changeRoom.bind(this);
}
@@ -47,8 +48,7 @@ class HelpBox extends React.Component {
const user = userFromMatch(this.props.match);
// Have we just been mounted and are we viewing something else then a room?
if (this.props.isVisible && users.byId[user].type !== 'r') {
- // Set the room to the first room.
- this.setRoom(this.getAllRooms()[0]);
+ this.props.dispatch({ type: 'ROOM_FINDER/HIDE' });
}
}
@@ -59,35 +59,26 @@ class HelpBox extends React.Component {
// Did we just become visible? Set the user to a room. If not, hide.
if (!this.props.isVisible) {
// Set the room to the first room.
- this.setRoom(this.getAllRooms()[0], nextProps);
+ setUser(users.allRoomIds[0], nextProps.location, nextProps.history);
} else {
this.props.dispatch({ type: 'ROOM_FINDER/HIDE' });
}
}
}
- getAllRooms() {
- return users.allUsers.filter(user => user.type === 'r').map(room => room.id);
- }
-
- setRoom(roomId, props = this.props) {
- const query = props.location.search;
- props.history.push(`/${roomId}${query}`);
- }
-
changeRoom(change) {
+ const { allRoomIds } = users;
const currentRoom = userFromMatch(this.props.match);
- const allRooms = this.getAllRooms();
- const currentRoomIndex = allRooms.indexOf(currentRoom);
+ const currentRoomIndex = allRoomIds.indexOf(currentRoom);
let nextRoomIndex = currentRoomIndex + change;
if (nextRoomIndex < 0) {
- nextRoomIndex = allRooms.length - 1;
- } else if (nextRoomIndex > allRooms.length - 1) {
+ nextRoomIndex = allRoomIds.length - 1;
+ } else if (nextRoomIndex > allRoomIds.length - 1) {
nextRoomIndex = 0;
}
- const nextRoom = allRooms[nextRoomIndex];
- this.setRoom(nextRoom);
+ const nextRoom = allRoomIds[nextRoomIndex];
+ setUser(nextRoom, this.props.location, this.props.history);
}
render() {
diff --git a/src/client/react/lib/url.js b/src/client/react/lib/url.js
index a1fa2fe..77731fc 100644
--- a/src/client/react/lib/url.js
+++ b/src/client/react/lib/url.js
@@ -42,3 +42,16 @@ export function weekFromLocation(location) {
return purifyWeek(parseInt(weekStr, 10));
}
+
+export function setUser(userId, location, history) {
+ const query = location.search;
+ history.push(`/${userId}${query}`);
+}
+
+export function setWeek(week, location, history) {
+ const query = queryString.stringify({
+ ...queryString.parse(location.search),
+ week,
+ });
+ history.push(`${location.pathname}${query}`);
+}