aboutsummaryrefslogtreecommitdiff
path: root/app/features/recent-list/reducer.js
blob: c78b9b2fb1ae75388fee9ac3cac3550acbf0c4e4 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// @flow

import { CONFERENCE_ENDED, CONFERENCE_JOINED } from '../conference';
import { CONFERENCE_REMOVED } from './actionTypes';

import type { RecentListItem } from './types';

type State = {
    recentList: Array<RecentListItem>;
};

const DEFAULT_STATE = {
    recentList: []
};

/**
 * Reduces redux actions for features/recent-list.
 *
 * @param {State} state - Current reduced redux state.
 * @param {Object} action - Action which was dispatched.
 * @returns {State} - Updated reduced redux state.
 */
export default (state: State = DEFAULT_STATE, action: Object) => {
    switch (action.type) {
    case CONFERENCE_ENDED:
        return {
            ...state,
            recentList:
                _updateEndtimeOfConference(state.recentList, action.conference)
        };

    case CONFERENCE_JOINED:
        return {
            ...state,
            recentList: _insertConference(state.recentList, action.conference)
        };

    case CONFERENCE_REMOVED:
        return {
            ...state,
            recentList: _removeConference(state.recentList, action.conference)
        };

    default:
        return state;
    }
};

/**
 * Cleans a room name of all parameters.
 *
 * @param {string} roomName - The room name to be cleaned.
 * @returns {string} - The cleaned up room name.
 */
function _cleanRoomName(roomName: string) {
    const [ noQuery ] = roomName.split('?', 2);
    const [ noParams ] = noQuery.split('#', 2);

    return noParams;
}

/**
 * Insert Conference details in the recent list array.
 *
 * @param {Array<RecentListItem>} recentList - Previous recent list array.
 * @param {RecentListItem} newConference - Conference that has to be added
 * to recent list.
 * @returns {Array<RecentListItem>} - Updated recent list array.
 */
function _insertConference(
        recentList: Array<RecentListItem>,
        newConference: RecentListItem
) {
    // Add start time to conference.
    newConference.startTime = Date.now();

    newConference.room = _cleanRoomName(newConference.room);

    // Remove same conference.
    const newRecentList: Array<RecentListItem> = recentList.filter(
        (conference: RecentListItem) => _cleanRoomName(conference.room) !== newConference.room
            || conference.serverURL !== newConference.serverURL);

    // Add the conference at the beginning.
    newRecentList.unshift(newConference);

    return newRecentList;
}

/**
 * Remove a conference from the recent list array.
 *
 * @param {Array<RecentListItem>} recentList - Previous recent list array.
 * @param {RecentListItem} toRemove - Conference to be removed.
 * @returns {Array<RecentListItem>} - Updated recent list array.
 */
function _removeConference(
        recentList: Array<RecentListItem>,
        toRemove: RecentListItem
): Array<RecentListItem> {
    return recentList.filter(
        (conference: RecentListItem) => conference !== toRemove);
}

/**
 * Update the EndTime of the last conference.
 *
 * @param {Array<RecentListItem>} recentList - Previous recent list array.
 * @param {RecentListItem} conference - Conference for which endtime has to
 * be updated.
 * @returns {Array<RecentListItem>} - Updated recent list array.
 */
function _updateEndtimeOfConference(
        recentList: Array<RecentListItem>,
        conference: RecentListItem
) {
    for (const item of recentList.slice()) {
        item.room = _cleanRoomName(item.room);

        if (item.room === _cleanRoomName(conference.room)
                && item.serverURL === conference.serverURL) {
            item.endTime = Date.now();
        }
    }

    return recentList;
}