aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/flows.js
blob: f732f536ad50e40e1ee971abb7dea6b07b00a380 (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
import { fetchApi } from '../utils'
import reduceList, * as listActions from './utils/list'
import reduceViews, * as viewsActions from './views'
import * as websocketActions from './websocket'

export const WS_MSG_TYPE    = 'UPDATE_FLOWS'

export const ADD            = 'FLOWS_ADD'
export const UPDATE         = 'FLOWS_UPDATE'
export const REMOVE         = 'FLOWS_REMOVE'
export const REQUEST        = 'FLOWS_REQUEST'
export const RECEIVE        = 'FLOWS_RECEIVE'
export const REQUEST_ACTION = 'FLOWS_REQUEST_ACTION'
export const UNKNOWN_CMD    = 'FLOWS_UNKNOWN_CMD'
export const FETCH_ERROR    = 'FLOWS_FETCH_ERROR'

const defaultState = {
    list: undefined,
    views: undefined,
}

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

        case ADD:
            return {
                ...state,
                list: reduceList(state.list, listActions.add(action.item)),
                views: reduceViews(state.views, viewsActions.add(action.item)),
            }

        case UPDATE:
            return {
                ...state,
                list: reduceList(state.list, listActions.update(action.id, action.item)),
                views: reduceViews(state.views, viewsActions.update(action.id, action.item)),
            }

        case REMOVE:
            return {
                ...state,
                list: reduceList(state.list, listActions.remove(action.item.id)),
                views: reduceViews(state.views, viewsActions.remove(action.item.id)),
            }

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

        case RECEIVE:
            const list = reduceList(state.list, listActions.receive(action.list))
            return {
                ...state,
                list,
                views: reduceViews(state.views, viewsActions.receive(list)),
            }

        default:
            return {
                ...state,
                list: reduceList(state.list, action),
                views: reduceViews(state.views, action),
            }
    }
}

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

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

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

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

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

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

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

/**
 * @public
 */
export function clear() {
    fetchApi('/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)
    fetchApi('/flows/dump',  { method: 'post', body })
    return { type: REQUEST_ACTION }
}

/**
 * This action creater takes all WebSocket events
 *
 * @public websocket
 */
export function handleWsMsg(msg) {
    switch (msg.cmd) {

        case websocketActions.CMD_ADD:
            return add(msg.data)

        case websocketActions.CMD_UPDATE:
            return update(msg.data.id, msg.data)

        case websocketActions.CMD_REMOVE:
            return remove(msg.data.id)

        case websocketActions.CMD_RESET:
            return fetchData()

        default:
            return { type: UNKNOWN_CMD, msg }
    }
}

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

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

/**
 * @private
 */
export function add(item) {
    return { type: ADD, item }
}

/**
 * @private
 */
export function update(id, item) {
    return { type: UPDATE, id, item }
}

/**
 * @private
 */
export function remove(id) {
    return { type: REMOVE, id }
}

/**
 * @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 }
}