18559: Add groups and admin tab to user profile, use for other users profile
[arvados-workbench2.git] / src / store / user-profile / user-profile-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4 import { RootState } from "store/store";
5 import { Dispatch } from 'redux';
6 import { reset } from "redux-form";
7 import { ServiceRepository } from "services/services";
8 import { bindDataExplorerActions } from "store/data-explorer/data-explorer-action";
9 import { propertiesActions } from 'store/properties/properties-actions';
10 import { getProperty } from 'store/properties/properties';
11 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
12 import { updateResources } from "store/resources/resources-actions";
13 import { dialogActions } from "store/dialog/dialog-actions";
14
15 export const USER_PROFILE_PANEL_ID = 'userProfilePanel';
16 export const USER_PROFILE_FORM = 'userProfileForm';
17 export const DEACTIVATE_DIALOG = 'deactivateDialog';
18
19 export const UserProfileGroupsActions = bindDataExplorerActions(USER_PROFILE_PANEL_ID);
20
21 export const getCurrentUserProfilePanelUuid = getProperty<string>(USER_PROFILE_PANEL_ID);
22
23 export const loadUserProfilePanel = (userUuid?: string) =>
24   async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25     // Get user uuid from route or use current user uuid
26     const uuid = userUuid || getState().auth.user?.uuid;
27     if (uuid) {
28       const user = await services.userService.get(uuid);
29       dispatch(updateResources([user]));
30       await dispatch(propertiesActions.SET_PROPERTY({ key: USER_PROFILE_PANEL_ID, value: uuid }));
31       dispatch(UserProfileGroupsActions.REQUEST_ITEMS());
32     }
33   }
34
35 export const saveEditedUser = (resource: any) =>
36   async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
37       try {
38           const user = await services.userService.update(resource.uuid, resource);
39           dispatch(updateResources([user]));
40           dispatch(reset(USER_PROFILE_FORM));
41           dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Profile has been updated.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
42       } catch (e) {
43           dispatch(snackbarActions.OPEN_SNACKBAR({
44               message: "Could not update profile",
45               kind: SnackbarKind.ERROR,
46           }));
47       }
48   };
49
50 export const openDeactivateDialog = (uuid: string) =>
51   (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
52     dispatch(dialogActions.OPEN_DIALOG({
53       id: DEACTIVATE_DIALOG,
54       data: {
55           title: 'Deactivate user',
56           text: 'Are you sure you want to deactivate this user?',
57           confirmButtonLabel: 'Deactvate',
58           uuid
59       }
60   }));
61 }
62
63 export const unsetup = (uuid: string) =>
64     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65         try {
66           const user = await services.userService.unsetup(uuid);
67           dispatch(updateResources([user]));
68           dispatch(snackbarActions.OPEN_SNACKBAR({
69               message: "User has been deactivated.",
70               hideDuration: 2000,
71               kind: SnackbarKind.SUCCESS
72           }));
73         } catch (e) {
74           dispatch(snackbarActions.OPEN_SNACKBAR({
75               message: "Could not deactivate user",
76               kind: SnackbarKind.ERROR,
77           }));
78         }
79     };