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

177 lines
4.1 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";
2018-08-23 14:14:49 +02:00
import { translate } from "react-i18next";
2018-08-28 14:29:45 +02:00
import {
modifyPermission,
isModifyPermissionPending,
2018-08-30 11:33:40 +02:00
deletePermission,
isDeletePermissionPending
2018-08-28 14:29:45 +02:00
} from "../modules/permissions";
import { connect } from "react-redux";
2018-08-28 13:39:32 +02:00
import type { History } from "history";
2018-09-11 16:00:15 +02:00
import { Checkbox } from "../../../../../scm-ui-components/packages/ui-components/src/index";
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,
repoName: string,
2018-08-28 13:39:32 +02:00
match: any,
2018-08-28 14:29:45 +02:00
history: History,
loading: boolean,
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;
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,
this.props.repoName
2018-08-30 09:49:36 +02:00
);
};
2018-08-23 14:14:49 +02:00
render() {
const { permission } = this.state;
const { loading, namespace, repoName } = this.props;
const typeSelector =
this.props.permission._links && this.props.permission._links.update ? (
<td>
<TypeSelector
handleTypeChange={this.handleTypeChange}
type={permission.type ? permission.type : "READ"}
loading={loading}
/>
</td>
) : (
<td>{permission.type}</td>
);
2018-08-28 14:29:45 +02:00
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}
repoName={repoName}
2018-08-30 09:49:36 +02:00
deletePermission={this.deletePermission}
2018-08-30 11:33:40 +02:00
loading={this.props.deleteLoading}
2018-08-30 09:49:36 +02:00
/>
</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.repoName
2018-08-28 13:39:32 +02:00
);
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.repoName,
2018-08-28 14:29:45 +02:00
permission.name
);
2018-08-30 11:33:40 +02:00
const deleteLoading = isDeletePermissionPending(
2018-08-28 14:29:45 +02:00
state,
ownProps.namespace,
ownProps.repoName,
2018-08-28 14:29:45 +02:00
permission.name
);
return { loading, 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,
repoName: string
2018-08-28 13:39:32 +02:00
) => {
dispatch(modifyPermission(permission, namespace, repoName));
2018-08-28 14:29:45 +02:00
},
2018-08-30 09:49:36 +02:00
deletePermission: (
permission: Permission,
namespace: string,
repoName: string
2018-08-30 09:49:36 +02:00
) => {
dispatch(deletePermission(permission, namespace, repoName));
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")(SinglePermission));