Files
SCM-Manager/scm-ui/src/permissions/containers/SinglePermission.js

130 lines
3.0 KiB
JavaScript
Raw Normal View History

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 = {
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 = {
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 = {
permission: {
name: "",
type: "READ",
groupPermission: false,
_links: {}
}
2018-08-23 14:14:49 +02:00
};
}
componentDidMount() {
const { permission } = this.props;
if (permission) {
this.setState({
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() {
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>
2018-08-28 13:39:32 +02:00
<td>{permission.name}</td>
2018-08-23 14:14:49 +02:00
<td className="is-hidden-mobile">
<Select
onChange={this.handleTypeChange}
value={permission.type ? permission.type : ""}
2018-08-23 14:14:49 +02:00
options={this.createSelectOptions(types)}
/>
</td>
<td>
2018-08-28 13:39:32 +02:00
<Checkbox checked={permission ? permission.groupPermission : false} />
2018-08-23 14:14:49 +02:00
</td>
<td>{deleteButton}</td>
</tr>
);
}
handleTypeChange = (type: string) => {
this.setState({
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)));