15012: Uses varargs version of joinFilters().
[arvados-workbench2.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, Tabs, Tab, Paper, Button, Grid } 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 import { noop } from 'lodash';
32
33 type UserPanelRules = "button";
34
35 const styles = withStyles<UserPanelRules>(theme => ({
36     button: {
37         marginTop: theme.spacing.unit,
38         marginRight: theme.spacing.unit * 2,
39         textAlign: 'right',
40         alignSelf: 'center'
41     },
42 }));
43
44 export enum UserPanelColumnNames {
45     FIRST_NAME = "First Name",
46     LAST_NAME = "Last Name",
47     UUID = "Uuid",
48     EMAIL = "Email",
49     ACTIVE = "Active",
50     ADMIN = "Admin",
51     REDIRECT_TO_USER = "Redirect to user",
52     USERNAME = "Username"
53 }
54
55 export const userPanelColumns: DataColumns<string> = [
56     {
57         name: UserPanelColumnNames.FIRST_NAME,
58         selected: true,
59         configurable: true,
60         sortDirection: SortDirection.NONE,
61         filters: createTree(),
62         render: uuid => <ResourceFirstName uuid={uuid} />
63     },
64     {
65         name: UserPanelColumnNames.LAST_NAME,
66         selected: true,
67         configurable: true,
68         sortDirection: SortDirection.NONE,
69         filters: createTree(),
70         render: uuid => <ResourceLastName uuid={uuid} />
71     },
72     {
73         name: UserPanelColumnNames.UUID,
74         selected: true,
75         configurable: true,
76         sortDirection: SortDirection.NONE,
77         filters: createTree(),
78         render: uuid => <ResourceUuid uuid={uuid} />
79     },
80     {
81         name: UserPanelColumnNames.EMAIL,
82         selected: true,
83         configurable: true,
84         sortDirection: SortDirection.NONE,
85         filters: createTree(),
86         render: uuid => <ResourceEmail uuid={uuid} />
87     },
88     {
89         name: UserPanelColumnNames.ACTIVE,
90         selected: true,
91         configurable: true,
92         sortDirection: SortDirection.NONE,
93         filters: createTree(),
94         render: uuid => <ResourceIsActive uuid={uuid} />
95     },
96     {
97         name: UserPanelColumnNames.ADMIN,
98         selected: true,
99         configurable: false,
100         sortDirection: SortDirection.NONE,
101         filters: createTree(),
102         render: uuid => <ResourceIsAdmin uuid={uuid} />
103     },
104     {
105         name: UserPanelColumnNames.USERNAME,
106         selected: true,
107         configurable: false,
108         sortDirection: SortDirection.NONE,
109         filters: createTree(),
110         render: uuid => <ResourceUsername uuid={uuid} />
111     }
112 ];
113
114 interface UserPanelDataProps {
115     resources: ResourcesState;
116 }
117
118 interface UserPanelActionProps {
119     openUserCreateDialog: () => void;
120     handleRowDoubleClick: (uuid: string) => void;
121     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => void;
122 }
123
124 const mapStateToProps = (state: RootState) => {
125     return {
126         resources: state.resources
127     };
128 };
129
130 const mapDispatchToProps = (dispatch: Dispatch) => ({
131     openUserCreateDialog: () => dispatch<any>(openUserCreateDialog()),
132     handleRowDoubleClick: (uuid: string) => dispatch<any>(navigateTo(uuid)),
133     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: any) => dispatch<any>(openContextMenu(event, item))
134 });
135
136 type UserPanelProps = UserPanelDataProps & UserPanelActionProps & DispatchProp & WithStyles<UserPanelRules>;
137
138 export const UserPanel = compose(
139     styles,
140     connect(mapStateToProps, mapDispatchToProps))(
141         class extends React.Component<UserPanelProps> {
142             state = {
143                 value: 0,
144             };
145
146             componentDidMount() {
147                 this.setState({ value: 0 });
148             }
149
150             render() {
151                 const { value } = this.state;
152                 return <Paper>
153                     <Tabs value={value} onChange={this.handleChange} fullWidth>
154                         <Tab label="USERS" />
155                         <Tab label="ACTIVITY" disabled />
156                     </Tabs>
157                     {value === 0 &&
158                         <span>
159                             <DataExplorer
160                                 id={USERS_PANEL_ID}
161                                 onRowClick={noop}
162                                 onRowDoubleClick={noop}
163                                 onContextMenu={this.handleContextMenu}
164                                 contextMenuColumn={true}
165                                 hideColumnSelector
166                                 actions={
167                                     <Grid container justify='flex-end'>
168                                         <Button variant="contained" color="primary" onClick={this.props.openUserCreateDialog}>
169                                             <AddIcon /> NEW USER
170                                         </Button>
171                                     </Grid>
172                                 }
173                                 paperProps={{
174                                     elevation: 0,
175                                 }}
176                                 dataTableDefaultView={
177                                     <DataTableDefaultView
178                                         icon={ShareMeIcon}
179                                         messages={['Your user list is empty.']} />
180                                 } />
181                         </span>}
182                 </Paper>;
183             }
184
185             handleChange = (event: React.MouseEvent<HTMLElement>, value: number) => {
186                 this.setState({ value });
187             }
188
189             handleContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => {
190                 const resource = getResource<UserResource>(resourceUuid)(this.props.resources);
191                 if (resource) {
192                     this.props.onContextMenu(event, {
193                         name: '',
194                         uuid: resource.uuid,
195                         ownerUuid: resource.ownerUuid,
196                         kind: resource.kind,
197                         menuKind: ContextMenuKind.USER
198                     });
199                 }
200             }
201         }
202     );