WF- get workflow after restoring workflow
[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     getVersionsAction,
23     GET_OVERVIEW,
24     UPDATE_WORKFLOW,
25     ARCHIVE_WORKFLOW,
26     RESTORE_WORKFLOW
27 } from 'features/workflow/overview/overviewConstansts';
28 import { setWorkflowAction } from 'features/workflow/workflowConstants';
29 import { notificationActions } from 'shared/notifications/notificationsActions';
30 import { fetchWorkflow } from 'features/catalog/catalogActions';
31 import { WORKFLOW_STATUS } from 'features/workflow/workflowConstants';
32
33 export function* getOverview({ payload }) {
34     try {
35         const versions = yield call(overviewApi.getVersions, payload);
36         yield put(versionListFetchAction(versions));
37         const workflow = yield call(overviewApi.getWorkflow, payload);
38         yield put(setWorkflowAction(workflow));
39     } catch (error) {
40         yield put(genericNetworkErrorAction(error));
41     }
42 }
43
44 export function* updateWorkflow(action) {
45     try {
46         yield call(overviewApi.updateWorkflow, action.payload);
47         yield put(
48             notificationActions.showSuccess({
49                 title: 'Update Workflow',
50                 message: 'Successfully updated'
51             })
52         );
53     } catch (e) {
54         yield put(genericNetworkErrorAction(e));
55     }
56 }
57
58 export function* archiveRestoreWorkflow(action) {
59     try {
60         const { ...data } = action.payload;
61         yield call(overviewApi.archiveRestoreWorkflow, data);
62         const {
63             catalog: { sort },
64             searchNameFilter = ''
65         } = yield select();
66
67         yield put(
68             fetchWorkflow({
69                 sort,
70                 searchNameFilter,
71                 status: WORKFLOW_STATUS.ACTIVE
72             })
73         );
74     } catch (e) {
75         yield put(genericNetworkErrorAction(e));
76     }
77 }
78
79 export function* restoreWorkflow(action) {
80     const { id } = action.payload;
81     yield archiveRestoreWorkflow(action);
82     yield put(getVersionsAction(id));
83 }
84
85 export function* archiveWorkflow(action) {
86     const { history } = action.payload;
87     yield archiveRestoreWorkflow(action);
88     history.push('/');
89 }
90
91 export function* watchOverview() {
92     yield takeEvery(GET_OVERVIEW, getOverview);
93     yield takeEvery(UPDATE_WORKFLOW, updateWorkflow);
94     yield takeEvery(ARCHIVE_WORKFLOW, archiveWorkflow);
95     yield takeEvery(RESTORE_WORKFLOW, restoreWorkflow);
96 }