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

177 lines
3.9 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-16 13:36:11 +02:00
import type { RepositoryRole } from "@scm-manager/ui-types";
2019-05-15 02:04:54 +02:00
import { InputField, SubmitButton } from "@scm-manager/ui-components";
import PermissionCheckbox from "../../../repos/permissions/components/PermissionCheckbox";
import {
fetchAvailableVerbs,
getFetchVerbsFailure,
getVerbsFromState,
isFetchVerbsPending
} from "../modules/roles";
2019-05-16 13:36:11 +02:00
import {
getRepositoryRolesLink,
getRepositoryVerbsLink
} from "../../../modules/indexResource";
type Props = {
2019-05-16 13:36:11 +02:00
role?: RepositoryRole,
loading?: boolean,
nameDisabled: boolean,
availableVerbs: string[],
verbsLink: string,
2019-05-16 13:36:11 +02:00
submitForm: RepositoryRole => void,
2019-05-15 02:04:54 +02:00
// context objects
t: string => string,
// dispatch functions
2019-05-15 14:48:12 +02:00
fetchAvailableVerbs: (link: string) => void
};
type State = {
2019-05-16 13:36:11 +02:00
role: RepositoryRole
};
class RepositoryRoleForm 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-16 13:36:11 +02:00
const { fetchAvailableVerbs, verbsLink } = this.props;
fetchAvailableVerbs(verbsLink);
2019-05-15 17:30:42 +02:00
if (this.props.role) {
2019-05-16 13:36:11 +02:00
this.setState({ role: this.props.role });
2019-05-15 17:30:42 +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.length > 0)
2019-05-15 02:04:54 +02:00
);
};
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()) {
2019-05-16 13:36:11 +02:00
this.props.submitForm(this.state.role);
}
};
render() {
const { loading, availableVerbs, nameDisabled, t } = this.props;
const { role } = this.state;
const verbSelectBoxes = !availableVerbs
? null
: availableVerbs.map(verb => (
<PermissionCheckbox
key={verb}
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"
label={t("repositoryRole.create.name")}
2019-05-15 02:04:54 +02:00
onChange={this.handleNameChange}
value={role.name ? role.name : ""}
disabled={nameDisabled}
2019-05-15 02:04:54 +02:00
/>
</div>
2019-05-15 02:04:54 +02:00
</div>
<>{verbSelectBoxes}</>
<hr />
2019-05-15 02:04:54 +02:00
<div className="columns">
<div className="column">
<SubmitButton
loading={loading}
label={t("repositoryRole.form.submit")}
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);
const repositoryRolesLink = getRepositoryRolesLink(state);
return {
loading,
error,
verbsLink,
availableVerbs,
repositoryRolesLink
};
};
const mapDispatchToProps = dispatch => {
return {
fetchAvailableVerbs: (link: string) => {
dispatch(fetchAvailableVerbs(link));
2019-05-16 13:36:11 +02:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("config")(RepositoryRoleForm));