talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 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 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()} |
| 60 | labledButtons={true}> |
| 61 | <div className="manage-permissions-title"> |
| 62 | {i18n('Owner')} |
| 63 | </div> |
| 64 | <div className="owner-details"> |
| 65 | <span>{owner.fullName}</span> |
| 66 | <span |
| 67 | className="change-owner" |
| 68 | onClick={() => |
| 69 | this.setState({ |
| 70 | showChangeOwner: !this.state.showChangeOwner |
| 71 | }) |
| 72 | }> |
| 73 | {i18n('Change Owner')} |
| 74 | </span> |
| 75 | </div> |
| 76 | {this.state.showChangeOwner && ( |
| 77 | <div className="change-owner-wrapper"> |
| 78 | <div className="change-owner-title"> |
| 79 | <span |
| 80 | className="manage-permissions-title" |
| 81 | data-test-id="change-owner"> |
| 82 | {i18n('Change Owner')} |
| 83 | </span> |
| 84 | <OverlayTrigger |
| 85 | placement="right" |
| 86 | trigger="click" |
| 87 | overlay={ |
| 88 | <Tooltip |
| 89 | id="manage-permissions-owner-tooltip" |
| 90 | className="manage-permissions-owner-tooltip"> |
| 91 | {i18n(changeOwnerMessage)} |
| 92 | </Tooltip> |
| 93 | }> |
| 94 | <SVGIcon name="questionMark" /> |
| 95 | </OverlayTrigger> |
| 96 | </div> |
| 97 | <Select |
| 98 | data-test-id="selected-owner" |
| 99 | value={newOwnerId} |
| 100 | onChange={item => |
| 101 | this.setState({ |
| 102 | newOwnerId: item ? item.value : '' |
| 103 | }) |
| 104 | } |
| 105 | options={this.buildUserOptions()} |
| 106 | /> |
| 107 | </div> |
| 108 | )} |
| 109 | <div className="manage-permissions-title"> |
| 110 | {i18n('Contributors')} |
| 111 | </div> |
| 112 | <Select |
| 113 | data-test-id="selected-contributors" |
| 114 | value={this.state.itemUsers.map(item => item.userId)} |
| 115 | className="options-input contributors-select" |
| 116 | clearable={false} |
| 117 | onMultiSelectChanged={value => { |
| 118 | this.onChangeItemUsers({ itemUsers: value }); |
| 119 | }} |
| 120 | options={this.buildUserOptions()} |
| 121 | multi |
| 122 | /> |
| 123 | </Form> |
| 124 | </div> |
| 125 | ); |
| 126 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 127 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 128 | onChangeItemUsers({ itemUsers }) { |
| 129 | this.setState({ |
| 130 | itemUsers: itemUsers.map(contributor => { |
| 131 | let contributorFromProps = this.props.itemUsers.find( |
| 132 | user => user.userId === contributor.userId |
| 133 | ); |
| 134 | return { |
| 135 | userId: contributor.value, |
| 136 | fullName: contributor.label, |
| 137 | permission: contributorFromProps |
| 138 | ? contributorFromProps.permission |
| 139 | : permissionTypes.CONTRIBUTOR |
| 140 | }; |
| 141 | }) |
| 142 | }); |
| 143 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 144 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 145 | onsaveItemUsers() { |
| 146 | let { itemUsers: newUsers, newOwnerId } = this.state; |
| 147 | let { itemUsers: oldUsers, onSubmit, itemId, users } = this.props; |
| 148 | let addedUsersIds = newUsers |
| 149 | .filter( |
| 150 | newUser => |
| 151 | !oldUsers |
| 152 | .map(oldUser => oldUser.userId) |
| 153 | .includes(newUser.userId) |
| 154 | ) |
| 155 | .map(user => user.userId); |
| 156 | let removedUsersIds = oldUsers |
| 157 | .filter( |
| 158 | oldUser => |
| 159 | !newUsers |
| 160 | .map(newUser => newUser.userId) |
| 161 | .includes(oldUser.userId) |
| 162 | ) |
| 163 | .map(user => user.userId); |
| 164 | onSubmit({ |
| 165 | itemId, |
| 166 | addedUsersIds, |
| 167 | removedUsersIds, |
| 168 | allUsers: users, |
| 169 | newOwnerId |
| 170 | }); |
| 171 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | export default Permissions; |