Merge branch '14452-my-account'
[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 * as 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} />;
22
23 const SharingManagementFieldArray = ({ fields }: WrappedFieldArrayProps<{ email: string }>) =>
24     <div>
25         {
26             fields.map((field, index, fields) =>
27                 <PermissionManagementRow key={field} {...{ field, index, fields }} />)
28         }
29         <Divider />
30     </div>;
31
32 const permissionManagementRowStyles: StyleRulesCallback<'root'> = theme => ({
33     root: {
34         padding: `${theme.spacing.unit}px 0`,
35     }
36 });
37 const PermissionManagementRow = withStyles(permissionManagementRowStyles)(
38     ({ field, index, fields, classes }: { field: string, index: number, fields: FieldArrayFieldsProps<{ email: string }> } & WithStyles<'root'>) =>
39         <>
40             <Divider />
41             <Grid container alignItems='center' spacing={8} wrap='nowrap' className={classes.root}>
42                 <Grid item xs={8}>
43                     <Typography noWrap variant='subheading'>{fields.get(index).email}</Typography>
44                 </Grid>
45                 <Grid item xs={4} container wrap='nowrap'>
46                     <Field
47                         name={`${field}.permissions`}
48                         component={PermissionSelectComponent}
49                         format={formatPermissionLevel}
50                         parse={parsePermissionLevel} />
51                     <IconButton onClick={() => fields.remove(index)}>
52                         <CloseIcon />
53                     </IconButton>
54                 </Grid>
55             </Grid>
56         </>
57 );
58
59 const PermissionSelectComponent = ({ input }: WrappedFieldProps) =>
60     <PermissionSelect fullWidth disableUnderline {...input} />;