aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/connection.js
blob: 151277fbd18ef58971cc91f8e2736b89bfc2d69a (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
export const ConnectionState = {
    INIT: Symbol("init"),
    FETCHING: Symbol("fetching"), // WebSocket is established, but still fetching resources.
    ESTABLISHED: Symbol("established"),
    ERROR: Symbol("error"),
    OFFLINE: Symbol("offline"), // indicates that there is no live (websocket) backend.
}

const defaultState = {
    state: ConnectionState.INIT,
    message: null,
}

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

        case ConnectionState.ESTABLISHED:
        case ConnectionState.FETCHING:
        case ConnectionState.ERROR:
        case ConnectionState.OFFLINE:
            return {
                state: action.type,
                message: action.message
            }

        default:
            return state
    }
}

export function startFetching() {
    return { type: ConnectionState.FETCHING }
}

export function connectionEstablished() {
    return { type: ConnectionState.ESTABLISHED }
}

export function connectionError(message) {
    return { type: ConnectionState.ERROR, message }
}
export function setOffline() {
    return { type: ConnectionState.OFFLINE }
}