Skip to content

Commit

Permalink
Merge pull request #145 from alan-turing-institute/create-group-extra
Browse files Browse the repository at this point in the history
Create group button in permission manager
  • Loading branch information
nbarlowATI committed May 30, 2022
2 parents ddacf97 + 43ab2dd commit e92506a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 59 deletions.
28 changes: 15 additions & 13 deletions frontend/src/components/CaseContainer.js
Expand Up @@ -50,19 +50,21 @@ class CaseContainer extends Component {
},
};
const res = await fetch(this.url + id, requestOptions);
const json_response = await res.json();
if (
JSON.stringify(this.state.assurance_case) !==
JSON.stringify(json_response)
) {
this.setState({ loading: true });
this.setState({
assurance_case: json_response,
});
this.setState({
mermaid_md: jsonToMermaid(this.state.assurance_case),
});
this.setState({ loading: false });
if (res.status === 200) {
const json_response = await res.json();
if (
JSON.stringify(this.state.assurance_case) !==
JSON.stringify(json_response)
) {
this.setState({ loading: true });
this.setState({
assurance_case: json_response,
});
this.setState({
mermaid_md: jsonToMermaid(this.state.assurance_case),
});
this.setState({ loading: false });
}
}
};

Expand Down
29 changes: 20 additions & 9 deletions frontend/src/components/CasePermissionsManager.js
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { getBaseURL } from "./utils.js";
import { Box, Button, Form, Heading, Text } from "grommet";
import CreateGroup from "./CreateGroup.js";
import PermissionSelector from "./PermissionSelector.js";
import { removeArrayElement } from "./utils.js";

Expand All @@ -16,7 +17,7 @@ class CasePermissionsManager extends Component {
};
}

componentDidMount() {
getGroups() {
const requestOptions = {
headers: {
Authorization: `Token ${localStorage.getItem("token")}`,
Expand All @@ -35,6 +36,10 @@ class CasePermissionsManager extends Component {
});
}

componentDidMount() {
this.getGroups();
}

dialValue(group, assurance_case) {
if (group.editable_cases.includes(assurance_case.id)) return "Edit";
if (group.viewable_cases.includes(assurance_case.id)) return "View";
Expand All @@ -52,15 +57,15 @@ class CasePermissionsManager extends Component {
}

renderGroupDials(group) {
const initialValue = this.getGroupPermission(group);
if (initialValue === undefined)
const value = this.getGroupPermission(group);
if (value === undefined)
return <Text key={group.id}>Loading permissions...</Text>;
return (
<Box key={group.id} direction="row">
<Text>{group.name}</Text>
<PermissionSelector
name={group.name}
initialValue={initialValue}
value={value}
setValue={(value) => this.setGroupPermission(group, value)}
/>
</Box>
Expand Down Expand Up @@ -104,13 +109,19 @@ class CasePermissionsManager extends Component {

render() {
return (
<Form onSubmit={this.onSubmit.bind(this)}>
<Heading level={3}>Group permissions</Heading>
<Box>
<Form onSubmit={this.onSubmit.bind(this)}>
<Heading level={3}>Group permissions</Heading>
<Box pad="medium" gap="small" fill>
{this.state.groups.map(this.renderGroupDials.bind(this))}
<Button type="submit" label="Submit" />
</Box>
</Form>
<Box pad="medium" gap="small" fill>
{this.state.groups.map(this.renderGroupDials.bind(this))}
<Button type="submit" label="Submit" />
<Heading level={4}>Create a new group</Heading>
<CreateGroup afterSubmit={this.getGroups.bind(this)} />
</Box>
</Form>
</Box>
);
}
}
Expand Down
50 changes: 50 additions & 0 deletions frontend/src/components/CreateGroup.js
@@ -0,0 +1,50 @@
import { Box, Button, Form, FormField, TextInput } from "grommet";
import React from "react";
import { useNavigate, useParams } from "react-router-dom";
import { getBaseURL } from "./utils.js";

class CreateGroup extends React.Component {
constructor(props) {
super(props);
this.state = {
newGroupName: "",
};
}

async submitCreateGroup() {
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ name: this.state.newGroupName }),
};

await fetch(`${getBaseURL()}/groups/`, requestOptions);
this.setState({ newGroupName: "" });
this.props.afterSubmit();
}

render() {
return (
<Form onSubmit={this.submitCreateGroup.bind(this)}>
<Box gap="small" direction="row">
<FormField margin={{ left: "small" }}>
<TextInput
plain={true}
value={this.state.newGroupName}
name="new-group-name"
onChange={(e) => this.setState({ newGroupName: e.target.value })}
/>
</FormField>
<Button type="submit" label="Create group" />
</Box>
</Form>
);
}
}

export default (props) => (
<CreateGroup {...props} params={useParams()} navigate={useNavigate()} />
);
32 changes: 2 additions & 30 deletions frontend/src/components/Groups.js
Expand Up @@ -15,6 +15,7 @@ import {
joinCommaSeparatedString,
splitCommaSeparatedString,
} from "./utils.js";
import CreateGroup from "./CreateGroup.js";

class Groups extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -42,21 +43,6 @@ class Groups extends React.Component {
await this.setState({ userList: users });
}

async submitCreateGroup() {
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ name: this.state.newGroupName }),
};

await fetch(`${getBaseURL()}/groups/`, requestOptions);
this.setState({ newGroupName: "" });
this.getGroups();
}

async getGroups() {
const response = await fetch(`${getBaseURL()}/groups/`, {
method: "GET",
Expand Down Expand Up @@ -219,21 +205,7 @@ class Groups extends React.Component {
<ul>
{this.state.ownerGroups.map(this.ownerGroupLine.bind(this))}
<li>
<Form onSubmit={this.submitCreateGroup.bind(this)}>
<Box gap="small" direction="row">
<FormField margin={{ left: "small" }}>
<TextInput
plain={true}
value={this.state.newGroupName}
name="new-group-name"
onChange={(e) =>
this.setState({ newGroupName: e.target.value })
}
/>
</FormField>
<Button type="submit" label="Create group" />
</Box>
</Form>
<CreateGroup afterSubmit={this.getGroups.bind(this)} />
</li>
</ul>
</Box>
Expand Down
12 changes: 5 additions & 7 deletions frontend/src/components/PermissionSelector.js
Expand Up @@ -5,14 +5,12 @@ import { Box, RadioButton } from "grommet";
class PermissionSelector extends Component {
constructor(props) {
super(props);
this.state = {
value: props.initialValue,
};
}

render() {
// Grommet also has an element called RadioButtonGroup, which should be perfect for
// this, but as of 2022-05-26 it's too buggy to use.
// this, but as of 2022-05-26 it's too buggy to use: Having more than one of them on
// the same page causes the onChange events of one to affect the others.
return (
<Box
key={this.props.name}
Expand All @@ -23,7 +21,7 @@ class PermissionSelector extends Component {
<RadioButton
name={this.props.name}
label="None"
checked={this.state.value === "None"}
checked={this.props.value === "None"}
onChange={(e) => {
if (e.target.checked) this.setState({ value: "None" });
this.props.setValue("None");
Expand All @@ -32,7 +30,7 @@ class PermissionSelector extends Component {
<RadioButton
name={this.props.name}
label="View"
checked={this.state.value === "View"}
checked={this.props.value === "View"}
onChange={(e) => {
if (e.target.checked) this.setState({ value: "View" });
this.props.setValue("View");
Expand All @@ -41,7 +39,7 @@ class PermissionSelector extends Component {
<RadioButton
name={this.props.name}
label="Edit"
checked={this.state.value === "Edit"}
checked={this.props.value === "Edit"}
onChange={(e) => {
if (e.target.checked) this.setState({ value: "Edit" });
this.props.setValue("Edit");
Expand Down

0 comments on commit e92506a

Please sign in to comment.