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

import * as msgQueueActions from './msgQueue'
import * as websocketActions from './websocket'

export const MSG_TYPE = 'UPDATE_FLOWS'
export const DATA_URL = '/flows'

export const ADD = 'FLOWS_ADD'
export const UPDATE = 'FLOWS_UPDATE'
export const REMOVE = 'FLOWS_REMOVE'
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'
export const SELECT = 'FLOWS_SELECT'


const defaultState = {
    selected: [],
    ...reduceList(undefined, {}),
}

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

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

        case UPDATE:
            return {
                ...state,
                ...reduceList(state, listActions.update(action.item)),
            }

        case REMOVE:
            return {
                ...state,
                ...reduceList(state, listActions.remove(action.id)),
            }

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

        case SELECT:
            return {
                ...state,
                selected: action.flowIds
            }

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

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

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

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

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

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

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

/**
 * @public
 */
export function update(flow, data) {
    return dispatch => fetchApi.put(`/flows/${flow.id}`, data)
}

export function uploadContent(flow, file, type) {
    const body = new FormData()
    file = new Blob([file], {type: 'plain/text'})
    body.append('file', file)
    return dispatch => fetchApi(`/flows/${flow.id}/${type}/content`, {method: 'post',  body} )
}


/**
 * @public
 */
export function clear() {
    return dispatch => fetchApi('/clear', { method: 'POST' })
}

/**
 * @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)
    return dispatch => fetchApi('/flows/dump', { method: 'post', body })
}


export function select(id) {
    return {
        type: SELECT,
        flowIds: id ? [id] : []
    }
}


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

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

        case websocketActions.CMD_UPDATE:
            return updateFlow(msg.data)

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

        case websocketActions.CMD_RESET:
            return fetchFlows()

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

/**
 * @public websocket
 */
export function fetchFlows() {
    return msgQueueActions.fetchData(MSG_TYPE)
}

/**
 * @public msgQueue
 */
export function receiveData(list) {
    return { type: RECEIVE, list }
}

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

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

/**
 * @private
 */
export function removeFlow(id) {
    return (dispatch) => {
        dispatch(select())
        dispatch({ type: REMOVE, id })
    }
}