2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { withRouter } from "react-router-dom";
|
|
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
|
|
|
|
import queryString from "query-string";
|
|
|
|
|
import { History } from "history";
|
|
|
|
|
import { Repository, Branch, BranchRequest } from "@scm-manager/ui-types";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { ErrorNotification, Loading, Subtitle } from "@scm-manager/ui-components";
|
2019-10-20 18:02:52 +02:00
|
|
|
import BranchForm from "../components/BranchForm";
|
2019-04-03 13:12:07 +02:00
|
|
|
import {
|
|
|
|
|
fetchBranches,
|
|
|
|
|
getBranches,
|
2019-04-10 16:20:55 +02:00
|
|
|
getBranchCreateLink,
|
2019-04-03 13:12:07 +02:00
|
|
|
createBranch,
|
|
|
|
|
createBranchReset,
|
|
|
|
|
isCreateBranchPending,
|
2019-04-04 11:43:30 +02:00
|
|
|
getCreateBranchFailure,
|
|
|
|
|
isFetchBranchesPending,
|
2019-10-20 18:02:52 +02:00
|
|
|
getFetchBranchesFailure
|
|
|
|
|
} from "../modules/branches";
|
2019-03-28 17:09:59 +01:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
loading?: boolean;
|
|
|
|
|
error?: Error;
|
|
|
|
|
repository: Repository;
|
|
|
|
|
branches: Branch[];
|
|
|
|
|
createBranchesLink: string;
|
|
|
|
|
isPermittedToCreateBranches: boolean;
|
2019-04-03 13:12:07 +02:00
|
|
|
|
|
|
|
|
// dispatcher functions
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchBranches: (p: Repository) => void;
|
2019-04-10 15:33:50 +02:00
|
|
|
createBranch: (
|
|
|
|
|
createLink: string,
|
|
|
|
|
repository: Repository,
|
|
|
|
|
branch: BranchRequest,
|
2019-10-20 18:02:52 +02:00
|
|
|
callback?: (p: Branch) => void
|
2019-10-19 16:38:07 +02:00
|
|
|
) => void;
|
|
|
|
|
resetForm: (p: Repository) => void;
|
2019-04-03 13:12:07 +02:00
|
|
|
|
|
|
|
|
// context objects
|
2019-10-19 16:38:07 +02:00
|
|
|
history: History;
|
|
|
|
|
location: any;
|
2019-03-29 10:22:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CreateBranch extends React.Component<Props> {
|
2019-04-03 13:12:07 +02:00
|
|
|
componentDidMount() {
|
|
|
|
|
const { fetchBranches, repository } = this.props;
|
|
|
|
|
fetchBranches(repository);
|
2019-04-10 16:20:55 +02:00
|
|
|
this.props.resetForm(repository);
|
2019-04-03 13:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
branchCreated = (branch: Branch) => {
|
2019-04-10 15:33:50 +02:00
|
|
|
const { history, repository } = this.props;
|
2019-10-21 10:57:56 +02:00
|
|
|
history.push(`/repo/${repository.namespace}/${repository.name}/branch/${encodeURIComponent(branch.name)}/info`);
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
|
2019-04-10 15:33:50 +02:00
|
|
|
createBranch = (branch: BranchRequest) => {
|
2019-10-21 10:57:56 +02:00
|
|
|
this.props.createBranch(this.props.createBranchesLink, this.props.repository, branch, newBranch =>
|
|
|
|
|
this.branchCreated(newBranch)
|
2019-04-10 15:33:50 +02:00
|
|
|
);
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
|
2019-04-04 13:03:51 +02:00
|
|
|
transmittedName = (url: string) => {
|
2019-04-04 11:43:30 +02:00
|
|
|
const params = queryString.parse(url);
|
|
|
|
|
return params.name;
|
2019-04-04 11:04:39 +02:00
|
|
|
};
|
|
|
|
|
|
2019-03-28 17:09:59 +01:00
|
|
|
render() {
|
2019-10-21 10:57:56 +02:00
|
|
|
const { t, loading, error, repository, branches, createBranchesLink, location } = this.props;
|
2019-04-03 13:12:07 +02:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 13:03:51 +02:00
|
|
|
if (loading || !branches) {
|
2019-04-04 11:43:30 +02:00
|
|
|
return <Loading />;
|
2019-04-03 13:12:07 +02:00
|
|
|
}
|
2019-03-28 17:09:59 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2019-10-20 18:02:52 +02:00
|
|
|
<Subtitle subtitle={t("branches.create.title")} />
|
2019-04-03 13:12:07 +02:00
|
|
|
<BranchForm
|
2019-04-10 15:33:50 +02:00
|
|
|
submitForm={branchRequest => this.createBranch(branchRequest)}
|
2019-04-03 13:12:07 +02:00
|
|
|
loading={loading}
|
|
|
|
|
repository={repository}
|
|
|
|
|
branches={branches}
|
2019-04-04 13:03:51 +02:00
|
|
|
transmittedName={this.transmittedName(location.search)}
|
2019-04-10 16:38:56 +02:00
|
|
|
disabled={!createBranchesLink}
|
2019-04-03 13:12:07 +02:00
|
|
|
/>
|
2019-03-28 17:09:59 +01:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 13:12:07 +02:00
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
|
|
|
|
fetchBranches: (repository: Repository) => {
|
|
|
|
|
dispatch(fetchBranches(repository));
|
|
|
|
|
},
|
|
|
|
|
createBranch: (
|
2019-04-10 15:33:50 +02:00
|
|
|
createLink: string,
|
2019-04-03 13:12:07 +02:00
|
|
|
repository: Repository,
|
2019-04-10 15:33:50 +02:00
|
|
|
branchRequest: BranchRequest,
|
2019-10-20 18:02:52 +02:00
|
|
|
callback?: (newBranch: Branch) => void
|
2019-04-03 13:12:07 +02:00
|
|
|
) => {
|
2019-04-10 15:33:50 +02:00
|
|
|
dispatch(createBranch(createLink, repository, branchRequest, callback));
|
2019-04-03 13:12:07 +02:00
|
|
|
},
|
2019-04-10 16:20:55 +02:00
|
|
|
resetForm: (repository: Repository) => {
|
|
|
|
|
dispatch(createBranchReset(repository));
|
2019-10-20 18:02:52 +02:00
|
|
|
}
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
|
const { repository } = ownProps;
|
2019-10-21 10:57:56 +02:00
|
|
|
const loading = isFetchBranchesPending(state, repository) || isCreateBranchPending(state, repository);
|
|
|
|
|
const error = getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state);
|
2019-04-03 13:12:07 +02:00
|
|
|
const branches = getBranches(state, repository);
|
2019-04-10 16:20:55 +02:00
|
|
|
const createBranchesLink = getBranchCreateLink(state, repository);
|
2019-04-03 13:12:07 +02:00
|
|
|
return {
|
|
|
|
|
repository,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
2019-04-10 15:33:50 +02:00
|
|
|
branches,
|
2019-10-20 18:02:52 +02:00
|
|
|
createBranchesLink
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-04 11:43:30 +02:00
|
|
|
export default withRouter(
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
2019-10-20 18:02:52 +02:00
|
|
|
mapDispatchToProps
|
2019-10-23 15:47:08 +02:00
|
|
|
)(withTranslation("repos")(CreateBranch))
|
2019-04-04 11:43:30 +02:00
|
|
|
);
|