21128: fixed unwanted rowselects on other operational clicks Arvados-DCO-1.1-Signed...
[arvados.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 { dialogActions } from 'store/dialog/dialog-actions';
10 import { deleteGroupMember } from 'store/groups-panel/groups-panel-actions';
11 import { getResource } from 'store/resources/resources';
12 import { RootState } from 'store/store';
13 import { ServiceRepository } from 'services/services';
14 import { PermissionResource, PermissionLevel } from 'models/permission';
15 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
16 import { LinkResource } from 'models/link';
17 import { deleteResources, updateResources } from 'store/resources/resources-actions';
18 import { openSharingDialog } from 'store/sharing-dialog/sharing-dialog-actions';
19 import { UserProfileGroupsActions } from 'store/user-profile/user-profile-actions';
20
21 export const GROUP_DETAILS_MEMBERS_PANEL_ID = 'groupDetailsMembersPanel';
22 export const GROUP_DETAILS_PERMISSIONS_PANEL_ID = 'groupDetailsPermissionsPanel';
23 export const MEMBER_ATTRIBUTES_DIALOG = 'memberAttributesDialog';
24 export const MEMBER_REMOVE_DIALOG = 'memberRemoveDialog';
25
26 export const GroupMembersPanelActions = bindDataExplorerActions(GROUP_DETAILS_MEMBERS_PANEL_ID);
27 export const GroupPermissionsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PERMISSIONS_PANEL_ID);
28
29 export const loadGroupDetailsPanel = (groupUuid: string) =>
30     (dispatch: Dispatch) => {
31         dispatch(propertiesActions.SET_PROPERTY({ key: GROUP_DETAILS_MEMBERS_PANEL_ID, value: groupUuid }));
32         dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
33         dispatch(propertiesActions.SET_PROPERTY({ key: GROUP_DETAILS_PERMISSIONS_PANEL_ID, value: groupUuid }));
34         dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
35     };
36
37 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_MEMBERS_PANEL_ID);
38
39 export const openAddGroupMembersDialog = () =>
40     (dispatch: Dispatch, getState: () => RootState) => {
41         const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
42         if (groupUuid) {
43             dispatch<any>(openSharingDialog(groupUuid, () => {
44                 dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
45             }));
46         }
47     };
48
49 export const editPermissionLevel = (uuid: string, level: PermissionLevel) =>
50     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
51         try {
52             const permission = await permissionService.update(uuid, {name: level});
53             dispatch(updateResources([permission]));
54             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Permission level changed.', hideDuration: 2000 }));
55         } catch (e) {
56             dispatch(snackbarActions.OPEN_SNACKBAR({
57                 message: 'Failed to update permission',
58                 kind: SnackbarKind.ERROR,
59             }));
60         }
61     };
62
63 export const openGroupMemberAttributes = (uuid: string) =>
64     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65         const { resources } = getState();
66         const data = getResource<PermissionResource>(uuid)(resources);
67         dispatch(dialogActions.OPEN_DIALOG({ id: MEMBER_ATTRIBUTES_DIALOG, data }));
68     };
69
70 export const openRemoveGroupMemberDialog = (uuid: string) =>
71     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72         dispatch(dialogActions.OPEN_DIALOG({
73             id: MEMBER_REMOVE_DIALOG,
74             data: {
75                 title: 'Remove member',
76                 text: 'Are you sure you want to remove this member from this group?',
77                 confirmButtonLabel: 'Remove',
78                 uuid
79             }
80         }));
81     };
82
83 export const removeGroupMember = (uuid: string) =>
84
85     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
86         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
87         await deleteGroupMember({
88             link: {
89                 uuid,
90             },
91             permissionService,
92             dispatch,
93         });
94         dispatch<any>(deleteResources([uuid]));
95         dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
96         dispatch(UserProfileGroupsActions.REQUEST_ITEMS());
97
98         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
99     };
100
101 export const setMemberIsHidden = (memberLinkUuid: string, permissionLinkUuid: string, visible: boolean) =>
102     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
103         const memberLink = getResource<LinkResource>(memberLinkUuid)(getState().resources);
104
105         if (!visible && permissionLinkUuid) {
106             // Remove read permission
107             try {
108                 await permissionService.delete(permissionLinkUuid);
109                 dispatch<any>(deleteResources([permissionLinkUuid]));
110                 dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
111                 dispatch(snackbarActions.OPEN_SNACKBAR({
112                     message: 'Removed read permission.',
113                     hideDuration: 2000,
114                     kind: SnackbarKind.SUCCESS,
115                 }));
116             } catch (e) {
117                 dispatch(snackbarActions.OPEN_SNACKBAR({
118                     message: 'Failed to remove permission',
119                     kind: SnackbarKind.ERROR,
120                 }));
121             }
122         } else if (visible && memberLink) {
123             // Create read permission
124             try {
125                 const permission = await permissionService.create({
126                     headUuid: memberLink.tailUuid,
127                     tailUuid: memberLink.headUuid,
128                     name: PermissionLevel.CAN_READ,
129                 });
130                 dispatch(updateResources([permission]));
131                 dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
132                 dispatch(snackbarActions.OPEN_SNACKBAR({
133                     message: 'Created read permission.',
134                     hideDuration: 2000,
135                     kind: SnackbarKind.SUCCESS,
136                 }));
137             } catch(e) {
138                 dispatch(snackbarActions.OPEN_SNACKBAR({
139                     message: 'Failed to create permission',
140                     kind: SnackbarKind.ERROR,
141                 }));
142             }
143         }
144     };