ef5e1168241d1cdcd57fcaac432083e89c1d53aa
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / version / versionController / VersionControllerView.jsx
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 React, { Component } from 'react';
17 import PropTypes from 'prop-types';
18
19 import ActionButtons from 'features/version/versionController/views/ActionButtons';
20 import OperationModeButtons from 'features/version/versionController/views/OperationModeButtons';
21 import VersionContainer from 'features/version/versionController/views/VersionsContainer';
22 import WorkflowTitle from 'features/version/versionController/views/WorkflowTitle';
23 import { PluginPubSub } from 'shared/pubsub/plugin-pubsub.ts';
24 import {
25     notificationType,
26     CATALOG_PATH
27 } from 'wfapp/pluginContext/pluginContextConstants';
28 export default class VersionControllerView extends Component {
29     static propTypes = {
30         location: PropTypes.object,
31         workflowName: PropTypes.string,
32         currentWorkflowVersion: PropTypes.object,
33         viewableVersions: PropTypes.arrayOf(Object),
34         getVersions: PropTypes.func,
35         versionsList: PropTypes.array,
36         history: PropTypes.object,
37         getOverview: PropTypes.func,
38         match: PropTypes.object,
39         savedParams: PropTypes.object,
40         saveParamsToServer: PropTypes.func,
41         workflowId: PropTypes.string,
42         certifyVersion: PropTypes.func,
43         changeVersion: PropTypes.func,
44         isCertifyDisable: PropTypes.bool,
45         hasErrors: PropTypes.bool,
46         isArchive: PropTypes.bool,
47         operationMode: PropTypes.bool,
48         pluginContext: PropTypes.object
49     };
50
51     constructor(props) {
52         super(props);
53     }
54
55     routeToOverview = () => {
56         const { history, match } = this.props;
57         const workflowId = match.params.workflowId;
58         history.push(`/workflows/workflow/${workflowId}/overview`);
59     };
60
61     sendSaveParamsToServer = () => {
62         const {
63             savedParams,
64             saveParamsToServer,
65             workflowId,
66             workflowName
67         } = this.props;
68         saveParamsToServer({ params: savedParams, workflowId, workflowName });
69     };
70     handleSendMsgToCatalog = () => {
71         const {
72             pluginContext: { eventsClientId, parentUrl },
73             workflowId,
74             isCertifyDisable
75         } = this.props;
76         const client = new PluginPubSub(eventsClientId, parentUrl);
77         client.notify(notificationType.CLOSE, {
78             isCompleted: isCertifyDisable,
79             workflowId,
80             path: CATALOG_PATH
81         });
82     };
83     certifyVersion = () => {
84         const {
85             certifyVersion,
86             workflowId,
87             currentWorkflowVersion,
88             savedParams,
89             workflowName
90         } = this.props;
91         certifyVersion({
92             workflowId,
93             workflowName,
94             versionId: currentWorkflowVersion.id,
95             params: savedParams
96         });
97     };
98
99     versionChangeCallback = versionId => {
100         const { changeVersion, workflowId } = this.props;
101         changeVersion({ versionId, workflowId });
102     };
103
104     undoClickCallback = () => {
105         const {
106             currentWorkflowVersion,
107             changeVersion,
108             workflowId
109         } = this.props;
110         changeVersion({ versionId: currentWorkflowVersion.id, workflowId });
111     };
112
113     render() {
114         const {
115             currentWorkflowVersion,
116             workflowName,
117             versionsList,
118             hasErrors,
119             isCertifyDisable,
120             isArchive,
121             operationMode
122         } = this.props;
123         const isReadonly = isCertifyDisable || hasErrors || isArchive;
124         return (
125             <div className="version-controller-bar">
126                 <WorkflowTitle workflowName={workflowName} />
127                 <div
128                     className={`vc-container ${
129                         operationMode ? 'vs-container-operation' : ''
130                     }`}>
131                     {!operationMode && (
132                         <VersionContainer
133                             currentWorkflowVersion={currentWorkflowVersion}
134                             viewableVersions={versionsList}
135                             onOverviewClick={this.routeToOverview}
136                             onVersionSelectChange={this.versionChangeCallback}
137                             isArchive={isArchive}
138                         />
139                     )}
140                     {operationMode && (
141                         <OperationModeButtons
142                             sendMsgToCatalog={this.handleSendMsgToCatalog}
143                             saveDisabled={isReadonly}
144                             onSaveClick={this.sendSaveParamsToServer}
145                             onCertifyClick={this.certifyVersion}
146                         />
147                     )}
148                     {!operationMode && (
149                         <ActionButtons
150                             saveDisabled={isReadonly}
151                             onSaveClick={this.sendSaveParamsToServer}
152                             certifyDisabled={isReadonly}
153                             onCertifyClick={this.certifyVersion}
154                             onUndoClick={this.undoClickCallback}
155                         />
156                     )}
157                 </div>
158             </div>
159         );
160     }
161 }
162
163 VersionControllerView.defaultProps = {
164     getVersions: () => {}
165 };