Merge branch '14504-users-admin-panel'
[arvados.git] / src / views / user-panel / user-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { WithStyles, withStyles, Typography, Tabs, Tab, Paper, Button } from '@material-ui/core';
7 import { DataExplorer } from "~/views-components/data-explorer/data-explorer";
8 import { connect, DispatchProp } from 'react-redux';
9 import { DataColumns } from '~/components/data-table/data-table';
10 import { RootState } from '~/store/store';
11 import { SortDirection } from '~/components/data-table/data-column';
12 import { openContextMenu } from "~/store/context-menu/context-menu-actions";
13 import { getResource, ResourcesState } from "~/store/resources/resources";
14 import {
15     ResourceFirstName,
16     ResourceLastName,
17     ResourceUuid,
18     ResourceEmail,
19     ResourceIsActive,
20     ResourceIsAdmin,
21     ResourceUsername
22 } from "~/views-components/data-explorer/renderers";
23 import { navigateTo } from "~/store/navigation/navigation-action";
24 import { ContextMenuKind } from "~/views-components/context-menu/context-menu";
25 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
26 import { createTree } from '~/models/tree';
27 import { compose, Dispatch } from 'redux';
28 import { UserResource } from '~/models/user';
29 import { ShareMeIcon, AddIcon } from '~/components/icon/icon';
30 import { USERS_PANEL_ID, openUserCreateDialog } from '~/store/users/users-actions';
31
32 type UserPanelRules = "button";
33
34 const styles = withStyles<UserPanelRules>(theme => ({
35     button: {
36         marginTop: theme.spacing.unit,
37         marginRight: theme.spacing.unit * 2,
38         textAlign: 'right',
39         alignSelf: 'center'
40     },
41 }));
42
43 export enum UserPanelColumnNames {
44     FIRST_NAME = "First Name",
45     LAST_NAME = "Last Name",
46     UUID = "Uuid",
47     EMAIL = "Email",
48     ACTIVE = "Active",
49     ADMIN = "Admin",
50     REDIRECT_TO_USER = "Redirect to user",
51     USERNAME = "Username"
52 }
53
54 export const userPanelColumns: DataColumns<string> = [
55     {
56         name: UserPanelColumnNames.FIRST_NAME,
57         selected: true,
58         configurable: true,
59         sortDirection: SortDirection.NONE,
60         filters: createTree(),
61         render: uuid => <ResourceFirstName uuid={uuid} />
62     },
63     {
64         name: UserPanelColumnNames.LAST_NAME,
65         selected: true,
66         configurable: true,
67         sortDirection: SortDirection.NONE,
68         filters: createTree(),
69         render: uuid => <ResourceLastName uuid={uuid} />
70     },
71     {
72         name: UserPanelColumnNames.UUID,
73         selected: true,
74         configurable: true,
75         sortDirection: SortDirection.NONE,
76         filters: createTree(),
77         render: uuid => <ResourceUuid uuid={uuid} />
78     },
79     {
80         name: UserPanelColumnNames.EMAIL,
81         selected: true,
82         configurable: true,
83         sortDirection: SortDirection.NONE,
84         filters: createTree(),
85         render: uuid => <ResourceEmail uuid={uuid} />
86     },
87     {
88         name: UserPanelColumnNames.ACTIVE,
89         selected: true,
90         configurable: true,
91         sortDirection: SortDirection.NONE,
92         filters: createTree(),
93         render: uuid => <ResourceIsActive uuid={uuid} />
94     },
95     {
96         name: UserPanelColumnNames.ADMIN,
97         selected: true,
98         configurable: false,
99         sortDirection: SortDirection.NONE,
100         filters: createTree(),
101         render: uuid => <ResourceIsAdmin uuid={uuid} />
102     },
103     {
104         name: UserPanelColumnNames.REDIRECT_TO_USER,
105         selected: true,
106         configurable: false,
107         sortDirection: SortDirection.NONE,
108         filters: createTree(),
109         render: () => <Typography noWrap>(none)</Typography>
110     },
111     {
112         name: UserPanelColumnNames.USERNAME,
113         selected: true,
114         configurable: false,
115         sortDirection: SortDirection.NONE,
116         filters: createTree(),
117         render: uuid => <ResourceUsername uuid={uuid} />
118     }
119 ];
120
121 interface UserPanelDataProps {
122     resources: ResourcesState;
123 }
124
125 interface UserPanelActionProps {
126     openUserCreateDialog: () => void;
127     handleRowDoubleClick: (uuid: string) => void;
128     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
129 }
130
131 const mapStateToProps = (state: RootState) => {
132     return {
133         resources: state.resources
134     };
135 };
136
137 const mapDispatchToProps = (dispatch: Dispatch) => ({
138     openUserCreateDialog: () => dispatch<any>(openUserCreateDialog()),
139     handleRowDoubleClick: (uuid: string) => dispatch<any>(navigateTo(uuid)),
140     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => dispatch<any>(openContextMenu(event, item))
141 });
142
143 type UserPanelProps = UserPanelDataProps & UserPanelActionProps & DispatchProp & WithStyles<UserPanelRules>;
144
145 export const UserPanel = compose(
146     styles,
147     connect(mapStateToProps, mapDispatchToProps))(
148         class extends React.Component<UserPanelProps> {
149             state = {
150                 value: 0,
151             };
152
153             componentDidMount() {
154                 this.setState({ value: 0 });
155             }
156
157             render() {
158                 const { value } = this.state;
159                 return <Paper>
160                     <Tabs value={value} onChange={this.handleChange} fullWidth>
161                         <Tab label="USERS" />
162                         <Tab label="ACTIVITY" disabled />
163                     </Tabs>
164                     {value === 0 &&
165                         <span>
166                             <div className={this.props.classes.button}>
167                                 <Button variant="contained" color="primary" onClick={this.props.openUserCreateDialog}>
168                                     <AddIcon /> NEW USER
169                                 </Button>
170                             </div>
171                             <DataExplorer
172                                 id={USERS_PANEL_ID}
173                                 onRowClick={this.handleRowClick}
174                                 onRowDoubleClick={this.handleRowDoubleClick}
175                                 onContextMenu={this.handleContextMenu}
176                                 contextMenuColumn={true}
177                                 isUserPanel={true}
178                                 dataTableDefaultView={
179                                     <DataTableDefaultView
180                                         icon={ShareMeIcon}
181                                         messages={['Your user list is empty.']} />
182                                 } />
183                         </span>}
184                 </Paper>;
185             }
186
187             handleChange = (event: React.MouseEvent<HTMLElement>, value: number) => {
188                 this.setState({ value });
189             }
190
191             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
192                 const resource = getResource<UserResource>(resourceUuid)(this.props.resources);
193                 if (resource) {
194                     this.props.onContextMenu(event, {
195                         name: '',
196                         uuid: resource.uuid,
197                         ownerUuid: resource.ownerUuid,
198                         kind: resource.kind,
199                         menuKind: ContextMenuKind.USER
200                     });
201                 }
202             }
203
204             handleRowDoubleClick = (uuid: string) => {
205                 this.props.handleRowDoubleClick(uuid);
206             }
207
208             handleRowClick = () => {
209                 return;
210             }
211         }
212     );