blob: d42a2f83ad724962b81187ee44198018af3610d1 [file] [log] [blame]
talig8e9c0652017-12-20 14:30:43 +02001/*!
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
17import i18n from 'nfvo-utils/i18n/i18n.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
19import { actionTypes } from './PermissionsConstants.js';
20import { permissionTypes } from './PermissionsConstants.js';
21import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
22import { askForRightsMsg } from './PermissionsManager.jsx';
talig8e9c0652017-12-20 14:30:43 +020023import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
24
25const PermissionsActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020026 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 },
talig8e9c0652017-12-20 14:30:43 +020050
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020051 closePermissionManager(dispatch) {
52 dispatch({
53 type: modalActionTypes.GLOBAL_MODAL_CLOSE
54 });
55 },
talig8e9c0652017-12-20 14:30:43 +020056
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020057 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 },
talig8e9c0652017-12-20 14:30:43 +020072
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020073 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 },
talig8e9c0652017-12-20 14:30:43 +020082
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020083 fetchItemUsers(dispatch, { itemId, allUsers }) {
84 return ItemsHelper.fetchUsers({ itemId }).then(response => {
85 let allContributors = response.results;
talig8e9c0652017-12-20 14:30:43 +020086
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020087 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 };
talig8e9c0652017-12-20 14:30:43 +020099
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 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 });
talig8e9c0652017-12-20 14:30:43 +0200110
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200111 dispatch({
112 type: actionTypes.ITEM_USERS_LOADED,
113 contributors,
114 owner
115 });
116 });
117 },
talig8e9c0652017-12-20 14:30:43 +0200118
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200119 askForContributorRights() {
120 console.log('asked for contributor rights');
121 }
talig8e9c0652017-12-20 14:30:43 +0200122};
123
124export default PermissionsActionHelper;