2019-03-29 10:22:54 +01:00
|
|
|
//@flow
|
2019-03-28 17:09:59 +01:00
|
|
|
import React from "react";
|
2019-04-04 11:43:30 +02:00
|
|
|
import {
|
|
|
|
|
ErrorNotification,
|
|
|
|
|
Loading,
|
|
|
|
|
Subtitle
|
|
|
|
|
} from "@scm-manager/ui-components";
|
2019-04-03 13:12:07 +02:00
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import BranchForm from "../components/BranchForm";
|
2019-04-10 15:33:50 +02:00
|
|
|
import type { Repository, Branch, BranchRequest } from "@scm-manager/ui-types";
|
2019-04-03 13:12:07 +02:00
|
|
|
import {
|
|
|
|
|
fetchBranches,
|
|
|
|
|
getBranches,
|
2019-04-10 15:33:50 +02:00
|
|
|
getBrancheCreateLink,
|
2019-04-03 13:12:07 +02:00
|
|
|
createBranch,
|
|
|
|
|
createBranchReset,
|
|
|
|
|
isCreateBranchPending,
|
2019-04-04 11:43:30 +02:00
|
|
|
getCreateBranchFailure,
|
|
|
|
|
isFetchBranchesPending,
|
|
|
|
|
getFetchBranchesFailure
|
2019-04-03 13:12:07 +02:00
|
|
|
} from "../modules/branches";
|
|
|
|
|
import type { History } from "history";
|
|
|
|
|
import { connect } from "react-redux";
|
2019-04-04 11:43:30 +02:00
|
|
|
import { withRouter } from "react-router-dom";
|
|
|
|
|
import queryString from "query-string";
|
2019-03-28 17:09:59 +01:00
|
|
|
|
2019-03-29 10:22:54 +01:00
|
|
|
type Props = {
|
2019-04-03 13:12:07 +02:00
|
|
|
loading?: boolean,
|
|
|
|
|
error?: Error,
|
|
|
|
|
repository: Repository,
|
|
|
|
|
branches: Branch[],
|
2019-04-10 15:33:50 +02:00
|
|
|
createBranchesLink: string,
|
2019-04-03 13:12:07 +02:00
|
|
|
|
|
|
|
|
// dispatcher functions
|
|
|
|
|
fetchBranches: Repository => void,
|
2019-04-10 15:33:50 +02:00
|
|
|
createBranch: (
|
|
|
|
|
createLink: string,
|
|
|
|
|
repository: Repository,
|
|
|
|
|
branch: BranchRequest,
|
|
|
|
|
callback?: (Branch) => void
|
|
|
|
|
) => void,
|
2019-04-03 13:12:07 +02:00
|
|
|
resetForm: () => void,
|
|
|
|
|
|
|
|
|
|
// context objects
|
|
|
|
|
t: string => string,
|
2019-04-04 11:04:39 +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);
|
|
|
|
|
this.props.resetForm();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
branchCreated = (branch: Branch) => {
|
2019-04-10 15:33:50 +02:00
|
|
|
const { history, repository } = this.props;
|
|
|
|
|
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) => {
|
|
|
|
|
this.props.createBranch(
|
|
|
|
|
this.props.createBranchesLink,
|
|
|
|
|
this.props.repository,
|
|
|
|
|
branch,
|
|
|
|
|
newBranch => this.branchCreated(newBranch)
|
|
|
|
|
);
|
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-04-04 11:04:39 +02:00
|
|
|
const { t, loading, error, repository, branches, 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 (
|
|
|
|
|
<>
|
|
|
|
|
<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-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,
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
resetForm: () => {
|
|
|
|
|
dispatch(createBranchReset());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
|
const { repository } = ownProps;
|
2019-04-04 13:03:51 +02:00
|
|
|
const loading = isFetchBranchesPending(state, repository); //|| isCreateBranchPending(state);
|
2019-04-04 11:43:30 +02:00
|
|
|
const error =
|
|
|
|
|
getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state);
|
2019-04-03 13:12:07 +02:00
|
|
|
const branches = getBranches(state, repository);
|
2019-04-10 15:33:50 +02:00
|
|
|
const createBranchesLink = getBrancheCreateLink(state, repository);
|
2019-04-03 13:12:07 +02:00
|
|
|
return {
|
|
|
|
|
repository,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
2019-04-10 15:33:50 +02:00
|
|
|
branches,
|
|
|
|
|
createBranchesLink
|
2019-04-03 13:12:07 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-04 11:43:30 +02:00
|
|
|
export default withRouter(
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)(translate("repos")(CreateBranch))
|
|
|
|
|
);
|