1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { bindDataExplorerActions } from '~/store/data-explorer/data-explorer-action';
7 import { RootState } from '~/store/store';
8 import { ServiceRepository } from "~/services/services";
9 import { dialogActions } from '~/store/dialog/dialog-actions';
10 import { startSubmit, reset } from "redux-form";
11 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
12 import { UserResource } from "~/models/user";
13 import { getResource } from '~/store/resources/resources';
14 import { navigateToProject, navigateToUsers, navigateToRootProject } from "~/store/navigation/navigation-action";
16 export const USERS_PANEL_ID = 'usersPanel';
17 export const USER_ATTRIBUTES_DIALOG = 'userAttributesDialog';
18 export const USER_CREATE_FORM_NAME = 'userCreateFormName';
20 export interface UserCreateFormDialogData {
23 virtualMachineName: string;
24 groupVirtualMachine: string;
27 export const openUserAttributes = (uuid: string) =>
28 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29 const { resources } = getState();
30 const data = getResource<UserResource>(uuid)(resources);
31 dispatch(dialogActions.OPEN_DIALOG({ id: USER_ATTRIBUTES_DIALOG, data }));
34 export const openUserCreateDialog = () =>
35 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36 const userUuid = await services.authService.getUuid();
37 const user = await services.userService.get(userUuid!);
38 const virtualMachines = await services.virtualMachineService.list();
39 dispatch(reset(USER_CREATE_FORM_NAME));
40 dispatch(dialogActions.OPEN_DIALOG({ id: USER_CREATE_FORM_NAME, data: { user, ...virtualMachines } }));
43 export const openUserProjects = (uuid: string) =>
44 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45 dispatch<any>(navigateToProject(uuid));
49 export const createUser = (user: UserCreateFormDialogData) =>
50 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
51 dispatch(startSubmit(USER_CREATE_FORM_NAME));
53 const newUser = await services.userService.create({ ...user });
54 dispatch(dialogActions.CLOSE_DIALOG({ id: USER_CREATE_FORM_NAME }));
55 dispatch(reset(USER_CREATE_FORM_NAME));
56 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "User has been successfully created.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
57 dispatch<any>(loadUsersPanel());
58 dispatch(userBindedActions.REQUEST_ITEMS());
65 export const openUserPanel = () =>
66 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
67 const user = getState().auth.user;
68 if (user && user.isAdmin) {
69 dispatch<any>(navigateToUsers);
71 dispatch<any>(navigateToRootProject);
72 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
76 export const toggleIsActive = (uuid: string) =>
77 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78 const { resources } = getState();
79 const data = getResource<UserResource>(uuid)(resources);
80 const isActive = data!.isActive;
81 const newActivity = await services.userService.update(uuid, { ...data, isActive: !isActive });
82 dispatch<any>(loadUsersPanel());
86 export const toggleIsAdmin = (uuid: string) =>
87 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
88 const { resources } = getState();
89 const data = getResource<UserResource>(uuid)(resources);
90 const isAdmin = data!.isAdmin;
91 const newActivity = await services.userService.update(uuid, { ...data, isAdmin: !isAdmin });
92 dispatch<any>(loadUsersPanel());
96 export const userBindedActions = bindDataExplorerActions(USERS_PANEL_ID);
98 export const loadUsersData = () =>
99 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 await services.userService.list();
103 export const loadUsersPanel = () =>
104 (dispatch: Dispatch) => {
105 dispatch(userBindedActions.REQUEST_ITEMS());