disable undo and empty name validation
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / version / versionSaga.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 { all, call, put, takeEvery, takeLatest } from 'redux-saga/effects';
17
18 import { genericNetworkErrorAction } from 'src/appConstants';
19 import {
20     setWorkflowVersionAction,
21     versionStateChangedAction,
22     FETCH_REQUESTED
23 } from 'features/version/versionConstants';
24 import { setInputsOutputs } from 'features/version/inputOutput/inputOutputActions';
25 import { SUBMIT_VERSION } from 'features/version/create/createVersionConstants';
26 import {
27     SAVE_ACTION,
28     CERTIFY_ACTION
29 } from 'features/version/versionController/versionControllerConstants';
30 import versionApi from 'features/version/versionApi';
31 import { notificationActions } from 'shared/notifications/notificationsActions';
32 import { versionState } from 'features/version/versionConstants';
33 import overviewApi from '../workflow/overview/overviewApi';
34 import { versionListFetchAction } from '../workflow/overview/overviewConstansts';
35 import { updateComposition } from 'features/version/composition/compositionActions';
36 import activitiesApi from 'features/activities/activitiesApi';
37 import { setActivitiesList } from 'features/activities/activitiesActions';
38
39 function* fetchVersion(action) {
40     try {
41         const data = yield call(versionApi.fetchVersion, action.payload);
42         const { inputs, outputs, ...rest } = data;
43         let composition = false;
44
45         if (rest.hasArtifact) {
46             composition = yield call(
47                 versionApi.fetchVersionArtifact,
48                 action.payload
49             );
50         }
51         const activitiesList = yield call(activitiesApi.fetchActivities);
52         yield all([
53             put(setWorkflowVersionAction(rest)),
54             put(setInputsOutputs({ inputs, outputs })),
55             put(updateComposition(composition)),
56             put(setActivitiesList(activitiesList.results))
57         ]);
58     } catch (error) {
59         yield put(genericNetworkErrorAction(error));
60     }
61 }
62
63 function* watchSubmitVersion(action) {
64     try {
65         const { workflowId, history } = action.payload;
66         const data = yield call(versionApi.createNewVersion, action.payload);
67         const versions = yield call(overviewApi.getVersions, workflowId);
68         yield put(versionListFetchAction(versions));
69         yield call(
70             history.push('/workflow/' + workflowId + '/version/' + data.id)
71         );
72     } catch (error) {
73         yield put(genericNetworkErrorAction(error));
74     }
75 }
76
77 function* watchUpdateVersion(action) {
78     try {
79         const {
80             workflowId,
81             params: { composition, ...versionData }
82         } = action.payload;
83         yield call(versionApi.updateVersion, {
84             workflowId,
85             params: versionData
86         });
87         if (composition) {
88             yield call(versionApi.updateVersionArtifact, {
89                 workflowId,
90                 versionId: versionData.id,
91                 payload: composition
92             });
93         }
94         yield put(
95             notificationActions.showSuccess({
96                 title: 'Update Workflow Version',
97                 message: 'Successfully updated'
98             })
99         );
100     } catch (error) {
101         yield put(genericNetworkErrorAction(error));
102     }
103 }
104
105 function* watchCertifyVersion(action) {
106     try {
107         const { workflowId, params } = action.payload;
108         yield call(versionApi.updateVersion, {
109             workflowId,
110             params: params
111         });
112         yield call(versionApi.certifyVersion, {
113             ...action.payload
114         });
115         yield put(
116             notificationActions.showSuccess({
117                 title: 'Certify Version',
118                 message: 'Successfully updated'
119             })
120         );
121         yield put(versionStateChangedAction({ state: versionState.CERTIFIED }));
122     } catch (error) {
123         yield put(genericNetworkErrorAction(error));
124     }
125 }
126
127 function* versionSaga() {
128     yield takeLatest(FETCH_REQUESTED, fetchVersion);
129     yield takeEvery(SUBMIT_VERSION, watchSubmitVersion);
130     yield takeEvery(SAVE_ACTION, watchUpdateVersion);
131     yield takeEvery(CERTIFY_ACTION, watchCertifyVersion);
132 }
133
134 export default versionSaga;