2019-04-03 13:12:07 +02:00
|
|
|
// @flow
|
2019-04-02 09:27:34 +02:00
|
|
|
import React from "react";
|
2019-04-03 13:12:07 +02:00
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import type { Repository, Branch } from "@scm-manager/ui-types";
|
|
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
InputField,
|
|
|
|
|
SubmitButton,
|
|
|
|
|
validation as validator
|
|
|
|
|
} from "@scm-manager/ui-components";
|
2019-04-03 17:16:55 +02:00
|
|
|
import { orderBranches } from "../util/orderBranches";
|
2019-04-02 09:27:34 +02:00
|
|
|
|
2019-04-03 13:12:07 +02:00
|
|
|
type Props = {
|
|
|
|
|
submitForm: Branch => void,
|
|
|
|
|
repository: Repository,
|
|
|
|
|
branches: Branch[],
|
|
|
|
|
loading?: boolean,
|
2019-04-04 11:04:39 +02:00
|
|
|
transmittedName?: string,
|
2019-04-03 13:12:07 +02:00
|
|
|
t: string => string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
|
source?: string,
|
|
|
|
|
name?: string,
|
|
|
|
|
nameValidationError: boolean
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BranchForm extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2019-04-04 13:03:51 +02:00
|
|
|
nameValidationError: false,
|
|
|
|
|
name: props.transmittedName
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 17:16:55 +02:00
|
|
|
isFalsy(value) {
|
|
|
|
|
return !value;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 13:12:07 +02:00
|
|
|
isValid = () => {
|
2019-04-03 17:16:55 +02:00
|
|
|
const { source, name } = this.state;
|
|
|
|
|
return !(
|
|
|
|
|
this.state.nameValidationError ||
|
|
|
|
|
this.isFalsy(source) ||
|
|
|
|
|
this.isFalsy(name)
|
|
|
|
|
);
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (this.isValid()) {
|
|
|
|
|
this.props.submitForm(this.state.branch);
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-04-02 09:27:34 +02:00
|
|
|
|
|
|
|
|
render() {
|
2019-04-04 13:03:51 +02:00
|
|
|
const { t, branches, loading, transmittedName } = this.props;
|
2019-04-03 17:16:55 +02:00
|
|
|
const { name } = this.state;
|
|
|
|
|
orderBranches(branches);
|
2019-04-03 13:12:07 +02:00
|
|
|
const options = branches.map(branch => ({
|
|
|
|
|
label: branch.name,
|
|
|
|
|
value: branch.name
|
|
|
|
|
}));
|
|
|
|
|
|
2019-04-02 09:27:34 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
2019-04-03 13:12:07 +02:00
|
|
|
<form onSubmit={this.submit}>
|
|
|
|
|
<div className="columns">
|
|
|
|
|
<div className="column">
|
|
|
|
|
<Select
|
|
|
|
|
name="source"
|
|
|
|
|
label={t("branches.create.source")}
|
|
|
|
|
options={options}
|
|
|
|
|
onChange={this.handleSourceChange}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
<InputField
|
|
|
|
|
name="name"
|
|
|
|
|
label={t("branches.create.name")}
|
|
|
|
|
onChange={this.handleNameChange}
|
2019-04-03 17:16:55 +02:00
|
|
|
value={name ? name : ""}
|
|
|
|
|
validationError={this.state.nameValidationError}
|
|
|
|
|
errorMessage={t("validation.branch.nameInvalid")}
|
2019-04-04 13:03:51 +02:00
|
|
|
disabled={!!transmittedName}
|
2019-04-03 13:12:07 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="columns">
|
|
|
|
|
<div className="column">
|
|
|
|
|
<SubmitButton
|
|
|
|
|
disabled={!this.isValid()}
|
|
|
|
|
loading={loading}
|
|
|
|
|
label={t("branches.create.submit")}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
2019-04-02 09:27:34 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-04-03 13:12:07 +02:00
|
|
|
|
|
|
|
|
handleSourceChange = (source: string) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
source
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleNameChange = (name: string) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
nameValidationError: !validator.isNameValid(name),
|
|
|
|
|
...this.state,
|
|
|
|
|
name
|
|
|
|
|
});
|
|
|
|
|
};
|
2019-04-02 09:27:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default translate("repos")(BranchForm);
|