aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/store/actions.js
blob: 96256e4f9fa55d52a80914773fe91d22166d7e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import users from '../users';
import purifyWeek from '../lib/purifyWeek';

export function setUser(newUser) {
  return (dispatch, getState, { getHistory }) => {
    const { user, updatePathname } = getHistory();

    if (newUser === user) {
      // EDGE CASE: If the user that is being selected is equal to the user
      // that is already selected, then updatePathname will not change
      // anything. This results in state.search not properly being resetted.
      // Example: If you search for 5H2 while already viewing 6-5H2, pressing
      // enter won't do anything unless this check is present.
      dispatch({ type: 'SEARCH/SET_USER', user });
    } else {
      updatePathname(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();

    const isCurrentWeek = moment().week() === newWeek;

    updateQuery({
      week: isCurrentWeek ? undefined : newWeek,
    });
  };
}

export function shiftWeek(shift) {
  return (dispatch, getState, { getHistory }) => {
    const { week } = getHistory();
    dispatch(setWeek(purifyWeek(week + shift)));
  };
}

export function showRoomFinder() {
  return (dispatch, getState, { getHistory }) => {
    const { user } = getHistory();

    if (user == null || users.byId[user].type !== 'r') {
      // We are not currently viewing a room, correct the situation.
      dispatch(setUser(users.allRoomIds[0]));
    }

    dispatch({ type: 'ROOM_FINDER/SHOW' });
  };
}