1 // Copyright (C) The Arvados Authors. All rights reserved.
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";
15 export const USER_PROFILE_PANEL_ID = 'userProfilePanel';
16 export const USER_PROFILE_FORM = 'userProfileForm';
17 export const DEACTIVATE_DIALOG = 'deactivateDialog';
19 export const UserProfileGroupsActions = bindDataExplorerActions(USER_PROFILE_PANEL_ID);
21 export const getCurrentUserProfilePanelUuid = getProperty<string>(USER_PROFILE_PANEL_ID);
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;
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());
36 export const saveEditedUser = (resource: any) =>
37 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
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 }));
44 dispatch(snackbarActions.OPEN_SNACKBAR({
45 message: "Could not update profile",
46 kind: SnackbarKind.ERROR,
51 export const openDeactivateDialog = (uuid: string) =>
52 (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
53 dispatch(dialogActions.OPEN_DIALOG({
54 id: DEACTIVATE_DIALOG,
56 title: 'Deactivate user',
57 text: 'Are you sure you want to deactivate this user?',
58 confirmButtonLabel: 'Deactvate',
64 export const unsetup = (uuid: string) =>
65 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
67 const user = await services.userService.unsetup(uuid);
68 dispatch(updateResources([user]));
69 dispatch(snackbarActions.OPEN_SNACKBAR({
70 message: "User has been deactivated.",
72 kind: SnackbarKind.SUCCESS
75 dispatch(snackbarActions.OPEN_SNACKBAR({
76 message: "Could not deactivate user",
77 kind: SnackbarKind.ERROR,