Add collaboration feature
[sdc.git] / openecomp-ui / src / nfvo-components / panel / versionController / VersionController.jsx
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import React from 'react';
17 import PropTypes from 'prop-types';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19
20 import {actionsEnum} from './VersionControllerConstants.js';
21 import ActionButtons from './components/ActionButtons.jsx';
22 import NotificationsView from 'sdc-app/onboarding/userNotifications/NotificationsView.jsx';
23
24
25 class VersionController extends React.Component {
26
27         static propTypes = {
28                 version: PropTypes.object,
29                 viewableVersions: PropTypes.array,
30                 onVersionSwitching: PropTypes.func,
31                 callVCAction: PropTypes.func,
32                 onSave: PropTypes.func,
33                 onClose: PropTypes.func,
34                 isFormDataValid: PropTypes.bool,
35                 onOpenCommentCommitModal: PropTypes.func,
36                 isReadOnlyMode: PropTypes.bool
37         };
38
39         state = {
40                 showPermissions: false,
41                 showRevisions: false
42         };
43
44         render() {
45                 let {version = {},  viewableVersions = [], onVersionSwitching, onMoreVersionsClick, callVCAction, onSave, isReadOnlyMode, itemPermission,
46                                 isFormDataValid, onClose, onManagePermissions, permissions = {},  userInfo, usersList, itemName, onOpenCommentCommitModal, onOpenRevisionsModal, isManual} = this.props;
47                 return (
48                         <div className='version-controller-bar'>
49                                 <div className='vc-container'>
50                                         <div className='version-status-container'>
51                                                 <VersionSelector
52                                                         viewableVersions={viewableVersions}
53                                                         version={version}
54                                                         onVersionSwitching={onVersionSwitching}
55                                                         onMoreVersionsClick={() => onMoreVersionsClick({itemName, users: usersList})}/>
56                                         </div>
57                                         <div className='save-submit-cancel-container'>
58                                                 <ActionButtons onSubmit={callVCAction ? () => this.submit(callVCAction, version) : undefined}
59                                                         onRevert={callVCAction ? () => this.revert(callVCAction, version) : undefined}
60                                                         onOpenRevisionsModal={onOpenRevisionsModal}
61                                                         onSave={onSave ? () => onSave() : undefined}
62                                                         permissions={permissions}
63                                                         userInfo={userInfo}
64                                                         onManagePermissions={onManagePermissions}
65                                                         showPermissions={this.state.showPermissions}
66                                                         onClosePermissions={()=>this.setState({showPermissions: false})}
67                                                         onClickPermissions={() => this.onClickPermissions()}
68                                                         onSync={callVCAction ? () => this.sync(callVCAction, version) : undefined}
69                                                         onOpenCommentCommitModal={onOpenCommentCommitModal}
70                                                         onCommit={callVCAction ? (comment) => this.commit(callVCAction, version, comment) : undefined}
71                                                         isFormDataValid={isFormDataValid}
72                                                         itemPermissions={itemPermission}
73                                                         isReadOnlyMode={isReadOnlyMode}
74                                                         isManual={isManual} />
75                                                 <div className='vc-separator'></div>
76                                                 <NotificationsView />
77                                                 {onClose && <div className='vc-nav-item-close' onClick={() => onClose()} data-test-id='vc-cancel-btn'> X</div>}
78                                         </div>
79                                 </div>
80                         </div>
81                 );
82         }
83
84         onClickPermissions() {
85                 let {onOpenPermissions, usersList} = this.props;
86                 let {showPermissions} = this.state;
87                 let promise = showPermissions ? Promise.resolve() : onOpenPermissions({users: usersList});
88                 promise.then(() => this.setState({showPermissions: !showPermissions}));
89         }
90
91
92         submit(callVCAction, version) {
93                 const action = actionsEnum.SUBMIT;
94                 callVCAction(action, version);
95         }
96
97         revert(callVCAction, version) {
98                 const action = actionsEnum.REVERT;
99                 callVCAction(action, version);
100         }
101
102         sync(callVCAction, version) {
103                 const action = actionsEnum.SYNC;
104                 callVCAction(action, version);
105         }
106
107         commit(callVCAction, version, comment) {
108                 const action = actionsEnum.COMMIT;
109                 callVCAction(action, version, comment);
110         }
111
112         permissions() {
113
114         }
115 }
116
117 function VersionSelector(props) {
118         let {version = {}, onMoreVersionsClick, viewableVersions = [], onVersionSwitching} = props;
119         const includedVersions = viewableVersions.filter(ver => {return ver.id === version.id;});
120         return (<div className='version-section-wrapper'>
121                 <select className='version-selector'
122                         onChange={ev => onVersionSwitching && onVersionSwitching(viewableVersions.find(version => version.id === ev.target.value))}
123                         value={version.id}
124                         data-test-id='vc-versions-select-box'>
125                                 {viewableVersions && viewableVersions.map(viewVersion => {
126                                         return (
127                                                 <option key={viewVersion.id} value={viewVersion.id} data-test-id='vc-version-option'>{`V ${viewVersion.name} ${viewVersion.status}`}</option>
128                                         );
129                                 })
130                                 }
131                                 {!includedVersions.length &&
132                                 <option key={version.id} value={version.id} data-test-id='vc-selected-version-option'>{`V ${version.name} ${version.status}`}</option>}
133                 </select>
134                 <span onClick={onMoreVersionsClick} className='version-selector-more-versions' data-test-id='vc-versions-page-link'>{i18n('Versions Page')}</span>
135         </div>);
136 }
137
138 export default VersionController;