16743: Removed console.log's updated tests
[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 import { extractUuidKind, ResourceKind } from '~/models/resource';
21
22 type CssRules = 'button' | 'menuItem' | 'icon';
23
24 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25     button: {
26         boxShadow: 'none',
27         padding: '2px 10px 2px 5px',
28         fontSize: '0.75rem'
29     },
30     menuItem: {
31         fontSize: '0.875rem',
32         color: theme.palette.grey["700"]
33     },
34     icon: {
35         marginRight: theme.spacing.unit
36     }
37 });
38
39 interface SidePanelDataProps {
40     location: any;
41     currentItemId: string;
42     resources: ResourcesState;
43     currentUserUUID: string | undefined;
44 }
45
46 interface SidePanelState {
47     anchorEl: any;
48 }
49
50 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
51
52 const transformOrigin: PopoverOrigin = {
53     vertical: -50,
54     horizontal: 0
55 };
56
57 export const isProjectTrashed = (proj: GroupResource | undefined, resources: ResourcesState): boolean => {
58     if (proj === undefined) { return false; }
59     if (proj.isTrashed) { return true; }
60     if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
61     const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
62     return isProjectTrashed(parentProj, resources);
63 };
64
65 export const SidePanelButton = withStyles(styles)(
66     connect((state: RootState) => ({
67         currentItemId: state.router.location
68             ? state.router.location.pathname.split('/').slice(-1)[0]
69             : null,
70         location: state.router.location,
71         resources: state.resources,
72         currentUserUUID: getUserUuid(state),
73     }))(
74         class extends React.Component<SidePanelProps> {
75
76             state: SidePanelState = {
77                 anchorEl: undefined
78             };
79
80             render() {
81                 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
82                 const { anchorEl } = this.state;
83                 let enabled = false;
84                 if (currentItemId === currentUserUUID) {
85                     enabled = true;
86                 } else if (matchProjectRoute(location ? location.pathname : '')) {
87                     const currentProject = getResource<GroupResource>(currentItemId)(resources);
88                     if (currentProject &&
89                         currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
90                         !isProjectTrashed(currentProject, resources)) {
91                         enabled = true;
92                     }
93                 }
94                 return <Toolbar>
95                     <Grid container>
96                         <Grid container item xs alignItems="center" justify="flex-start">
97                             <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
98                                 color="primary" size="small" className={classes.button}
99                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
100                                 aria-haspopup="true"
101                                 onClick={this.handleOpen}>
102                                 <AddIcon />
103                                 New
104                             </Button>
105                             <Menu
106                                 id='aside-menu-list'
107                                 anchorEl={anchorEl}
108                                 open={Boolean(anchorEl)}
109                                 onClose={this.handleClose}
110                                 onClick={this.handleClose}
111                                 transformOrigin={transformOrigin}>
112                                 <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
113                                     <CollectionIcon className={classes.icon} /> New collection
114                                 </MenuItem>
115                                 <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
116                                     <ProcessIcon className={classes.icon} /> Run a process
117                                 </MenuItem>
118                                 <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
119                                     <ProjectIcon className={classes.icon} /> New project
120                                 </MenuItem>
121                             </Menu>
122                         </Grid>
123                     </Grid>
124                 </Toolbar>;
125             }
126
127             handleNewProjectClick = () => {
128                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
129             }
130
131             handleRunProcessClick = () => {
132                 const location = this.props.location;
133                 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
134                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
135                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
136
137                 this.props.dispatch<any>(navigateToRunProcess);
138             }
139
140             handleNewCollectionClick = () => {
141                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
142             }
143
144             handleClose = () => {
145                 this.setState({ anchorEl: undefined });
146             }
147
148             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
149                 this.setState({ anchorEl: event.currentTarget });
150             }
151         }
152     )
153 );