f2b4cf17a95c8064dea4a79cc57ee4a1140a3aeb
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / workflow / overview / overviewSagas.js
1 /*
2 * Copyright © 2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 import { call, takeEvery, put, select } from 'redux-saga/effects';
17
18 import { genericNetworkErrorAction } from 'wfapp/appConstants';
19 import overviewApi from 'features/workflow/overview/overviewApi';
20 import {
21     versionListFetchAction,
22     GET_OVERVIEW,
23     UPDATE_WORKFLOW,
24     ARCHIVE_WORKFLOW,
25     RESTORE_WORKFLOW
26 } from 'features/workflow/overview/overviewConstansts';
27 import { setWorkflowAction } from 'features/workflow/workflowConstants';
28 import { notificationActions } from 'shared/notifications/notificationsActions';
29 import { fetchWorkflow } from 'features/catalog/catalogActions';
30 import { WORKFLOW_STATUS } from 'features/workflow/workflowConstants';
31
32 export function* getOverview(action) {
33     try {
34         const versions = yield call(overviewApi.getVersions, action.payload);
35         yield put(versionListFetchAction(versions));
36         const workflow = yield call(overviewApi.getWorkflow, action.payload);
37         yield put(setWorkflowAction(workflow));
38     } catch (error) {
39         yield put(genericNetworkErrorAction(error));
40     }
41 }
42
43 export function* updateWorkflow(action) {
44     try {
45         yield call(overviewApi.updateWorkflow, action.payload);
46         yield put(
47             notificationActions.showSuccess({
48                 title: 'Update Workflow',
49                 message: 'Successfully updated'
50             })
51         );
52     } catch (e) {
53         yield put(genericNetworkErrorAction(e));
54     }
55 }
56
57 export function* archiveRestoreWorkflow(action) {
58     try {
59         const { history, ...data } = action.payload;
60         yield call(overviewApi.archiveRestoreWorkflow, data);
61         const {
62             catalog: { sort },
63             searchNameFilter = ''
64         } = yield select();
65
66         yield put(
67             fetchWorkflow({
68                 sort,
69                 searchNameFilter,
70                 status: WORKFLOW_STATUS.ACTIVE
71             })
72         );
73         history.push('/');
74     } catch (e) {
75         yield put(genericNetworkErrorAction(e));
76     }
77 }
78
79 export function* watchOverview() {
80     yield takeEvery(GET_OVERVIEW, getOverview);
81     yield takeEvery(UPDATE_WORKFLOW, updateWorkflow);
82     yield takeEvery(ARCHIVE_WORKFLOW, archiveRestoreWorkflow);
83     yield takeEvery(RESTORE_WORKFLOW, archiveRestoreWorkflow);
84 }