aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/eventLog.js
blob: 2040711cdb6de70c914591c12baf6922e3ee483f (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
import getList, {ADD} from "./list"
const TOGGLE_FILTER = 'TOGGLE_EVENTLOG_FILTER'
const TOGGLE_VISIBILITY = 'TOGGLE_EVENTLOG_VISIBILITY'
const UPDATE_LIST = "UPDATE_EVENTLOG"


const defaultState = {
    visible: false,
    filter: {
        "debug": false,
        "info": true,
        "web": true
    },
    events: getList(),
    filteredEvents: [],
}

export default function reducer(state = defaultState, action) {
    switch (action.type) {
        case TOGGLE_FILTER:
            const filter = {
                ...state.filter,
                [action.filter]: !state.filter[action.filter]
            }
            return {
                ...state,
                filter,
                filteredEvents: state.events.list.filter(x => filter[x.level])
            }
        case TOGGLE_VISIBILITY:
            return {
                ...state,
                visible: !state.visible
            }
        case UPDATE_LIST:
            const events = getList(state.events, action)
            return {
                ...state,
                events,
                filteredEvents: events.list.filter(x => state.filter[x.level])
            }
        default:
            return state
    }
}


export function toggleEventLogFilter(filter) {
    return {type: TOGGLE_FILTER, filter}
}
export function toggleEventLogVisibility() {
    return {type: TOGGLE_VISIBILITY}
}
let id = 0;
export function addLogEntry(message, level = "web") {
    return {
        type: UPDATE_LIST,
        cmd: ADD,
        data: {message, level, id: `log-${id++}`}
    }
}