Merge branch '14505-admin-groups-panel'
[arvados-workbench2.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 { 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';
21
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';
28
29 export const GroupDetailsPanelActions = bindDataExplorerActions(GROUP_DETAILS_PANEL_ID);
30
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());
35     };
36
37 export const getCurrentGroupDetailsPanelUuid = getProperty<string>(GROUP_DETAILS_PANEL_ID);
38
39 export interface AddGroupMembersFormData {
40     [ADD_GROUP_MEMBERS_USERS_FIELD_NAME]: Person[];
41 }
42
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));
47     };
48
49 export const addGroupMembers = ({ users }: AddGroupMembersFormData) =>
50
51     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
52
53         const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
54
55         if (groupUuid) {
56
57             dispatch(startSubmit(ADD_GROUP_MEMBERS_FORM));
58
59             const group = getResource<GroupResource>(groupUuid)(getState().resources);
60
61             for (const user of users) {
62
63                 await addGroupMember({
64                     user,
65                     group: {
66                         uuid: groupUuid,
67                         name: group ? group.name : groupUuid,
68                     },
69                     dispatch,
70                     permissionService,
71                 });
72
73             }
74
75             dispatch(dialogActions.CLOSE_DIALOG({ id: ADD_GROUP_MEMBERS_FORM }));
76             dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());
77
78         }
79     };
80
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 }));
86     };
87
88 export const openRemoveGroupMemberDialog = (uuid: string) =>
89     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90         dispatch(dialogActions.OPEN_DIALOG({
91             id: MEMBER_REMOVE_DIALOG,
92             data: {
93                 title: 'Remove member',
94                 text: 'Are you sure you want to remove this member from this group?',
95                 confirmButtonLabel: 'Remove',
96                 uuid
97             }
98         }));
99     };
100
101 export const removeGroupMember = (uuid: string) =>
102
103     async (dispatch: Dispatch, getState: () => RootState, { permissionService }: ServiceRepository) => {
104
105         const groupUuid = getCurrentGroupDetailsPanelUuid(getState().properties);
106
107         if (groupUuid) {
108
109             const group = getResource<GroupResource>(groupUuid)(getState().resources);
110             const user = getResource<UserResource>(groupUuid)(getState().resources);
111
112             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
113
114             await deleteGroupMember({
115                 user: {
116                     uuid,
117                     name: user ? getUserFullname(user) : uuid,
118                 },
119                 group: {
120                     uuid: groupUuid,
121                     name: group ? group.name : groupUuid,
122                 },
123                 permissionService,
124                 dispatch,
125             });
126
127             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
128             dispatch(GroupDetailsPanelActions.REQUEST_ITEMS());
129
130         }
131
132     };