18123: Change edit permission level to context menu.
[arvados-workbench2.git] / src / store / group-details-panel / group-details-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { bindDataExplorerActions } from 'store/data-explorer/data-explorer-action';
6 import { Dispatch } from 'redux';
7 import { propertiesActions } from 'store/properties/properties-actions';
8 import { getProperty } from 'store/properties/properties';
9 import { Participant } from 'views-components/sharing-dialog/participant-select';
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { reset, startSubmit } from 'redux-form';
12 import { addGroupMember, deleteGroupMember } from 'store/groups-panel/groups-panel-actions';
13 import { getResource } from 'store/resources/resources';
14 import { GroupResource } from 'models/group';
15 import { RootState } from 'store/store';
16 import { ServiceRepository } from 'services/services';
17 import { PermissionResource, PermissionLevel } from 'models/permission';
18 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
19 import { LinkResource } from 'models/link';
20 import { deleteResources } from 'store/resources/resources-actions';
21
22 export const GROUP_DETAILS_MEMBERS_PANEL_ID = 'groupDetailsMembersPanel';
23 export const GROUP_DETAILS_PERMISSIONS_PANEL_ID = 'groupDetailsPermissionsPanel';
24 export const ADD_GROUP_MEMBERS_DIALOG = 'addGroupMembers';
25 export const ADD_GROUP_MEMBERS_FORM = 'addGroupMembers';
26 export const ADD_GROUP_MEMBERS_USERS_FIELD_NAME = 'users';
27 export const MEMBER_ATTRIBUTES_DIALOG = 'memberAttributesDialog';
28 export const MEMBER_REMOVE_DIALOG = 'memberRemoveDialog';
29
30 export const GroupMembersPanelActions = bindDataExplorerActions(GROUP_DETAILS_MEMBERS_PANEL_ID);
31 export const GroupPermissionsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PERMISSIONS_PANEL_ID);
32
33 export const loadGroupDetailsPanel = (groupUuid: string) =>
34     (dispatch: Dispatch) => {
35         dispatch(propertiesActions.SET_PROPERTY({ key: GROUP_DETAILS_MEMBERS_PANEL_ID, value: groupUuid }));
36         dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
37         dispatch(propertiesActions.SET_PROPERTY({ key: GROUP_DETAILS_PERMISSIONS_PANEL_ID, value: groupUuid }));
38         dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
39     };
40
41 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_MEMBERS_PANEL_ID);
42
43 export interface AddGroupMembersFormData {
44     [ADD_GROUP_MEMBERS_USERS_FIELD_NAME]: Participant[];
45 }
46
47 export const openAddGroupMembersDialog = () =>
48     (dispatch: Dispatch) => {
49         dispatch(dialogActions.OPEN_DIALOG({ id: ADD_GROUP_MEMBERS_DIALOG, data: {} }));
50         dispatch(reset(ADD_GROUP_MEMBERS_FORM));
51     };
52
53 export const addGroupMembers = ({ users }: AddGroupMembersFormData) =>
54
55     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
56
57         const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
58
59         if (groupUuid) {
60
61             dispatch(startSubmit(ADD_GROUP_MEMBERS_FORM));
62
63             const group = getResource<GroupResource>(groupUuid)(getState().resources);
64
65             for (const user of users) {
66
67                 await addGroupMember({
68                     user,
69                     group: {
70                         uuid: groupUuid,
71                         name: group ? group.name : groupUuid,
72                     },
73                     dispatch,
74                     permissionService,
75                 });
76
77             }
78
79             dispatch(dialogActions.CLOSE_DIALOG({ id: ADD_GROUP_MEMBERS_FORM }));
80             dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
81
82         }
83     };
84
85 export const editPermissionLevel = (uuid: string, level: PermissionLevel) =>
86     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
87         try {
88             await permissionService.update(uuid, {name: level});
89             dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
90             dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
91             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Permission level changed.', hideDuration: 2000 }));
92         } catch (e) {
93             dispatch(snackbarActions.OPEN_SNACKBAR({
94                 message: 'Failed to update permission',
95                 kind: SnackbarKind.ERROR,
96             }));
97         }
98     };
99
100 export const openGroupMemberAttributes = (uuid: string) =>
101     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
102         const { resources } = getState();
103         const data = getResource<PermissionResource>(uuid)(resources);
104         dispatch(dialogActions.OPEN_DIALOG({ id: MEMBER_ATTRIBUTES_DIALOG, data }));
105     };
106
107 export const openRemoveGroupMemberDialog = (uuid: string) =>
108     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
109         dispatch(dialogActions.OPEN_DIALOG({
110             id: MEMBER_REMOVE_DIALOG,
111             data: {
112                 title: 'Remove member',
113                 text: 'Are you sure you want to remove this member from this group?',
114                 confirmButtonLabel: 'Remove',
115                 uuid
116             }
117         }));
118     };
119
120 export const removeGroupMember = (uuid: string) =>
121
122     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
123
124         const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
125
126         if (groupUuid) {
127             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
128
129             await deleteGroupMember({
130                 link: {
131                     uuid,
132                 },
133                 permissionService,
134                 dispatch,
135             });
136
137             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
138             dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
139
140         }
141
142     };
143
144 export const setMemberIsHidden = (memberLinkUuid: string, permissionLinkUuid: string, hide: boolean) =>
145     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
146         const memberLink = getResource<LinkResource>(memberLinkUuid)(getState().resources);
147
148         if (hide && permissionLinkUuid) {
149             // Remove read permission
150             try {
151                 await permissionService.delete(permissionLinkUuid);
152                 dispatch<any>(deleteResources([permissionLinkUuid]));
153                 dispatch(snackbarActions.OPEN_SNACKBAR({
154                     message: 'Removed read permission.',
155                     hideDuration: 2000,
156                     kind: SnackbarKind.SUCCESS,
157                 }));
158             } catch (e) {
159                 dispatch(snackbarActions.OPEN_SNACKBAR({
160                     message: 'Failed to remove permission',
161                     kind: SnackbarKind.ERROR,
162                 }));
163             }
164         } else if (!hide && memberLink) {
165             // Create read permission
166             try {
167                 await permissionService.create({
168                     headUuid: memberLink.tailUuid,
169                     tailUuid: memberLink.headUuid,
170                     name: PermissionLevel.CAN_READ,
171                 });
172                 dispatch(snackbarActions.OPEN_SNACKBAR({
173                     message: 'Created read permission.',
174                     hideDuration: 2000,
175                     kind: SnackbarKind.SUCCESS,
176                 }));
177                 dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
178             } catch(e) {
179                 dispatch(snackbarActions.OPEN_SNACKBAR({
180                     message: 'Failed to create permission',
181                     kind: SnackbarKind.ERROR,
182                 }));
183             }
184         }
185     };