21720:
[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 { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { Grid, Divider, IconButton, Typography, Tooltip } from '@mui/material';
8 import {
9     Field,
10     WrappedFieldProps,
11     WrappedFieldArrayProps,
12     FieldArray,
13     FieldArrayFieldsProps,
14     InjectedFormProps
15 } from 'redux-form';
16 import { PermissionSelect, formatPermissionLevel, parsePermissionLevel } from './permission-select';
17 import { WithStyles } from '@mui/styles';
18 import withStyles from '@mui/styles/withStyles';
19 import { CloseIcon } from 'components/icon/icon';
20 import { ArvadosTheme } from 'common/custom-theme';
21
22 export interface SaveProps {
23     onSave: () => void;
24 }
25
26 const headerStyles: CustomStyleRulesCallback<'heading'> = (theme: ArvadosTheme) => ({
27     heading: {
28         fontSize: '1.25rem',
29     }
30 });
31
32 export const SharingManagementFormComponent = withStyles(headerStyles)(
33     ({ classes, onSave }: WithStyles<'heading'> & SaveProps & InjectedFormProps<{}, SaveProps>) =>
34         <>
35             <Typography className={classes.heading}>People with access</Typography>
36             <FieldArray<{ onSave: () => void }> name='permissions' component={SharingManagementFieldArray as any} props={{ onSave }} />
37         </>);
38
39 export default SharingManagementFormComponent;
40
41 const SharingManagementFieldArray = ({ fields, onSave }: { onSave: () => void } & WrappedFieldArrayProps<{ email: string }>) =>
42     <div>{fields.map((field, index, fields) =>
43         <PermissionManagementRow key={field} {...{ field, index, fields }} onSave={onSave} />)}
44     </div>;
45
46 const permissionManagementRowStyles: CustomStyleRulesCallback<'root'> = theme => ({
47     root: {
48         padding: `${theme.spacing(1)} 0`,
49     }
50 });
51
52 const PermissionManagementRow = withStyles(permissionManagementRowStyles)(
53     ({ field, index, fields, classes, onSave }: { field: string, index: number, fields: FieldArrayFieldsProps<{ email: string }>, onSave: () => void; } & WithStyles<'root'>) =>
54         <>
55             <Grid container alignItems='center' spacing={1} wrap='nowrap' className={classes.root}>
56                 <Grid item xs={7}>
57                     <Typography noWrap variant='subtitle1'>{fields.get(index).email}</Typography>
58                 </Grid>
59                 <Grid item xs={1} container wrap='nowrap'>
60                     <Tooltip title='Remove access'>
61                         <IconButton onClick={() => { fields.remove(index); onSave(); }} size="large">
62                             <CloseIcon />
63                         </IconButton>
64                     </Tooltip>
65                 </Grid>
66                 <Grid item xs={4} container wrap='nowrap'>
67                     <Field
68                         name={`${field}.permissions` as string}
69                         component={PermissionSelectComponent}
70                         format={formatPermissionLevel}
71                         parse={parsePermissionLevel}
72                         onChange={onSave}
73                     />
74                     
75                 </Grid>
76             </Grid>
77             <Divider />
78         </>
79 );
80
81 const PermissionSelectComponent = ({ input }: WrappedFieldProps) =>
82     <PermissionSelect fullWidth disableUnderline {...input} />;