aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/flows.js
blob: 4b4b2a88be4ebe098a256528b91c6fc0dd0e5283 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { fetchApi as fetch } from '../utils'
import { CMD_RESET as WS_CMD_RESET } from './websocket'
import reduceList, * as listActions from './utils/list'

export const WS_MSG_TYPE ='UPDATE_FLOWS'

export const UPDATE_FILTER = 'FLOWS_UPDATE_FLOW_FILTER'
export const UPDATE_HIGHLIGHT = 'FLOWS_UPDATE_FLOW_HIGHLIGHT'
export const UPDATE_SORT = 'FLOWS_UPDATE_FLOW_SORT'
export const WS_MSG = 'FLOWS_WS_MSG'
export const SELECT_FLOW = 'FLOWS_SELECT_FLOW'
export const REQUEST_ACTION = 'FLOWS_REQUEST_ACTION'
export const REQUEST = 'FLOWS_REQUEST'
export const RECEIVE = 'FLOWS_RECEIVE'
export const FETCH_ERROR = 'FLOWS_FETCH_ERROR'

const defaultState = {
    selected: [],
    sorter: {},
    filter: null,
    highlight: null,
    list: reduceList(undefined, { type: Symbol('FLOWS_INIT_LIST') }),
}

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

        case UPDATE_FILTER:
            return {
                ...state,
                filter: action.filter,
                list: reduceList(state.list, listActions.updateFilter(makeFilterFun(action.filter))),
            }

        case UPDATE_HIGHLIGHT:
            return {
                ...state,
                highlight: action.highlight,
            }

        case UPDATE_SORTER:
            return {
                ...state,
                sorter: { column: action.column, desc: action.desc },
                list: reduceList(state.list, listActions.updateSorter(makeSortFun(action.sortKeyFun, action.desc))),
            }

        case SELECT_FLOW:
            return {
                ...state,
                selected: [action.id],
            }

        case WS_MSG:
            return {
                ...state,
                list: reduceList(state.list, listActions.handleWsMsg(action.msg)),
            }

        case REQUEST:
            return {
                ...state,
                list: reduceList(state.list, listActions.request())
            }

        case RECEIVE:
            return {
                ...state,
                list: reduceList(state.list, listActions.receive(action.list))
            }

        default:
            return state
    }
}

function makeFilterFun(filter) {
    return filter ? Filt.parse(filter) : () => true
}

function makeSortFun(sortKeyFun, desc) {
    return (a, b) => {
        const ka = sortKeyFun(a)
        const kb = sortKeyFun(b)
        if (ka > kb) {
            return desc ? -1 : 1
        }
        if (ka < kb) {
            return desc ? 1 : -1
        }
        return 0
    }
}

/**
 * @public
 */
export function updateFilter(filter) {
    return { type: UPDATE_FILTER, filter }
}

/**
 * @public
 */
export function updateHighlight(highlight) {
    return { type: UPDATE_HIGHLIGHT, highlight }
}

/**
 * @public
 */
export  function updateSorter(column, desc, sortKeyFun) {
    return { type: UPDATE_SORTER, column, desc, sortKeyFun }
}

/**
 * @public
 */
export function selectFlow(id) {
    return (dispatch, getState) => {
        dispatch({ type: SELECT_FLOW, currentSelection: getState().flows.selected[0], id })
    }
}

/**
 * @public
 */
export function accept(flow) {
    fetch(`/flows/${flow.id}/accept`, { method: 'POST' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function acceptAll() {
    fetch('/flows/accept', { method: 'POST' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function delete(flow) {
    fetch(`/flows/${flow.id}`, { method: 'DELETE' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function duplicate(flow) {
    fetch(`/flows/${flow.id}/duplicate`, { method: 'POST' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function replay(flow) {
    fetch(`/flows/${flow.id}/replay`, { method: 'POST' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function revert(flow) {
    fetch(`/flows/${flow.id}/revert`, { method: 'POST' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function update(flow, body) {
    fetch(`/flows/${flow.id}`, { method: 'PUT', body })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function clear() {
    fetch('/clear', { method: 'POST' })
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function download() {
    window.location = '/flows/dump'
    return { type: REQUEST_ACTION }
}

/**
 * @public
 */
export function upload(file) {
    const body = new FormData()
    body.append('file', file)
    fetch('/flows/dump',  { method: 'post', body })
    return { type: REQUEST_ACTION }
}

/**
 * This action creater takes all WebSocket events
 *
 * @public websocket
 */
export function handleWsMsg(msg) {
    if (msg.cmd === WS_CMD_RESET) {
        return fetchData()
    }
    return { type: WS_MSG, msg }
}

/**
 * @public websocket
 */
export function fetchData() {
    return dispatch => {
        dispatch(request())

        return fetch('/flows')
            .then(res => res.json())
            .then(json => dispatch(receive(json.data)))
            .catch(error => dispatch(fetchError(error)))
    }
}

/**
 * @private
 */
export function request() {
    return { type: REQUEST }
}

/**
 * @private
 */
export function receive(list) {
    return { type: RECEIVE, list }
}

/**
 * @private
 */
export function fetchError(error) {
    return { type: FETCH_ERROR, error }
}