82e90a2bc46eec16f0387bdef534e4f856e14ede
[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 { initialize, 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       await dispatch(propertiesActions.SET_PROPERTY({ key: USER_PROFILE_PANEL_ID, value: uuid }));
29       const user = await services.userService.get(uuid);
30       dispatch(initialize(USER_PROFILE_FORM, user));
31       dispatch(updateResources([user]));
32       dispatch(UserProfileGroupsActions.REQUEST_ITEMS());
33     }
34   }
35
36 export const saveEditedUser = (resource: any) =>
37   async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
38       try {
39           const user = await services.userService.update(resource.uuid, resource);
40           dispatch(updateResources([user]));
41           dispatch(reset(USER_PROFILE_FORM));
42           dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Profile has been updated.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
43       } catch (e) {
44           dispatch(snackbarActions.OPEN_SNACKBAR({
45               message: "Could not update profile",
46               kind: SnackbarKind.ERROR,
47           }));
48       }
49   };
50
51 export const openDeactivateDialog = (uuid: string) =>
52   (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
53     dispatch(dialogActions.OPEN_DIALOG({
54       id: DEACTIVATE_DIALOG,
55       data: {
56           title: 'Deactivate user',
57           text: 'Are you sure you want to deactivate this user?',
58           confirmButtonLabel: 'Deactvate',
59           uuid
60       }
61   }));
62 }
63
64 export const unsetup = (uuid: string) =>
65     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
66         try {
67           const user = await services.userService.unsetup(uuid);
68           dispatch(updateResources([user]));
69           dispatch(snackbarActions.OPEN_SNACKBAR({
70               message: "User has been deactivated.",
71               hideDuration: 2000,
72               kind: SnackbarKind.SUCCESS
73           }));
74         } catch (e) {
75           dispatch(snackbarActions.OPEN_SNACKBAR({
76               message: "Could not deactivate user",
77               kind: SnackbarKind.ERROR,
78           }));
79         }
80     };