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 { 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';
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';
30 export const GroupMembersPanelActions = bindDataExplorerActions(GROUP_DETAILS_MEMBERS_PANEL_ID);
31 export const GroupPermissionsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PERMISSIONS_PANEL_ID);
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());
41 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_MEMBERS_PANEL_ID);
43 export interface AddGroupMembersFormData {
44 [ADD_GROUP_MEMBERS_USERS_FIELD_NAME]: Participant[];
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));
53 export const addGroupMembers = ({ users }: AddGroupMembersFormData) =>
55 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
57 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
61 dispatch(startSubmit(ADD_GROUP_MEMBERS_FORM));
63 const group = getResource<GroupResource>(groupUuid)(getState().resources);
65 for (const user of users) {
67 await addGroupMember({
71 name: group ? group.name : groupUuid,
79 dispatch(dialogActions.CLOSE_DIALOG({ id: ADD_GROUP_MEMBERS_FORM }));
80 dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
85 export const editPermissionLevel = (uuid: string, level: PermissionLevel) =>
86 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
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 }));
93 dispatch(snackbarActions.OPEN_SNACKBAR({
94 message: 'Failed to update permission',
95 kind: SnackbarKind.ERROR,
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 }));
107 export const openRemoveGroupMemberDialog = (uuid: string) =>
108 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
109 dispatch(dialogActions.OPEN_DIALOG({
110 id: MEMBER_REMOVE_DIALOG,
112 title: 'Remove member',
113 text: 'Are you sure you want to remove this member from this group?',
114 confirmButtonLabel: 'Remove',
120 export const removeGroupMember = (uuid: string) =>
122 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
124 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
127 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
129 await deleteGroupMember({
137 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
138 dispatch(GroupMembersPanelActions.REQUEST_ITEMS());
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);
148 if (hide && permissionLinkUuid) {
149 // Remove read permission
151 await permissionService.delete(permissionLinkUuid);
152 dispatch<any>(deleteResources([permissionLinkUuid]));
153 dispatch(snackbarActions.OPEN_SNACKBAR({
154 message: 'Removed read permission.',
156 kind: SnackbarKind.SUCCESS,
159 dispatch(snackbarActions.OPEN_SNACKBAR({
160 message: 'Failed to remove permission',
161 kind: SnackbarKind.ERROR,
164 } else if (!hide && memberLink) {
165 // Create read permission
167 await permissionService.create({
168 headUuid: memberLink.tailUuid,
169 tailUuid: memberLink.headUuid,
170 name: PermissionLevel.CAN_READ,
172 dispatch(snackbarActions.OPEN_SNACKBAR({
173 message: 'Created read permission.',
175 kind: SnackbarKind.SUCCESS,
177 dispatch(GroupPermissionsPanelActions.REQUEST_ITEMS());
179 dispatch(snackbarActions.OPEN_SNACKBAR({
180 message: 'Failed to create permission',
181 kind: SnackbarKind.ERROR,