blob: 230c15e37113fdc5343ae0d85c11ba9cfec33176 [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
talig8e9c0652017-12-20 14:30:43 +02003 *
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 *
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03008 * http://www.apache.org/licenses/LICENSE-2.0
talig8e9c0652017-12-20 14:30:43 +02009 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030012 * 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.
talig8e9c0652017-12-20 14:30:43 +020015 */
16import React from 'react';
17import Form from 'nfvo-components/input/validation/Form.jsx';
18import Select from 'nfvo-components/input/SelectInput.jsx';
Arielka51eac02019-07-07 12:56:11 +030019import { SVGIcon } from 'onap-ui-react';
talig8e9c0652017-12-20 14:30:43 +020020import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger.js';
21import Tooltip from 'react-bootstrap/lib/Tooltip.js';
22import i18n from 'nfvo-utils/i18n/i18n.js';
23
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020024import { permissionTypes, changeOwnerMessage } from './PermissionsConstants.js';
talig8e9c0652017-12-20 14:30:43 +020025
26export const askForRightsMsg = () => {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020027 return (
28 <div>
29 <p>{i18n('Send a Contributor rights reguest to Owner')}</p>
30 </div>
31 );
talig8e9c0652017-12-20 14:30:43 +020032};
33
talig8e9c0652017-12-20 14:30:43 +020034class Permissions extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020035 constructor(props) {
36 super(props);
37 this.state = {
38 itemUsers: props.itemUsers,
39 newOwnerId: '',
40 showChangeOwner: false
41 };
42 }
talig8e9c0652017-12-20 14:30:43 +020043
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020044 buildUserOptions() {
45 let { users, owner } = this.props;
46 return users.filter(user => user.userId !== owner.userId).map(item => {
47 return { label: item.fullName, value: item.userId };
48 });
49 }
talig8e9c0652017-12-20 14:30:43 +020050
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020051 render() {
52 let { onCancel, owner } = this.props;
53 let { newOwnerId } = this.state;
54 return (
55 <div className="manage-permissions-page">
56 <Form
57 hasButtons={true}
58 onSubmit={() => this.onsaveItemUsers()}
59 onReset={() => onCancel()}
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030060 labledButtons={true}
61 btnClassName="sdc-modal__footer">
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020062 <div className="manage-permissions-title">
63 {i18n('Owner')}
64 </div>
65 <div className="owner-details">
66 <span>{owner.fullName}</span>
67 <span
68 className="change-owner"
69 onClick={() =>
70 this.setState({
71 showChangeOwner: !this.state.showChangeOwner
72 })
73 }>
74 {i18n('Change Owner')}
75 </span>
76 </div>
77 {this.state.showChangeOwner && (
78 <div className="change-owner-wrapper">
79 <div className="change-owner-title">
80 <span
81 className="manage-permissions-title"
82 data-test-id="change-owner">
83 {i18n('Change Owner')}
84 </span>
85 <OverlayTrigger
86 placement="right"
87 trigger="click"
88 overlay={
89 <Tooltip
90 id="manage-permissions-owner-tooltip"
91 className="manage-permissions-owner-tooltip">
92 {i18n(changeOwnerMessage)}
93 </Tooltip>
94 }>
95 <SVGIcon name="questionMark" />
96 </OverlayTrigger>
97 </div>
98 <Select
99 data-test-id="selected-owner"
100 value={newOwnerId}
101 onChange={item =>
102 this.setState({
103 newOwnerId: item ? item.value : ''
104 })
105 }
106 options={this.buildUserOptions()}
107 />
108 </div>
109 )}
110 <div className="manage-permissions-title">
111 {i18n('Contributors')}
112 </div>
113 <Select
114 data-test-id="selected-contributors"
115 value={this.state.itemUsers.map(item => item.userId)}
116 className="options-input contributors-select"
117 clearable={false}
118 onMultiSelectChanged={value => {
119 this.onChangeItemUsers({ itemUsers: value });
120 }}
121 options={this.buildUserOptions()}
122 multi
123 />
124 </Form>
125 </div>
126 );
127 }
talig8e9c0652017-12-20 14:30:43 +0200128
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200129 onChangeItemUsers({ itemUsers }) {
130 this.setState({
131 itemUsers: itemUsers.map(contributor => {
132 let contributorFromProps = this.props.itemUsers.find(
133 user => user.userId === contributor.userId
134 );
135 return {
136 userId: contributor.value,
137 fullName: contributor.label,
138 permission: contributorFromProps
139 ? contributorFromProps.permission
140 : permissionTypes.CONTRIBUTOR
141 };
142 })
143 });
144 }
talig8e9c0652017-12-20 14:30:43 +0200145
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200146 onsaveItemUsers() {
147 let { itemUsers: newUsers, newOwnerId } = this.state;
148 let { itemUsers: oldUsers, onSubmit, itemId, users } = this.props;
149 let addedUsersIds = newUsers
150 .filter(
151 newUser =>
152 !oldUsers
153 .map(oldUser => oldUser.userId)
154 .includes(newUser.userId)
155 )
156 .map(user => user.userId);
157 let removedUsersIds = oldUsers
158 .filter(
159 oldUser =>
160 !newUsers
161 .map(newUser => newUser.userId)
162 .includes(oldUser.userId)
163 )
164 .map(user => user.userId);
165 onSubmit({
166 itemId,
167 addedUsersIds,
168 removedUsersIds,
169 allUsers: users,
170 newOwnerId
171 });
172 }
talig8e9c0652017-12-20 14:30:43 +0200173}
174
175export default Permissions;