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