16439: Enables the +NEW button only when the user's view is writable.
[arvados-workbench2.git] / src / views-components / side-panel-button / side-panel-button.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 { connect, DispatchProp } from 'react-redux';
7 import { RootState } from '~/store/store';
8 import { ArvadosTheme } from '~/common/custom-theme';
9 import { PopoverOrigin } from '@material-ui/core/Popover';
10 import { StyleRulesCallback, WithStyles, withStyles, Toolbar, Grid, Button, MenuItem, Menu } from '@material-ui/core';
11 import { AddIcon, CollectionIcon, ProcessIcon, ProjectIcon } from '~/components/icon/icon';
12 import { openProjectCreateDialog } from '~/store/projects/project-create-actions';
13 import { openCollectionCreateDialog } from '~/store/collections/collection-create-actions';
14 import { navigateToRunProcess } from '~/store/navigation/navigation-action';
15 import { runProcessPanelActions } from '~/store/run-process-panel/run-process-panel-actions';
16 import { getUserUuid } from '~/common/getuser';
17 import { matchProjectRoute } from '~/routes/routes';
18 import { GroupResource } from '~/models/group';
19 import { ResourcesState, getResource } from '~/store/resources/resources';
20
21 type CssRules = 'button' | 'menuItem' | 'icon';
22
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24     button: {
25         boxShadow: 'none',
26         padding: '2px 10px 2px 5px',
27         fontSize: '0.75rem'
28     },
29     menuItem: {
30         fontSize: '0.875rem',
31         color: theme.palette.grey["700"]
32     },
33     icon: {
34         marginRight: theme.spacing.unit
35     }
36 });
37
38 interface SidePanelDataProps {
39     location: any;
40     currentItemId: string;
41     resources: ResourcesState;
42     currentUserUUID: string | undefined;
43 }
44
45 interface SidePanelState {
46     anchorEl: any;
47 }
48
49 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
50
51 const transformOrigin: PopoverOrigin = {
52     vertical: -50,
53     horizontal: 0
54 };
55
56 export const SidePanelButton = withStyles(styles)(
57     connect((state: RootState) => ({
58         currentItemId: state.router.location
59             ? state.router.location.pathname.split('/').slice(-1)[0]
60             : null,
61         location: state.router.location,
62         resources: state.resources,
63         currentUserUUID: getUserUuid(state),
64     }))(
65         class extends React.Component<SidePanelProps> {
66
67             state: SidePanelState = {
68                 anchorEl: undefined
69             };
70
71             render() {
72                 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
73                 const { anchorEl } = this.state;
74                 let enabled = false;
75                 if (currentItemId === currentUserUUID) {
76                     enabled = true;
77                 } else if (matchProjectRoute(location ? location.pathname : '')) {
78                     const currentProject = getResource<GroupResource>(currentItemId)(resources);
79                     if (currentProject &&
80                         currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
81                         !currentProject.isTrashed) {
82                         enabled = true;
83                     }
84                 }
85                 return <Toolbar>
86                     <Grid container>
87                         <Grid container item xs alignItems="center" justify="flex-start">
88                             <Button variant="contained" disabled={!enabled}
89                                 color="primary" size="small" className={classes.button}
90                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
91                                 aria-haspopup="true"
92                                 onClick={this.handleOpen}>
93                                 <AddIcon />
94                                 New
95                             </Button>
96                             <Menu
97                                 id='aside-menu-list'
98                                 anchorEl={anchorEl}
99                                 open={Boolean(anchorEl)}
100                                 onClose={this.handleClose}
101                                 onClick={this.handleClose}
102                                 transformOrigin={transformOrigin}>
103                                 <MenuItem className={classes.menuItem} onClick={this.handleNewCollectionClick}>
104                                     <CollectionIcon className={classes.icon} /> New collection
105                                 </MenuItem>
106                                 <MenuItem className={classes.menuItem} onClick={this.handleRunProcessClick}>
107                                     <ProcessIcon className={classes.icon} /> Run a process
108                                 </MenuItem>
109                                 <MenuItem className={classes.menuItem} onClick={this.handleNewProjectClick}>
110                                     <ProjectIcon className={classes.icon} /> New project
111                                 </MenuItem>
112                             </Menu>
113                         </Grid>
114                     </Grid>
115                 </Toolbar>;
116             }
117
118             handleNewProjectClick = () => {
119                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
120             }
121
122             handleRunProcessClick = () => {
123                 const location = this.props.location;
124                 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
125                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
126                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
127
128                 this.props.dispatch<any>(navigateToRunProcess);
129             }
130
131             handleNewCollectionClick = () => {
132                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
133             }
134
135             handleClose = () => {
136                 this.setState({ anchorEl: undefined });
137             }
138
139             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
140                 this.setState({ anchorEl: event.currentTarget });
141             }
142         }
143     )
144 );