Skip to content

Commit 16b82c0

Browse files
author
Matthew Grill
committed
Permissions
1 parent 3e9343e commit 16b82c0

File tree

2 files changed

+152
-13
lines changed

2 files changed

+152
-13
lines changed
Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,98 @@
1-
import React from 'react';
2-
import { css } from 'emotion';
1+
import React, { Fragment, Component } from 'react';
2+
import Loading from '../../Helpers/Loading';
33

4-
const styles = {
5-
title: css`
6-
text-decoration: underline;
7-
`,
4+
const Permissions = class Permissions extends Component {
5+
state = {
6+
loaded: false,
7+
};
8+
componentDidMount() {
9+
Promise.all([
10+
fetch(
11+
`${process.env.REACT_APP_DRUPAL_BASE_URL}/permissions?_format=json`,
12+
).then(res => res.json()),
13+
fetch(
14+
`${process.env.REACT_APP_DRUPAL_BASE_URL}/jsonapi/user_role/user_role`,
15+
).then(res => res.json()),
16+
])
17+
.then(([permissions, { data: roles }]) =>
18+
this.setState({ permissions, roles, loaded: true }),
19+
)
20+
.catch(err => this.setState({ loaded: false, err }));
21+
}
22+
groupPermissions = permissions =>
23+
Object.entries(
24+
Object.keys(permissions)
25+
.map(key => permissions[key])
26+
.reduce((acc, cur) => {
27+
acc[cur.provider] = acc[cur.provider] || [];
28+
acc[cur.provider].push(cur);
29+
return acc;
30+
}, {}),
31+
);
32+
render() {
33+
return (
34+
<Fragment>
35+
{!this.state.loaded ? (
36+
<Loading />
37+
) : (
38+
<table>
39+
<thead>
40+
<tr>
41+
{[
42+
'PERMISSION',
43+
...this.state.roles.map(role =>
44+
role.attributes.label.toUpperCase(),
45+
),
46+
].map(label => <td key={`column-${label}`}>{label}</td>)}
47+
</tr>
48+
</thead>
49+
<tbody>
50+
{this.groupPermissions(this.state.permissions).map(
51+
([permissionGroupName, permissions]) =>
52+
permissions.length && (
53+
<Fragment key={`fragment-${permissionGroupName}`}>
54+
<tr key={`permissionGroup-${permissionGroupName}`}>
55+
<td colSpan={this.state.roles.length + 1}>
56+
<b>{permissionGroupName}</b>
57+
</td>
58+
</tr>
59+
{permissions.map(permission => (
60+
<tr
61+
key={`permissionGroup-${permissionGroupName}-${
62+
permission.title
63+
}`}
64+
>
65+
<td>{permission.title}</td>
66+
{this.state.roles.map(({ attributes }) => (
67+
<td
68+
key={`role-${attributes.id}-permission-${
69+
permission.id
70+
}`}
71+
>
72+
{attributes.is_admin &&
73+
attributes.id === 'administrator' ? (
74+
<input type="checkbox" checked />
75+
) : (
76+
<input
77+
type="checkbox"
78+
checked={attributes.permissions.includes(
79+
permission.id,
80+
)}
81+
/>
82+
)}
83+
</td>
84+
))}
85+
</tr>
86+
))}
87+
</Fragment>
88+
),
89+
)}
90+
</tbody>
91+
</table>
92+
)}
93+
</Fragment>
94+
);
95+
}
896
};
997

10-
const Permissions = () => (
11-
<div>
12-
<h1 className={styles.title}>Permissions</h1>
13-
<p>This will be the permissions page.</p>
14-
</div>
15-
);
16-
1798
export default Permissions;

src/components/Helpers/Loading.jsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { css } from 'emotion';
3+
4+
const spinner = css`
5+
margin: 100px auto 0;
6+
width: 70px;
7+
text-align: center;
8+
> div {
9+
width: 10px;
10+
height: 10px;
11+
background-color: #333;
12+
13+
border-radius: 100%;
14+
display: inline-block;
15+
-webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
16+
animation: sk-bouncedelay 1.4s infinite ease-in-out both;
17+
}
18+
.bounce1 {
19+
-webkit-animation-delay: -0.32s;
20+
animation-delay: -0.32s;
21+
}
22+
.bounce2 {
23+
-webkit-animation-delay: -0.16s;
24+
animation-delay: -0.16s;
25+
}
26+
@-webkit-keyframes sk-bouncedelay {
27+
0%,
28+
80%,
29+
100% {
30+
-webkit-transform: scale(0);
31+
}
32+
40% {
33+
-webkit-transform: scale(1);
34+
}
35+
}
36+
@keyframes sk-bouncedelay {
37+
0%,
38+
80%,
39+
100% {
40+
-webkit-transform: scale(0);
41+
transform: scale(0);
42+
}
43+
40% {
44+
-webkit-transform: scale(1);
45+
transform: scale(1);
46+
}
47+
}
48+
`;
49+
50+
const Loading = () => (
51+
<div className={spinner}>
52+
<div className="bounce1" />
53+
<div className="bounce2" />
54+
<div className="bounce3" />
55+
</div>
56+
);
57+
58+
export default Loading;

0 commit comments

Comments
 (0)