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 | |
| 17 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 18 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 19 | import { actionTypes } from './PermissionsConstants.js'; |
| 20 | import { permissionTypes } from './PermissionsConstants.js'; |
| 21 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 22 | import { askForRightsMsg } from './PermissionsManager.jsx'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 23 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
| 24 | |
| 25 | const PermissionsActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 26 | openPermissonsManager(dispatch, { itemId, askForRights }) { |
| 27 | if (askForRights) { |
| 28 | dispatch({ |
| 29 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 30 | data: { |
| 31 | title: i18n('Ask For Contributers Rights'), |
| 32 | msg: askForRightsMsg(), |
| 33 | confirmationButtonText: i18n('SEND'), |
| 34 | onConfirmed: () => this.askForContributorRights() |
| 35 | } |
| 36 | }); |
| 37 | } else { |
| 38 | dispatch({ |
| 39 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 40 | data: { |
| 41 | modalComponentName: modalContentMapper.MANAGE_PERMISSIONS, |
| 42 | title: i18n('Manage Permissions'), |
| 43 | modalComponentProps: { |
| 44 | itemId |
| 45 | } |
| 46 | } |
| 47 | }); |
| 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 | closePermissionManager(dispatch) { |
| 52 | dispatch({ |
| 53 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 54 | }); |
| 55 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 56 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 57 | saveItemUsers( |
| 58 | dispatch, |
| 59 | { itemId, removedUsersIds, addedUsersIds, allUsers } |
| 60 | ) { |
| 61 | return ItemsHelper.updateContributors({ |
| 62 | itemId, |
| 63 | removedUsersIds, |
| 64 | addedUsersIds |
| 65 | }).then(() => |
| 66 | PermissionsActionHelper.fetchItemUsers(dispatch, { |
| 67 | itemId, |
| 68 | allUsers |
| 69 | }) |
| 70 | ); |
| 71 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 72 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 73 | changeOwner(dispatch, { itemId, newOwnerId, allUsers }) { |
| 74 | return ItemsHelper.changeOwner({ itemId, ownerId: newOwnerId }).then( |
| 75 | () => |
| 76 | PermissionsActionHelper.fetchItemUsers(dispatch, { |
| 77 | itemId, |
| 78 | allUsers |
| 79 | }) |
| 80 | ); |
| 81 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 82 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 83 | fetchItemUsers(dispatch, { itemId, allUsers }) { |
| 84 | return ItemsHelper.fetchUsers({ itemId }).then(response => { |
| 85 | let allContributors = response.results; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 86 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 87 | let owner = {}; |
| 88 | let contributors = []; |
| 89 | allContributors.map(user => { |
| 90 | let userObject = allUsers.find( |
| 91 | userObject => userObject.userId === user.userId |
| 92 | ); |
| 93 | if (userObject) { |
| 94 | user = { |
| 95 | ...user, |
| 96 | fullName: userObject.fullName, |
| 97 | role: userObject.role |
| 98 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 99 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 100 | switch (user.permission) { |
| 101 | case permissionTypes.OWNER: |
| 102 | owner = user; |
| 103 | break; |
| 104 | case permissionTypes.CONTRIBUTOR: |
| 105 | contributors.push(user); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 110 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 111 | dispatch({ |
| 112 | type: actionTypes.ITEM_USERS_LOADED, |
| 113 | contributors, |
| 114 | owner |
| 115 | }); |
| 116 | }); |
| 117 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 118 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 119 | askForContributorRights() { |
| 120 | console.log('asked for contributor rights'); |
| 121 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | export default PermissionsActionHelper; |