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