Merge branch '15027-object-update-fix'
[arvados-workbench2.git] / src / views-components / sharing-dialog / permission-select.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 { MenuItem, Select } from '@material-ui/core';
7 import RemoveRedEye from '@material-ui/icons/RemoveRedEye';
8 import Edit from '@material-ui/icons/Edit';
9 import Computer from '@material-ui/icons/Computer';
10 import { SelectProps } from '@material-ui/core/Select';
11 import { SelectItem } from './select-item';
12 import { PermissionLevel } from '../../models/permission';
13
14 export enum PermissionSelectValue {
15     READ = 'Read',
16     WRITE = 'Write',
17     MANAGE = 'Manage',
18 }
19
20 export const parsePermissionLevel = (value: PermissionSelectValue) => {
21     switch (value) {
22         case PermissionSelectValue.READ:
23             return PermissionLevel.CAN_READ;
24         case PermissionSelectValue.WRITE:
25             return PermissionLevel.CAN_WRITE;
26         case PermissionSelectValue.MANAGE:
27             return PermissionLevel.CAN_MANAGE;
28         default:
29             return PermissionLevel.NONE;
30     }
31 };
32
33 export const formatPermissionLevel = (value: PermissionLevel) => {
34     switch (value) {
35         case PermissionLevel.CAN_READ:
36             return PermissionSelectValue.READ;
37         case PermissionLevel.CAN_WRITE:
38             return PermissionSelectValue.WRITE;
39         case PermissionLevel.CAN_MANAGE:
40             return PermissionSelectValue.MANAGE;
41         default:
42             return PermissionSelectValue.READ;
43     }
44 };
45
46
47 export const PermissionSelect = (props: SelectProps) =>
48     <Select
49         {...props}
50         renderValue={renderPermissionItem}>
51         <MenuItem value={PermissionSelectValue.READ}>
52             {renderPermissionItem(PermissionSelectValue.READ)}
53         </MenuItem>
54         <MenuItem value={PermissionSelectValue.WRITE}>
55             {renderPermissionItem(PermissionSelectValue.WRITE)}
56         </MenuItem>
57         <MenuItem value={PermissionSelectValue.MANAGE}>
58             {renderPermissionItem(PermissionSelectValue.MANAGE)}
59         </MenuItem>
60     </Select>;
61
62 const renderPermissionItem = (value: string) =>
63     <SelectItem {...{ value, icon: getIcon(value) }} />;
64
65 const getIcon = (value: string) => {
66     switch (value) {
67         case PermissionSelectValue.READ:
68             return RemoveRedEye;
69         case PermissionSelectValue.WRITE:
70             return Edit;
71         case PermissionSelectValue.MANAGE:
72             return Computer;
73         default:
74             return Computer;
75     }
76 };