Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2018 European Support Limited |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 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 | * |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 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, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 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. |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 15 | */ |
| 16 | import React from 'react'; |
| 17 | import Form from 'nfvo-components/input/validation/Form.jsx'; |
| 18 | import Select from 'nfvo-components/input/SelectInput.jsx'; |
| 19 | import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js'; |
| 20 | import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger.js'; |
| 21 | import Tooltip from 'react-bootstrap/lib/Tooltip.js'; |
| 22 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 23 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 24 | import { permissionTypes, changeOwnerMessage } from './PermissionsConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 25 | |
| 26 | export const askForRightsMsg = () => { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 27 | return ( |
| 28 | <div> |
| 29 | <p>{i18n('Send a Contributor rights reguest to Owner')}</p> |
| 30 | </div> |
| 31 | ); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 34 | class Permissions extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | constructor(props) { |
| 36 | super(props); |
| 37 | this.state = { |
| 38 | itemUsers: props.itemUsers, |
| 39 | newOwnerId: '', |
| 40 | showChangeOwner: false |
| 41 | }; |
| 42 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 43 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 44 | 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 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 50 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 51 | 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 Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 60 | labledButtons={true} |
| 61 | btnClassName="sdc-modal__footer"> |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 62 | <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 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 128 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 129 | 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 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 145 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 146 | 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 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | export default Permissions; |