2018-08-23 14:14:49 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
2018-08-28 13:39:32 +02:00
|
|
|
import type { Permission } from "../types/Permissions";
|
|
|
|
|
import { Checkbox } from "../../components/forms/index";
|
|
|
|
|
import { DeleteButton } from "../../components/buttons/index";
|
2018-08-23 14:14:49 +02:00
|
|
|
import { translate } from "react-i18next";
|
2018-08-28 13:39:32 +02:00
|
|
|
import { Select } from "../../components/forms/index";
|
|
|
|
|
import { modifyPermission } from "../modules/permissions";
|
|
|
|
|
import connect from "react-redux/es/connect/connect";
|
|
|
|
|
import { withRouter } from "react-router-dom";
|
|
|
|
|
import type { History } from "history";
|
2018-08-23 14:14:49 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-08-23 15:39:09 +02:00
|
|
|
submitForm: Permission => void,
|
2018-08-28 13:39:32 +02:00
|
|
|
modifyPermission: (Permission, string, string) => void,
|
2018-08-23 14:14:49 +02:00
|
|
|
permission: Permission,
|
2018-08-28 13:39:32 +02:00
|
|
|
t: string => string,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string,
|
|
|
|
|
match: any,
|
|
|
|
|
history: History
|
2018-08-23 14:14:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2018-08-23 15:39:09 +02:00
|
|
|
permission: Permission
|
2018-08-23 14:14:49 +02:00
|
|
|
};
|
|
|
|
|
|
2018-08-28 13:39:32 +02:00
|
|
|
class SinglePermission extends React.Component<Props, State> {
|
2018-08-23 14:14:49 +02:00
|
|
|
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-28 13:39:32 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2018-08-28 13:47:50 +02:00
|
|
|
const typeSelector = this.props.permission._links.update ? (
|
|
|
|
|
<td>
|
|
|
|
|
<Select
|
|
|
|
|
onChange={this.handleTypeChange}
|
|
|
|
|
value={permission.type ? permission.type : ""}
|
|
|
|
|
options={this.createSelectOptions(types)}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
|
|
|
|
) : (
|
|
|
|
|
<td>{permission.type}</td>
|
|
|
|
|
);
|
|
|
|
|
|
2018-08-23 14:14:49 +02:00
|
|
|
return (
|
|
|
|
|
<tr>
|
2018-08-28 13:39:32 +02:00
|
|
|
<td>{permission.name}</td>
|
2018-08-23 14:14:49 +02:00
|
|
|
<td>
|
2018-08-28 13:39:32 +02:00
|
|
|
<Checkbox checked={permission ? permission.groupPermission : false} />
|
2018-08-23 14:14:49 +02:00
|
|
|
</td>
|
2018-08-28 13:47:50 +02:00
|
|
|
{typeSelector}
|
2018-08-23 14:14:49 +02:00
|
|
|
<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
|
|
|
});
|
2018-08-28 13:39:32 +02:00
|
|
|
this.modifyPermission(type);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
modifyPermission = (type: string) => {
|
|
|
|
|
let permission = this.state.permission;
|
|
|
|
|
permission.type = type;
|
|
|
|
|
this.props.modifyPermission(
|
|
|
|
|
permission,
|
|
|
|
|
this.props.namespace,
|
|
|
|
|
this.props.name
|
|
|
|
|
);
|
2018-08-23 14:14:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createSelectOptions(types: string[]) {
|
|
|
|
|
return types.map(type => {
|
|
|
|
|
return {
|
|
|
|
|
label: type,
|
|
|
|
|
value: type
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-08-28 13:39:32 +02:00
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
|
2018-08-28 13:39:32 +02:00
|
|
|
const mapStateToProps = (state, ownProps) => {};
|
2018-08-23 14:14:49 +02:00
|
|
|
|
2018-08-28 13:39:32 +02:00
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
|
|
|
|
modifyPermission: (
|
|
|
|
|
permission: Permission,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) => {
|
|
|
|
|
dispatch(modifyPermission(permission, namespace, name));
|
|
|
|
|
}
|
2018-08-23 14:14:49 +02:00
|
|
|
};
|
2018-08-28 13:39:32 +02:00
|
|
|
};
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)(translate("permissions")(withRouter(SinglePermission)));
|