Files
SCM-Manager/scm-ui/ui-webapp/src/repos/branches/containers/CreateBranch.js

151 lines
3.7 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
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 16:20:55 +02:00
getBranchCreateLink,
2019-04-03 13:12:07 +02:00
createBranch,
createBranchReset,
isCreateBranchPending,
getCreateBranchFailure,
isFetchBranchesPending,
getFetchBranchesFailure
2019-04-03 13:12:07 +02:00
} from "../modules/branches";
import type { History } from "history";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import queryString from "query-string";
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,
isPermittedToCreateBranches: boolean,
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-10 16:20:55 +02:00
resetForm: Repository => void,
2019-04-03 13:12:07 +02:00
// context objects
t: string => string,
history: History,
location: any
};
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;
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) => {
const params = queryString.parse(url);
return params.name;
};
render() {
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) {
return <Loading />;
2019-04-03 13:12:07 +02: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)}
disabled={!createBranchesLink}
2019-04-03 13:12:07 +02: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
},
2019-04-10 16:20:55 +02:00
resetForm: (repository: Repository) => {
dispatch(createBranchReset(repository));
2019-04-03 13:12:07 +02:00
}
};
};
const mapStateToProps = (state, ownProps) => {
const { repository } = ownProps;
2019-04-10 16:20:55 +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,
createBranchesLink
2019-04-03 13:12:07 +02:00
};
};
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(translate("repos")(CreateBranch))
);