-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
176 lines (157 loc) · 5.48 KB
/
Copy pathindex.tsx
File metadata and controls
176 lines (157 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import './index.scss';
import { PERMISSIONS, schema as schemaDefn } from '@bcgsc-pori/graphkb-schema';
import {
Checkbox,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
} from '@mui/material';
import React, { useCallback, useState } from 'react';
import { FormContextState } from '@/components/FormContext';
/* eslint-disable no-bitwise */
/**
* For a mapping of class names to the single permissions value, split these by operation types
* for display as checkboxes
*/
const splitPermissionsByOperation = (permissions: Record<string, number>) => {
const permByModelName: Record<string, {
READ: number | null;
CREATE: number | null;
UPDATE: number | null;
DELETE: number | null;
}> = {};
Object.keys(schemaDefn.models).forEach((modelName) => {
const model = schemaDefn.get(modelName);
if (!model.embedded) {
const value = permissions[model.name] === undefined
? PERMISSIONS.NONE
: permissions[model.name];
permByModelName[modelName] = {
READ: value & PERMISSIONS.READ,
CREATE: null,
UPDATE: null,
DELETE: null,
};
if (!model.isAbstract) {
permByModelName[modelName].CREATE = value & PERMISSIONS.CREATE;
permByModelName[modelName].DELETE = value & PERMISSIONS.DELETE;
if (!model.isEdge) {
permByModelName[modelName].UPDATE = value & PERMISSIONS.UPDATE;
}
}
}
});
return permByModelName;
};
interface PermissionsTableProps {
/** field name to use in simulating events */
name: string;
/** handler to propogate changes to the parent form */
onChange: FormContextState['updateFieldEvent'];
/** flag to indicate this field cannot be edited */
disabled?: boolean;
/** the current permissions set */
value?: Record<string, number>;
}
/**
* Table to display permissions state for a certain user group.
*/
const PermissionsTable = ({
value = {}, disabled = false, onChange, name,
}: PermissionsTableProps) => {
const [content, setContent] = useState<Record<string, number>>(value || {});
const [topBoxes, setTopboxes] = useState({});
/**
* Handle the user clicking a checkbox to either clear or check it
* when the modelName is not given and is null, assumes a checkAll event
*/
const handleClick = useCallback((operation, currModelName: string | null = null) => {
const newContent = { ...content };
const newTopBoxes = { ...topBoxes };
if (!currModelName) {
newTopBoxes[operation] = !newTopBoxes[operation];
}
for (const modelName of Object.keys(content)) { // eslint-disable-line no-restricted-syntax
if (!schemaDefn.has(modelName) || content[modelName] === null) {
continue; // eslint-disable-line no-continue
}
if (currModelName) {
const checked = (content[modelName] & PERMISSIONS[operation]) !== 0;
// update an operation for a single row
if (modelName === currModelName) {
if (checked) {
newContent[modelName] &= ~PERMISSIONS[operation]; // remove permissions for this operation
} else {
newContent[modelName] |= PERMISSIONS[operation];
}
}
} else if (!newTopBoxes[operation]) {
// clear all
newContent[modelName] &= ~PERMISSIONS[operation];
topBoxes[operation] = false;
} else {
// check all
topBoxes[operation] = true;
newContent[modelName] |= PERMISSIONS[operation];
}
}
setContent(newContent);
setTopboxes(newTopBoxes);
onChange({ target: { name, value: newContent } });
}, [content, name, onChange, topBoxes]);
const operationOrder = ['CREATE', 'READ', 'UPDATE', 'DELETE'];
const permByModelName = splitPermissionsByOperation(content || {});
const modelOrder = Object.keys(permByModelName).sort();
return (
<div className="permissions-table">
<Table>
<TableHead>
<TableRow className="permissions-table__header">
<TableCell size="small" />
{operationOrder.map((operation) => (
<TableCell key={operation} padding="checkbox">
{operation}
</TableCell>
))}
</TableRow>
<TableRow>
<TableCell size="small" />
{operationOrder.map((operation) => (
<TableCell key={operation} padding="checkbox">
<Checkbox
checked={topBoxes[operation]}
disabled={disabled}
onChange={() => handleClick(operation, null)}
/>
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{modelOrder.map((modelName) => {
const permission = permByModelName[modelName];
return (
<TableRow key={modelName} className="permissions-table__row">
<TableCell size="small">{modelName}:</TableCell>
{operationOrder.map((operation) => (
<TableCell key={operation} padding="checkbox">
{permission[operation] !== null && (
<Checkbox
checked={permission[operation] !== 0}
disabled={disabled}
onChange={() => handleClick(operation, modelName)}
/>
)}
</TableCell>
))}
</TableRow>
);
})}
</TableBody>
</Table>
</div>
);
};
export default PermissionsTable;