blob: 5fe0892e59801e66d54ef5f19625fd46eeb9e51d [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
svishnevf6784902018-02-12 09:10:35 +02002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import React from 'react';
talig8e9c0652017-12-20 14:30:43 +020017import PropTypes from 'prop-types';
Michael Landoefa037d2017-02-19 12:57:33 +020018import i18n from 'nfvo-utils/i18n/i18n.js';
19
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020020import { actionsEnum } from './VersionControllerConstants.js';
talig8e9c0652017-12-20 14:30:43 +020021import ActionButtons from './components/ActionButtons.jsx';
22import NotificationsView from 'sdc-app/onboarding/userNotifications/NotificationsView.jsx';
Michael Landoefa037d2017-02-19 12:57:33 +020023
Michael Landoefa037d2017-02-19 12:57:33 +020024class VersionController extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025 static propTypes = {
26 version: PropTypes.object,
27 viewableVersions: PropTypes.array,
28 onVersionSwitching: PropTypes.func,
29 callVCAction: PropTypes.func,
30 onSave: PropTypes.func,
31 onClose: PropTypes.func,
32 isFormDataValid: PropTypes.bool,
33 onOpenCommentCommitModal: PropTypes.func,
34 isReadOnlyMode: PropTypes.bool
35 };
Michael Landoefa037d2017-02-19 12:57:33 +020036
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020037 state = {
38 showPermissions: false,
39 showRevisions: false
40 };
talig8e9c0652017-12-20 14:30:43 +020041
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020042 render() {
43 let {
44 version = {},
45 viewableVersions = [],
46 onVersionSwitching,
47 onMoreVersionsClick,
48 callVCAction,
49 onSave,
50 isReadOnlyMode,
51 itemPermission,
52 isFormDataValid,
53 onClose,
54 onManagePermissions,
55 permissions = {},
56 userInfo,
57 usersList,
58 itemName,
59 onOpenCommentCommitModal,
60 onOpenRevisionsModal,
61 isManual,
62 candidateInProcess,
63 isArchived
64 } = this.props;
65 return (
66 <div className="version-controller-bar">
svishnev04783c42018-04-17 11:32:22 +030067 <div className="vc-container">
68 <div
69 className={`version-status-container ${
70 candidateInProcess ? 'disabled' : ''
71 }`}>
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020072 <VersionSelector
73 viewableVersions={viewableVersions}
74 version={version}
75 onVersionSwitching={onVersionSwitching}
76 onMoreVersionsClick={() =>
77 onMoreVersionsClick({
78 itemName,
79 users: usersList
80 })
81 }
82 />
83 {isArchived && (
84 <div className="depricated-item-status">
85 {i18n('Archived')}
86 </div>
87 )}
88 </div>
89 <div className="save-submit-cancel-container">
90 <ActionButtons
91 onSubmit={
92 callVCAction
93 ? () => this.submit(callVCAction, version)
94 : undefined
95 }
96 onRevert={
97 callVCAction
98 ? () => this.revert(callVCAction, version)
99 : undefined
100 }
101 onOpenRevisionsModal={onOpenRevisionsModal}
102 onSave={onSave ? () => onSave() : undefined}
103 permissions={permissions}
104 userInfo={userInfo}
105 onManagePermissions={onManagePermissions}
106 showPermissions={this.state.showPermissions}
107 onClosePermissions={() =>
108 this.setState({ showPermissions: false })
109 }
110 onClickPermissions={() => this.onClickPermissions()}
111 onSync={
112 callVCAction
113 ? () => this.sync(callVCAction, version)
114 : undefined
115 }
116 onOpenCommentCommitModal={onOpenCommentCommitModal}
117 onCommit={
118 callVCAction
119 ? comment =>
120 this.commit(
121 callVCAction,
122 version,
123 comment
124 )
125 : undefined
126 }
127 isFormDataValid={isFormDataValid}
128 itemPermissions={itemPermission}
129 isArchived={isArchived}
130 isReadOnlyMode={
131 isReadOnlyMode || candidateInProcess
132 }
133 isManual={isManual}
134 />
135 <div className="vc-separator" />
136 <NotificationsView />
137 {onClose && (
138 <div
139 className="vc-nav-item-close"
140 onClick={() => onClose()}
141 data-test-id="vc-cancel-btn">
142 {' '}
143 X
144 </div>
145 )}
146 </div>
147 </div>
148 </div>
149 );
150 }
Michael Landoefa037d2017-02-19 12:57:33 +0200151
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200152 onClickPermissions() {
153 let { onOpenPermissions, usersList } = this.props;
154 let { showPermissions } = this.state;
155 let promise = showPermissions
156 ? Promise.resolve()
157 : onOpenPermissions({ users: usersList });
158 promise.then(() =>
159 this.setState({ showPermissions: !showPermissions })
160 );
161 }
Michael Landoefa037d2017-02-19 12:57:33 +0200162
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 submit(callVCAction, version) {
164 const action = actionsEnum.SUBMIT;
165 callVCAction(action, version);
166 }
talig8e9c0652017-12-20 14:30:43 +0200167
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200168 revert(callVCAction, version) {
169 const action = actionsEnum.REVERT;
170 callVCAction(action, version);
171 }
talig8e9c0652017-12-20 14:30:43 +0200172
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200173 sync(callVCAction, version) {
174 const action = actionsEnum.SYNC;
175 callVCAction(action, version);
176 }
Michael Landoefa037d2017-02-19 12:57:33 +0200177
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200178 commit(callVCAction, version, comment) {
179 const action = actionsEnum.COMMIT;
180 callVCAction(action, version, comment);
181 }
AviZi280f8012017-06-09 02:39:56 +0300182
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200183 permissions() {}
AviZi280f8012017-06-09 02:39:56 +0300184}
185
186function VersionSelector(props) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200187 let {
188 version = {},
189 onMoreVersionsClick,
190 viewableVersions = [],
191 onVersionSwitching
192 } = props;
193 const includedVersions = viewableVersions.filter(ver => {
194 return ver.id === version.id;
195 });
196 return (
197 <div className="version-section-wrapper">
198 <select
199 className="version-selector"
200 onChange={ev =>
201 onVersionSwitching &&
202 onVersionSwitching(
203 viewableVersions.find(
204 version => version.id === ev.target.value
205 )
206 )
207 }
208 value={version.id}
209 data-test-id="vc-versions-select-box">
210 {viewableVersions &&
211 viewableVersions.map(viewVersion => {
212 return (
213 <option
214 key={viewVersion.id}
215 value={viewVersion.id}
216 data-test-id="vc-version-option">{`V ${
217 viewVersion.name
218 } ${viewVersion.status}`}</option>
219 );
220 })}
221 {!includedVersions.length && (
222 <option
223 key={version.id}
224 value={version.id}
225 data-test-id="vc-selected-version-option">{`V ${
226 version.name
227 } ${version.status}`}</option>
228 )}
229 </select>
230 <span
231 onClick={onMoreVersionsClick}
232 className="version-selector-more-versions"
233 data-test-id="vc-versions-page-link">
234 {i18n('Versions Page')}
235 </span>
236 </div>
237 );
Michael Landoefa037d2017-02-19 12:57:33 +0200238}
239
240export default VersionController;