16743: fix for non existing parent project
[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, 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     console.log(1);
63     return isProjectTrashed(parentProj!, resources);
64 };
65
66 export const SidePanelButton = withStyles(styles)(
67     connect((state: RootState) => ({
68         currentItemId: state.router.location
69             ? state.router.location.pathname.split('/').slice(-1)[0]
70             : null,
71         location: state.router.location,
72         resources: state.resources,
73         currentUserUUID: getUserUuid(state),
74     }))(
75         class extends React.Component<SidePanelProps> {
76
77             state: SidePanelState = {
78                 anchorEl: undefined
79             };
80
81             render() {
82                 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
83                 const { anchorEl } = this.state;
84                 let enabled = false;
85                 if (currentItemId === currentUserUUID) {
86                     enabled = true;
87                 } else if (matchProjectRoute(location ? location.pathname : '')) {
88                     const currentProject = getResource<GroupResource>(currentItemId)(resources);
89                     if (currentProject &&
90                         currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
91                         !isProjectTrashed(currentProject, resources)) {
92                         enabled = true;
93                     }
94                 }
95                 return <Toolbar>
96                     <Grid container>
97                         <Grid container item xs alignItems="center" justify="flex-start">
98                             <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
99                                 color="primary" size="small" className={classes.button}
100                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
101                                 aria-haspopup="true"
102                                 onClick={this.handleOpen}>
103                                 <AddIcon />
104                                 New
105                             </Button>
106                             <Menu
107                                 id='aside-menu-list'
108                                 anchorEl={anchorEl}
109                                 open={Boolean(anchorEl)}
110                                 onClose={this.handleClose}
111                                 onClick={this.handleClose}
112                                 transformOrigin={transformOrigin}>
113                                 <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
114                                     <CollectionIcon className={classes.icon} /> New collection
115                                 </MenuItem>
116                                 <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
117                                     <ProcessIcon className={classes.icon} /> Run a process
118                                 </MenuItem>
119                                 <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
120                                     <ProjectIcon className={classes.icon} /> New project
121                                 </MenuItem>
122                             </Menu>
123                         </Grid>
124                     </Grid>
125                 </Toolbar>;
126             }
127
128             handleNewProjectClick = () => {
129                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
130             }
131
132             handleRunProcessClick = () => {
133                 const location = this.props.location;
134                 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
135                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
136                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
137
138                 this.props.dispatch<any>(navigateToRunProcess);
139             }
140
141             handleNewCollectionClick = () => {
142                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
143             }
144
145             handleClose = () => {
146                 this.setState({ anchorEl: undefined });
147             }
148
149             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
150                 this.setState({ anchorEl: event.currentTarget });
151             }
152         }
153     )
154 );