Merge branch 'main' into 19836-new-tooltip-impl
[arvados-workbench2.git] / 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 } from '@material-ui/core';
7 import {
8     Field,
9     WrappedFieldProps,
10     WrappedFieldArrayProps,
11     FieldArray,
12     FieldArrayFieldsProps
13 } from 'redux-form';
14 import { PermissionSelect, formatPermissionLevel, parsePermissionLevel } from './permission-select';
15 import { WithStyles } from '@material-ui/core/styles';
16 import withStyles from '@material-ui/core/styles/withStyles';
17 import { CloseIcon } from 'components/icon/icon';
18
19
20 export default () =>
21     <FieldArray name='permissions' component={SharingManagementFieldArray as any} />;
22
23 const SharingManagementFieldArray = ({ fields }: WrappedFieldArrayProps<{ email: string }>) =>
24     <div>{ fields.map((field, index, fields) =>
25         <PermissionManagementRow key={field} {...{ field, index, fields }} />) }
26         <Divider />
27     </div>;
28
29 const permissionManagementRowStyles: StyleRulesCallback<'root'> = theme => ({
30     root: {
31         padding: `${theme.spacing.unit}px 0`,
32     }
33 });
34 const PermissionManagementRow = withStyles(permissionManagementRowStyles)(
35     ({ field, index, fields, classes }: { field: string, index: number, fields: FieldArrayFieldsProps<{ email: string }> } & WithStyles<'root'>) =>
36         <>
37             <Divider />
38             <Grid container alignItems='center' spacing={8} wrap='nowrap' className={classes.root}>
39                 <Grid item xs={8}>
40                     <Typography noWrap variant='subtitle1'>{fields.get(index).email}</Typography>
41                 </Grid>
42                 <Grid item xs={4} container wrap='nowrap'>
43                     <Field
44                         name={`${field}.permissions` as string}
45                         component={PermissionSelectComponent}
46                         format={formatPermissionLevel}
47                         parse={parsePermissionLevel} />
48                     <IconButton onClick={() => fields.remove(index)}>
49                         <CloseIcon />
50                     </IconButton>
51                 </Grid>
52             </Grid>
53         </>
54 );
55
56 const PermissionSelectComponent = ({ input }: WrappedFieldProps) =>
57     <PermissionSelect fullWidth disableUnderline {...input} />;