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 { GroupDetailsPanel } from '~/views/group-details-panel/group-details-panel';
19 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
20 import { UserResource, getUserFullname } from '~/models/user';
22 export const GROUP_DETAILS_PANEL_ID = 'groupDetailsPanel';
23 export const ADD_GROUP_MEMBERS_DIALOG = 'addGrupMembers';
24 export const ADD_GROUP_MEMBERS_FORM = 'addGrupMembers';
25 export const ADD_GROUP_MEMBERS_USERS_FIELD_NAME = 'users';
26 export const MEMBER_ATTRIBUTES_DIALOG = 'memberAttributesDialog';
27 export const MEMBER_REMOVE_DIALOG = 'memberRemoveDialog';
29 export const GroupDetailsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PANEL_ID);
31 export const loadGroupDetailsPanel = (groupUuid: string) =>
32 (dispatch: Dispatch) => {
33 dispatch(propertiesActions.SET_PROPERTY({ key: GROUP_DETAILS_PANEL_ID, value: groupUuid }));
34 dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());
37 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_PANEL_ID);
39 export interface AddGroupMembersFormData {
40 [ADD_GROUP_MEMBERS_USERS_FIELD_NAME]: Person[];
43 export const openAddGroupMembersDialog = () =>
44 (dispatch: Dispatch) => {
45 dispatch(dialogActions.OPEN_DIALOG({ id: ADD_GROUP_MEMBERS_DIALOG, data: {} }));
46 dispatch(reset(ADD_GROUP_MEMBERS_FORM));
49 export const addGroupMembers = ({ users }: AddGroupMembersFormData) =>
51 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
53 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
57 dispatch(startSubmit(ADD_GROUP_MEMBERS_FORM));
59 const group = getResource<GroupResource>(groupUuid)(getState().resources);
61 for (const user of users) {
63 await addGroupMember({
67 name: group ? group.name : groupUuid,
75 dispatch(dialogActions.CLOSE_DIALOG({ id: ADD_GROUP_MEMBERS_FORM }));
76 dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());
81 export const openGroupMemberAttributes = (uuid: string) =>
82 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
83 const { resources } = getState();
84 const data = getResource<PermissionResource>(uuid)(resources);
85 dispatch(dialogActions.OPEN_DIALOG({ id: MEMBER_ATTRIBUTES_DIALOG, data }));
88 export const openRemoveGroupMemberDialog = (uuid: string) =>
89 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90 dispatch(dialogActions.OPEN_DIALOG({
91 id: MEMBER_REMOVE_DIALOG,
93 title: 'Remove member',
94 text: 'Are you sure you want to remove this member from this group?',
95 confirmButtonLabel: 'Remove',
101 export const removeGroupMember = (uuid: string) =>
103 async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
105 const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
109 const group = getResource<GroupResource>(groupUuid)(getState().resources);
110 const user = getResource<UserResource>(groupUuid)(getState().resources);
112 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
114 await deleteGroupMember({
117 name: user ? getUserFullname(user) : uuid,
121 name: group ? group.name : groupUuid,
127 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
128 dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());