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

217 lines
5.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";
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,
2018-08-30 11:33:40 +02:00
deletePermission,
getDeletePermissionFailure,
isDeletePermissionPending
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-30 11:33:40 +02:00
import TypeSelector from "../components/TypeSelector";
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,
2018-08-30 11:33:40 +02:00
deletePermission: (Permission, string, string) => void,
deleteLoading: boolean
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
deletePermission = () => {
this.props.deletePermission(
this.props.permission,
this.props.namespace,
2018-08-30 11:33:40 +02:00
this.props.name
2018-08-30 09:49:36 +02:00
);
};
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>
2018-08-30 11:33:40 +02:00
<TypeSelector
handleTypeChange={this.handleTypeChange}
type={permission.type ? permission.type : "READ"}
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}
2018-08-30 11:33:40 +02:00
loading={this.props.deleteLoading}
2018-08-30 09:49:36 +02:00
/>
{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;
2018-08-30 11:33:40 +02:00
console.log(permission);
2018-08-28 14:29:45 +02:00
const loading = isModifyPermissionPending(
state,
ownProps.namespace,
ownProps.name,
permission.name
);
2018-08-30 11:33:40 +02:00
const error =
getModifyPermissionFailure(
state,
ownProps.namespace,
ownProps.name,
permission.name
) ||
getDeletePermissionFailure(
state,
ownProps.namespace,
ownProps.name,
permission.name
);
const deleteLoading = isDeletePermissionPending(
2018-08-28 14:29:45 +02:00
state,
ownProps.namespace,
ownProps.name,
permission.name
);
2018-08-30 11:33:40 +02:00
return { loading, error, deleteLoading };
2018-08-28 14:29:45 +02:00
};
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,
2018-08-30 11:33:40 +02:00
name: string
2018-08-30 09:49:36 +02:00
) => {
2018-08-30 11:33:40 +02:00
dispatch(deletePermission(permission, namespace, name));
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)));