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'; |
| 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'; |
| 23 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
| 24 | |
| 25 | const PermissionsActionHelper = { |
| 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 | }, |
| 50 | |
| 51 | closePermissionManager(dispatch) { |
| 52 | dispatch({ |
| 53 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 54 | }); |
| 55 | }, |
| 56 | |
| 57 | saveItemUsers(dispatch, {itemId, removedUsersIds, addedUsersIds, allUsers}) { |
| 58 | return ItemsHelper.updateContributors({itemId, removedUsersIds, addedUsersIds}).then(() => |
| 59 | PermissionsActionHelper.fetchItemUsers(dispatch, {itemId, allUsers}) |
| 60 | ); |
| 61 | }, |
| 62 | |
| 63 | changeOwner(dispatch, {itemId, newOwnerId, allUsers}) { |
| 64 | return ItemsHelper.changeOwner({itemId, ownerId: newOwnerId}).then(() => |
| 65 | PermissionsActionHelper.fetchItemUsers(dispatch, {itemId, allUsers}) |
| 66 | ); |
| 67 | }, |
| 68 | |
| 69 | fetchItemUsers(dispatch, {itemId, allUsers}) { |
| 70 | return ItemsHelper.fetchUsers({itemId}).then(response => { |
| 71 | |
| 72 | let allContributors = response.results; |
| 73 | |
| 74 | let owner = {}; |
| 75 | let contributors = []; |
| 76 | allContributors.map(user => { |
| 77 | let userObject = allUsers.find(userObject => userObject.userId === user.userId); |
| 78 | if (userObject) { |
| 79 | user = {...user, fullName: userObject.fullName, role: userObject.role}; |
| 80 | |
| 81 | switch(user.permission) { |
| 82 | case permissionTypes.OWNER: |
| 83 | owner = user; |
| 84 | break; |
| 85 | case permissionTypes.CONTRIBUTOR: |
| 86 | contributors.push(user); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | dispatch({ |
| 93 | type: actionTypes.ITEM_USERS_LOADED, |
| 94 | contributors, |
| 95 | owner |
| 96 | }); |
| 97 | }); |
| 98 | }, |
| 99 | |
| 100 | askForContributorRights() { |
| 101 | console.log('asked for contributor rights'); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | }; |
| 107 | |
| 108 | export default PermissionsActionHelper; |