aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/eventLog.js
blob: 8f9ec34d7aedcd06ce03b3cb0abeda4a9a711f2f (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
import reduceStore from "./utils/store"
import * as storeActions from "./utils/store"

export const ADD               = 'EVENTS_ADD'
export const RECEIVE           = 'EVENTS_RECEIVE'
export const TOGGLE_VISIBILITY = 'EVENTS_TOGGLE_VISIBILITY'
export const TOGGLE_FILTER     = 'EVENTS_TOGGLE_FILTER'

const defaultState = {
    visible: false,
    filters: { debug: false, info: true, web: true, warn: true, error: true },
    ...reduceStore(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,
                ...reduceStore(state, storeActions.setFilter(log => filters[log.level]))
            }

        case ADD:
        case RECEIVE:
            return {
                ...state,
                ...reduceStore(state, storeActions[action.cmd](action.data, 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') {
    let data = {
        id: Math.random().toString(),
        message,
        level,
    }
    return {
        type: ADD,
        cmd: "add",
        data
    }
}