summaryrefslogtreecommitdiff
path: root/server/web/src/redux/allReducer.ts
blob: fe7200586ff416ab17b800719e1dfb0151740886 (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
import { IAllReducer } from '../interfaces/AllReducer.interface';
import { IClient } from '../interfaces/Client.interface';
import {
    ClientsLoadType,
    SessionLoadType,
    SessionAllType,
    SessionCloseType,
    SessionAddType,
    SessionRemoveType,
    ClientAddType,
    ClientRemoveType,
    ActivityUpdateType
} from './actions';

const initialState: IAllReducer = {
    clients: new Map<string, IClient>(),
    session: new Set<string>()
};

export const allReducer = (
    state: IAllReducer = initialState,
    action:
        | ClientsLoadType
        | SessionLoadType
        | SessionAllType
        | SessionCloseType
        | SessionAddType
        | SessionRemoveType
        | ClientAddType
        | ClientRemoveType
        | ActivityUpdateType
) => {
    switch (action.type) {
        case 'CLIENTS_LOAD': {
            return { ...state, clients: new Map<string, IClient>(action.payload) };
        }
        case 'SESSION_LOAD': {
            return { ...state, session: new Set<string>(action.payload) };
        }
        case 'SESSION_ALL': {
            return { ...state, session: new Set<string>(state.clients.keys()) };
        }
        case 'SESSION_CLOSE': {
            return { ...state, session: new Set<string>() };
        }
        case 'SESSION_ADD': {
            const updatedSession = new Set<string>(state.session);
            updatedSession.add(action.payload);
            return { ...state, session: updatedSession };
        }
        case 'SESSION_REMOVE': {
            const updatedSession = new Set<string>(state.session);
            updatedSession.delete(action.payload);
            return { ...state, session: updatedSession };
        }
        case 'CLIENT_ADD': {
            const updatedClients = new Map<string, IClient>(state.clients);
            updatedClients.set(action.payload.unique_id, action.payload.client);
            return { ...state, clients: updatedClients };
        }
        case 'CLIENT_REMOVE': {
            const updatedClients = new Map<string, IClient>(state.clients);
            updatedClients.delete(action.payload);
            return { ...state, clients: updatedClients };
        }
        case 'ACTIVITY_UPDATE': {
            const { unique_id, active_window, idle_time, resource_usage } = action.payload;
            const updatedClients = new Map<string, IClient>(state.clients);
            const client = updatedClients.get(unique_id);

            if (client !== undefined) {
                client.active_window = active_window;
                client.idle_time = idle_time;
                client.resource_usage = resource_usage;
                updatedClients.set(unique_id, client);
            }

            return { ...state, clients: updatedClients };
        }
        default:
            return state;
    }
};