Files
SCM-Manager/scm-ui/src/config/containers/GlobalPermissionRoleForm.js

180 lines
3.8 KiB
JavaScript
Raw Normal View History

// @flow
import React from "react";
2019-05-15 02:04:54 +02:00
import { connect } from "react-redux";
import { translate } from "react-i18next";
2019-05-15 02:04:54 +02:00
import type { Role } from "@scm-manager/ui-types";
import { InputField, SubmitButton } from "@scm-manager/ui-components";
import PermissionCheckbox from "../../repos/permissions/components/PermissionCheckbox";
import {
fetchAvailableVerbs,
getFetchVerbsFailure,
getVerbsFromState,
isFetchVerbsPending
} from "../modules/roles";
import { getRepositoryVerbsLink } from "../../modules/indexResource";
type Props = {
2019-05-15 02:04:54 +02:00
role?: Role,
loading?: boolean,
disabled: boolean,
availableVerbs: string[],
verbsLink: string,
2019-05-15 02:04:54 +02:00
// context objects
t: string => string,
// dispatch functions
fetchAvailableVerbs: (link: string) => void
};
type State = {
role: Role
};
class GlobalPermissionRoleForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
2019-05-15 02:04:54 +02:00
role: {
name: "",
verbs: [],
system: false,
_links: {}
}
};
}
componentDidMount() {
2019-05-15 02:04:54 +02:00
const { fetchAvailableVerbs, verbsLink, role } = this.props;
fetchAvailableVerbs(verbsLink);
2019-05-15 02:04:54 +02:00
if (role) {
this.setState({
role: {
...role,
role: { verbs: role.verbs }
}
});
2019-05-15 02:04:54 +02:00
}
}
isFalsy(value) {
return !value;
}
2019-05-15 02:04:54 +02:00
isValid = () => {
const { role } = this.state;
return !(
this.isFalsy(role) ||
this.isFalsy(role.name) ||
this.isFalsy(role.verbs)
);
};
handleNameChange = (name: string) => {
this.setState({
2019-05-15 02:04:54 +02:00
role: {
...this.state.role,
name
}
});
};
handleVerbChange = (value: boolean, name: string) => {
const { role } = this.state;
const newVerbs = value
? [...role.verbs, name]
: role.verbs.filter(v => v !== name);
this.setState({
...this.state,
role: {
...role,
verbs: newVerbs
}
});
};
submit = (event: Event) => {
event.preventDefault();
if (this.isValid()) {
// this.props.submitForm(this.state.role);
//TODO ADD createRole here
}
};
render() {
const { loading, availableVerbs, disabled, t } = this.props;
const { role } = this.state;
const verbSelectBoxes = !availableVerbs
? null
: availableVerbs.map(verb => (
<PermissionCheckbox
key={verb}
// disabled={readOnly}
name={verb}
checked={role.verbs.includes(verb)}
onChange={this.handleVerbChange}
/>
));
return (
2019-05-15 02:04:54 +02:00
<form onSubmit={this.submit}>
<div className="columns">
<div className="column">
<InputField
name="name"
2019-05-15 10:42:18 +02:00
label={t("roles.form.name")}
2019-05-15 02:04:54 +02:00
onChange={this.handleNameChange}
value={role.name ? role.name : ""}
disabled={!!role.name || disabled}
2019-05-15 02:04:54 +02:00
/>
</div>
2019-05-15 02:04:54 +02:00
</div>
2019-05-15 10:42:18 +02:00
{verbSelectBoxes}
<hr />
2019-05-15 02:04:54 +02:00
<div className="columns">
<div className="column">
<SubmitButton
loading={loading}
2019-05-15 10:42:18 +02:00
label={t("roles.form.submit")}
disabled={disabled || !this.isValid()}
2019-05-15 02:04:54 +02:00
/>
</div>
2019-05-15 02:04:54 +02:00
</div>
</form>
);
}
}
const mapStateToProps = (state, ownProps) => {
const loading = isFetchVerbsPending(state);
const error = getFetchVerbsFailure(state);
const verbsLink = getRepositoryVerbsLink(state);
const availableVerbs = getVerbsFromState(state);
return {
loading,
error,
verbsLink,
availableVerbs
};
};
const mapDispatchToProps = dispatch => {
return {
fetchAvailableVerbs: (link: string) => {
dispatch(fetchAvailableVerbs(link));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2019-05-15 10:42:18 +02:00
)(translate("config")(GlobalPermissionRoleForm));