2018-08-23 14:14:49 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import type { Permission } from "../../types/Permissions";
|
|
|
|
|
import { Checkbox, InputField } from "../../../components/forms/index";
|
|
|
|
|
import { DeleteButton, SubmitButton } from "../../../components/buttons/index";
|
|
|
|
|
import { translate } from "react-i18next";
|
2018-08-23 15:39:09 +02:00
|
|
|
import { Select } from "../../../components/forms/index";
|
2018-08-23 14:14:49 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-08-23 15:39:09 +02:00
|
|
|
submitForm: Permission => void,
|
2018-08-23 14:14:49 +02:00
|
|
|
permission: Permission,
|
|
|
|
|
t: string => string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: Permission
|
2018-08-23 14:14:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PermissionRowEditable extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: {
|
|
|
|
|
name: "",
|
|
|
|
|
type: "READ",
|
|
|
|
|
groupPermission: false,
|
|
|
|
|
_links: {}
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
const { permission } = this.props;
|
|
|
|
|
if (permission) {
|
|
|
|
|
this.setState({
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: {
|
|
|
|
|
name: permission.name,
|
|
|
|
|
type: permission.type,
|
|
|
|
|
groupPermission: permission.groupPermission,
|
|
|
|
|
_links: permission._links
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-23 15:39:09 +02:00
|
|
|
submit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.props.submitForm(this.state.permission);
|
|
|
|
|
};
|
2018-08-23 14:14:49 +02:00
|
|
|
render() {
|
2018-08-23 15:39:09 +02:00
|
|
|
const { permission } = this.state;
|
2018-08-23 14:14:49 +02:00
|
|
|
const { t } = this.props;
|
|
|
|
|
const types = ["READ", "OWNER", "GROUP"];
|
|
|
|
|
|
|
|
|
|
const deleteButton = this.props.permission._links.delete ? (
|
|
|
|
|
<DeleteButton label={t("edit-permission.delete-button")} />
|
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<tr>
|
|
|
|
|
<td>
|
|
|
|
|
<InputField
|
2018-08-23 15:39:09 +02:00
|
|
|
value={permission.name ? permission.name : ""}
|
2018-08-23 14:14:49 +02:00
|
|
|
onChange={this.handleNameChange}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
|
|
|
|
<td className="is-hidden-mobile">
|
|
|
|
|
<Select
|
|
|
|
|
onChange={this.handleTypeChange}
|
2018-08-23 15:39:09 +02:00
|
|
|
value={permission.type ? permission.type : ""}
|
2018-08-23 14:14:49 +02:00
|
|
|
options={this.createSelectOptions(types)}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
|
|
|
|
<Checkbox
|
2018-08-23 15:39:09 +02:00
|
|
|
checked={
|
|
|
|
|
permission.groupPermission ? permission.groupPermission : false
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
onChange={this.handleGroupPermissionChange}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
|
|
|
|
<td>
|
2018-08-23 15:39:09 +02:00
|
|
|
<SubmitButton
|
|
|
|
|
label={t("edit-permission.save-button")}
|
|
|
|
|
action={this.submit}
|
|
|
|
|
/>
|
2018-08-23 14:14:49 +02:00
|
|
|
</td>
|
|
|
|
|
<td>{deleteButton}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleTypeChange = (type: string) => {
|
|
|
|
|
this.setState({
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: {
|
|
|
|
|
...this.state.permission,
|
|
|
|
|
type: type
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createSelectOptions(types: string[]) {
|
|
|
|
|
return types.map(type => {
|
|
|
|
|
return {
|
|
|
|
|
label: type,
|
|
|
|
|
value: type
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleNameChange = (name: string) => {
|
|
|
|
|
this.setState({
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: {
|
|
|
|
|
...this.state.permission,
|
|
|
|
|
name: name
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleGroupPermissionChange = (groupPermission: boolean) => {
|
|
|
|
|
this.setState({
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: {
|
|
|
|
|
...this.state.permission,
|
|
|
|
|
groupPermission: groupPermission
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default translate("permissions")(PermissionRowEditable);
|