aboutsummaryrefslogtreecommitdiff
path: root/app/features/recent-list/reducer.js
blob: 17e86db497991bfb2e3de021783c5350ba77c9f3 (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
// @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;
    }
};

/**
 * 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();

    // Remove same conference.
    const newRecentList: Array<RecentListItem> = recentList.filter(
        (conference: RecentListItem) => 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) {
        if (item.room === conference.room
                && item.serverURL === conference.serverURL) {
            item.endTime = Date.now();
            break;
        }
    }

    return recentList;
}