1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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';
26 export const GroupMembersPanelActions = bindDataExplorerActions(GROUP_DETAILS_MEMBERS_PANEL_ID);
27 export const GroupPermissionsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PERMISSIONS_PANEL_ID);
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());
37 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_MEMBERS_PANEL_ID);
39 export const openAddGroupMembersDialog = () =>
40 (dispatch: Dispatch, getState: () => RootState) => {
41 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
43 dispatch<any>(openSharingDialog(groupUuid, () => {
44 dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
49 export const editPermissionLevel = (uuid: string, level: PermissionLevel) =>
50 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
52 const permission = await permissionService.update(uuid, {name: level});
53 dispatch(updateResources([permission]));
54 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Permission level changed.', hideDuration: 2000 }));
56 dispatch(snackbarActions.OPEN_SNACKBAR({
57 message: 'Failed to update permission',
58 kind: SnackbarKind.ERROR,
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 }));
70 export const openRemoveGroupMemberDialog = (uuid: string) =>
71 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72 dispatch(dialogActions.OPEN_DIALOG({
73 id: MEMBER_REMOVE_DIALOG,
75 title: 'Remove member',
76 text: 'Are you sure you want to remove this member from this group?',
77 confirmButtonLabel: 'Remove',
83 export const removeGroupMember = (uuid: string) =>
85 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
86 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
87 await deleteGroupMember({
94 dispatch<any>(deleteResources([uuid]));
95 dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
96 dispatch(UserProfileGroupsActions.REQUEST_ITEMS());
98 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
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);
105 if (!visible && permissionLinkUuid) {
106 // Remove read permission
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.',
114 kind: SnackbarKind.SUCCESS,
117 dispatch(snackbarActions.OPEN_SNACKBAR({
118 message: 'Failed to remove permission',
119 kind: SnackbarKind.ERROR,
122 } else if (visible && memberLink) {
123 // Create read permission
125 const permission = await permissionService.create({
126 headUuid: memberLink.tailUuid,
127 tailUuid: memberLink.headUuid,
128 name: PermissionLevel.CAN_READ,
130 dispatch(updateResources([permission]));
131 dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
132 dispatch(snackbarActions.OPEN_SNACKBAR({
133 message: 'Created read permission.',
135 kind: SnackbarKind.SUCCESS,
138 dispatch(snackbarActions.OPEN_SNACKBAR({
139 message: 'Failed to create permission',
140 kind: SnackbarKind.ERROR,