Merge branch 'jszlenk/create_new_subproject' refs #21937
[arvados.git] / services / workbench2 / src / views-components / sharing-dialog / sharing-management-form-component.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { Grid, StyleRulesCallback, Divider, IconButton, Typography, Tooltip } from '@material-ui/core';
7 import {
8     Field,
9     WrappedFieldProps,
10     WrappedFieldArrayProps,
11     FieldArray,
12     FieldArrayFieldsProps,
13     InjectedFormProps
14 } from 'redux-form';
15 import { PermissionSelect, formatPermissionLevel, parsePermissionLevel } from './permission-select';
16 import { WithStyles } from '@material-ui/core/styles';
17 import withStyles from '@material-ui/core/styles/withStyles';
18 import { CloseIcon } from 'components/icon/icon';
19 import { ArvadosTheme } from 'common/custom-theme';
20
21 export interface SaveProps {
22     onSave: () => void;
23 }
24
25 const headerStyles: StyleRulesCallback<'heading'> = (theme: ArvadosTheme) => ({
26     heading: {
27         fontSize: '1.25rem',
28     }
29 });
30
31 export const SharingManagementFormComponent = withStyles(headerStyles)(
32     ({ classes, onSave }: WithStyles<'heading'> & SaveProps & InjectedFormProps<{}, SaveProps>) =>
33         <>
34             <Typography className={classes.heading}>People with access</Typography>
35             <FieldArray<{ onSave: () => void }> name='permissions' component={SharingManagementFieldArray as any} props={{ onSave }} />
36         </>);
37
38 export default SharingManagementFormComponent;
39
40 const SharingManagementFieldArray = ({ fields, onSave }: { onSave: () => void } & WrappedFieldArrayProps<{ email: string }>) =>
41     <div>{fields.map((field, index, fields) =>
42         <PermissionManagementRow key={field} {...{ field, index, fields }} onSave={onSave} />)}
43     </div>;
44
45 const permissionManagementRowStyles: StyleRulesCallback<'root'> = theme => ({
46     root: {
47         padding: `${theme.spacing.unit}px 0`,
48     }
49 });
50
51 const PermissionManagementRow = withStyles(permissionManagementRowStyles)(
52     ({ field, index, fields, classes, onSave }: { field: string, index: number, fields: FieldArrayFieldsProps<{ email: string }>, onSave: () => void; } & WithStyles<'root'>) =>
53         <>
54             <Grid container alignItems='center' spacing={8} wrap='nowrap' className={classes.root}>
55                 <Grid item xs={7}>
56                     <Typography noWrap variant='subtitle1'>{fields.get(index).email}</Typography>
57                 </Grid>
58                 <Grid item xs={1} container wrap='nowrap'>
59                     <Tooltip title='Remove access'>
60                         <IconButton onClick={() => { fields.remove(index); onSave(); }}>
61                             <CloseIcon />
62                         </IconButton>
63                     </Tooltip>
64                 </Grid>
65                 <Grid item xs={4} container wrap='nowrap'>
66                     <Field
67                         name={`${field}.permissions` as string}
68                         component={PermissionSelectComponent}
69                         format={formatPermissionLevel}
70                         parse={parsePermissionLevel}
71                         onChange={onSave}
72                     />
73                     
74                 </Grid>
75             </Grid>
76             <Divider />
77         </>
78 );
79
80 const PermissionSelectComponent = ({ input }: WrappedFieldProps) =>
81     <PermissionSelect fullWidth disableUnderline {...input} />;