/
TaskActions.js
49 lines (40 loc) · 1.55 KB
/
TaskActions.js
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
import { addToast } from 'foremanReact/components/ToastsList';
import { propsToSnakeCase } from 'foremanReact/common/helpers';
import { get } from 'foremanReact/redux/API';
import { stopInterval, withInterval } from 'foremanReact/redux/middlewares/IntervalMiddleware';
import { foremanTasksApi } from '../../services/api';
import { bulkSearchKey, pollTaskKey, taskFinishedToast } from './helpers';
export const toastTaskFinished = task => async (dispatch) => {
dispatch(addToast(taskFinishedToast(task)));
};
const taskBulkSearchParams = params => ({
search: Object.entries(propsToSnakeCase(params))
.map((item) => {
if (item[0] === 'action') {
return `${item[0]}~${item[1]}`;
}
return `${item[0]}=${item[1]}`;
})
.join(' and '),
});
const getTasks = (key, params = {}) => get({
key,
url: `${foremanTasksApi.baseApiPath}/tasks`,
params: taskBulkSearchParams(params),
});
export const startPollingTasks = (key, taskSearchParams = {}) =>
withInterval(getTasks(bulkSearchKey(key), taskSearchParams));
export const stopPollingTasks = key => stopInterval(bulkSearchKey(key));
const getTask = (key, task, handleSuccess) => get({
key,
url: `${foremanTasksApi.baseApiPath}/tasks/${task.id}`,
handleSuccess,
});
export const startPollingTask = (key, task, handleSuccess) =>
withInterval(getTask(pollTaskKey(key), task, handleSuccess));
export const stopPollingTask = key => stopInterval(pollTaskKey(key));
export const clearPollTaskData = key => ({
type: `${pollTaskKey(key)}_UPDATE`,
key: pollTaskKey(key),
payload: {},
});