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

207 lines
4.8 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";
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";
2018-08-28 14:29:45 +02:00
import {
modifyPermission,
isModifyPermissionPending,
getModifyPermissionFailure,
2018-08-30 09:49:36 +02:00
modifyPermissionReset,
deletePermission
2018-08-28 14:29:45 +02:00
} from "../modules/permissions";
2018-08-28 13:39:32 +02:00
import connect from "react-redux/es/connect/connect";
import { withRouter } from "react-router-dom";
import type { History } from "history";
2018-08-28 14:29:45 +02:00
import ErrorNotification from "../../components/ErrorNotification";
2018-08-30 09:49:36 +02:00
import DeletePermissionButton from "../components/buttons/DeletePermissionButton";
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,
2018-08-28 14:29:45 +02:00
history: History,
loading: boolean,
error: Error,
2018-08-30 09:49:36 +02:00
permissionReset: (string, string, string) => void,
deletePermission: (Permission, string, string, (void) => void) => void
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;
2018-08-28 14:29:45 +02:00
this.props.permissionReset(
this.props.namespace,
this.props.name,
permission.name
);
2018-08-23 14:14:49 +02:00
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-30 09:49:36 +02:00
permissionDeleted = () => {
this.props.history.push(
"/repo/" + this.props.namespace + "/" + this.props.name + "/permissions"
);
};
deletePermission = () => {
this.props.deletePermission(
this.props.permission,
this.props.namespace,
this.props.name,
this.permissionDeleted
);
};
2018-08-23 14:14:49 +02:00
render() {
const { permission } = this.state;
2018-08-30 09:49:36 +02:00
const { t, loading, error, namespace, name } = this.props;
const types = ["READ", "OWNER", "WRITE"];
2018-08-23 14:14:49 +02:00
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)}
2018-08-28 14:29:45 +02:00
loading={loading}
2018-08-28 13:47:50 +02:00
/>
</td>
) : (
<td>{permission.type}</td>
);
2018-08-28 14:29:45 +02:00
const errorNotification = error ? (
<ErrorNotification error={error} />
) : null;
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}
<td>
2018-08-30 09:49:36 +02:00
<DeletePermissionButton
permission={permission}
namespace={namespace}
name={name}
deletePermission={this.deletePermission}
/>
{errorNotification}
</td>
2018-08-23 14:14:49 +02:00
</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 14:29:45 +02:00
const mapStateToProps = (state, ownProps) => {
const permission = ownProps.permission;
const loading = isModifyPermissionPending(
state,
ownProps.namespace,
ownProps.name,
permission.name
);
const error = getModifyPermissionFailure(
state,
ownProps.namespace,
ownProps.name,
permission.name
);
return { loading, error };
};
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-28 14:29:45 +02:00
},
permissionReset: (
namespace: string,
name: string,
permissionname: string
) => {
dispatch(modifyPermissionReset(namespace, name, permissionname));
2018-08-30 09:49:36 +02:00
},
deletePermission: (
permission: Permission,
namespace: string,
name: string,
callback: () => void
) => {
dispatch(deletePermission(permission, namespace, name, callback));
2018-08-28 13:39:32 +02:00
}
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)));