aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/eventLog.js
blob: 76572a5d3bffaf30fa01b34e38caa8d523a00d4f (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
import reduceList, * as listActions from './utils/list'
import reduceView, * as viewActions from './utils/view'

export const ADD               = 'EVENTS_ADD'
export const RECEIVE           = 'EVENTS_RECEIVE'
export const TOGGLE_VISIBILITY = 'EVENTS_TOGGLE_VISIBILITY'
export const TOGGLE_FILTER     = 'EVENTS_TOGGLE_FILTER'
export const UNKNOWN_CMD       = 'EVENTS_UNKNOWN_CMD'
export const FETCH_ERROR       = 'EVENTS_FETCH_ERROR'

const defaultState = {
    logId: 0,
    visible: false,
    filters: { debug: false, info: true, web: true },
    list: reduceList(undefined, {}),
    view: reduceView(undefined, {}),
}

export default function reduce(state = defaultState, action) {
    switch (action.type) {

        case TOGGLE_VISIBILITY:
            return {
                ...state,
                visible: !state.visible
            }

        case TOGGLE_FILTER:
            const filters = { ...state.filters, [action.filter]: !state.filters[action.filter] }
            return {
                ...state,
                filters,
                view: reduceView(state.view, viewActions.updateFilter(state.list.data, log => filters[log.level])),
            }

        case ADD:
            const item = {
                id: state.logId,
                message: action.message,
                level: action.level,
            }
            return {
                ...state,
                logId: state.logId + 1,
                list: reduceList(state.list, listActions.add(item)),
                view: reduceView(state.view, viewActions.add(item, log => state.filters[log.level])),
            }

        case RECEIVE:
            return {
                ...state,
                list: reduceList(state.list, listActions.receive(action.events)),
                view: reduceView(state.view, viewActions.receive(action.events, log => state.filters[log.level])),
            }

        default:
            return state
    }
}

export function toggleFilter(filter) {
    return { type: TOGGLE_FILTER, filter }
}

export function toggleVisibility() {
    return { type: TOGGLE_VISIBILITY }
}

export function add(message, level = 'web') {
    return { type: ADD, message, level }
}