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 { Person } from '~/views-components/sharing-dialog/people-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 } from '~/models/permission';
18 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
19 import { UserResource, getUserFullname } from '~/models/user';
21 export const GROUP_DETAILS_PANEL_ID = 'groupDetailsPanel';
22 export const ADD_GROUP_MEMBERS_DIALOG = 'addGrupMembers';
23 export const ADD_GROUP_MEMBERS_FORM = 'addGrupMembers';
24 export const ADD_GROUP_MEMBERS_USERS_FIELD_NAME = 'users';
25 export const MEMBER_ATTRIBUTES_DIALOG = 'memberAttributesDialog';
26 export const MEMBER_REMOVE_DIALOG = 'memberRemoveDialog';
28 export const GroupDetailsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PANEL_ID);
30 export const loadGroupDetailsPanel = (groupUuid: string) =>
31 (dispatch: Dispatch) => {
32 dispatch(propertiesActions.SET_PROPERTY({ key: GROUP_DETAILS_PANEL_ID, value: groupUuid }));
33 dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());
36 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_PANEL_ID);
38 export interface AddGroupMembersFormData {
39 [ADD_GROUP_MEMBERS_USERS_FIELD_NAME]: Person[];
42 export const openAddGroupMembersDialog = () =>
43 (dispatch: Dispatch) => {
44 dispatch(dialogActions.OPEN_DIALOG({ id: ADD_GROUP_MEMBERS_DIALOG, data: {} }));
45 dispatch(reset(ADD_GROUP_MEMBERS_FORM));
48 export const addGroupMembers = ({ users }: AddGroupMembersFormData) =>
50 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
52 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
56 dispatch(startSubmit(ADD_GROUP_MEMBERS_FORM));
58 const group = getResource<GroupResource>(groupUuid)(getState().resources);
60 for (const user of users) {
62 await addGroupMember({
66 name: group ? group.name : groupUuid,
74 dispatch(dialogActions.CLOSE_DIALOG({ id: ADD_GROUP_MEMBERS_FORM }));
75 dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());
80 export const openGroupMemberAttributes = (uuid: string) =>
81 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
82 const { resources } = getState();
83 const data = getResource<PermissionResource>(uuid)(resources);
84 dispatch(dialogActions.OPEN_DIALOG({ id: MEMBER_ATTRIBUTES_DIALOG, data }));
87 export const openRemoveGroupMemberDialog = (uuid: string) =>
88 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
89 dispatch(dialogActions.OPEN_DIALOG({
90 id: MEMBER_REMOVE_DIALOG,
92 title: 'Remove member',
93 text: 'Are you sure you want to remove this member from this group?',
94 confirmButtonLabel: 'Remove',
100 export const removeGroupMember = (uuid: string) =>
102 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
104 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
108 const group = getResource<GroupResource>(groupUuid)(getState().resources);
109 const user = getResource<UserResource>(groupUuid)(getState().resources);
111 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
113 await deleteGroupMember({
116 name: user ? getUserFullname(user) : uuid,
120 name: group ? group.name : groupUuid,
126 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
127 dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());