c8556862a1db7be46ff78fb18ad97a87594919d2
[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 } 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 export const SETUP_DIALOG = 'setupDialog';
19
20 export const UserProfileGroupsActions = bindDataExplorerActions(USER_PROFILE_PANEL_ID);
21
22 export const getCurrentUserProfilePanelUuid = getProperty<string>(USER_PROFILE_PANEL_ID);
23
24 export const loadUserProfilePanel = (userUuid?: string) =>
25   async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
26     // Get user uuid from route or use current user uuid
27     const uuid = userUuid || getState().auth.user?.uuid;
28     if (uuid) {
29       await dispatch(propertiesActions.SET_PROPERTY({ key: USER_PROFILE_PANEL_ID, value: uuid }));
30       const user = await services.userService.get(uuid);
31       dispatch(initialize(USER_PROFILE_FORM, user));
32       dispatch(updateResources([user]));
33       dispatch(UserProfileGroupsActions.REQUEST_ITEMS());
34     }
35   }
36
37 export const saveEditedUser = (resource: any) =>
38   async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
39       try {
40           const user = await services.userService.update(resource.uuid, resource);
41           dispatch(updateResources([user]));
42           dispatch(initialize(USER_PROFILE_FORM, user));
43           dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Profile has been updated.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
44       } catch (e) {
45           dispatch(snackbarActions.OPEN_SNACKBAR({
46               message: "Could not update profile",
47               kind: SnackbarKind.ERROR,
48           }));
49       }
50   };
51
52 export const openDeactivateDialog = (uuid: string) =>
53   (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
54     dispatch(dialogActions.OPEN_DIALOG({
55       id: DEACTIVATE_DIALOG,
56       data: {
57           title: 'Deactivate user',
58           text: 'Are you sure you want to deactivate this user?',
59           confirmButtonLabel: 'Deactvate',
60           uuid
61       }
62   }));
63 }
64
65 export const openSetupDialog = (uuid: string) =>
66   (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
67     dispatch(dialogActions.OPEN_DIALOG({
68       id: SETUP_DIALOG,
69       data: {
70           title: 'Setup user',
71           text: 'Are you sure you want to setup this user?',
72           confirmButtonLabel: 'Confirm',
73           uuid
74       }
75   }));
76 }
77
78 export const setup = (uuid: string) =>
79     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
80         try {
81             const resources = await services.userService.setup(uuid);
82             dispatch(updateResources(resources.items));
83             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "User has been setup", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
84         } catch (e) {
85             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
86         } finally {
87             dispatch(dialogActions.CLOSE_DIALOG({ id: SETUP_DIALOG }));
88         }
89
90     };
91
92 export const unsetup = (uuid: string) =>
93     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
94         try {
95           const user = await services.userService.unsetup(uuid);
96           dispatch(updateResources([user]));
97           dispatch(snackbarActions.OPEN_SNACKBAR({
98               message: "User has been deactivated.",
99               hideDuration: 2000,
100               kind: SnackbarKind.SUCCESS
101           }));
102         } catch (e) {
103           dispatch(snackbarActions.OPEN_SNACKBAR({
104               message: "Could not deactivate user",
105               kind: SnackbarKind.ERROR,
106           }));
107         }
108     };