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

128 lines
3.0 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";
import type { Repository, Branch } from "@scm-manager/ui-types";
import {
fetchBranches,
getBranches,
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[],
// dispatcher functions
fetchBranches: Repository => void,
createBranch: (branch: Branch, callback?: () => void) => void,
resetForm: () => void,
// 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);
this.props.resetForm();
}
branchCreated = (branch: Branch) => {
const { history } = this.props;
history.push("/branch/" + encodeURIComponent(branch.name) + "/info");
};
createBranch = (branch: Branch) => {
this.props.createBranch(branch, () => this.branchCreated(branch));
};
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, 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
submitForm={branch => this.createBranch(branch)}
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-04-03 13:12:07 +02:00
const mapDispatchToProps = dispatch => {
return {
fetchBranches: (repository: Repository) => {
dispatch(fetchBranches(repository));
},
createBranch: (
repository: Repository,
branch: Branch,
callback?: () => void
) => {
2019-04-03 17:35:02 +02:00
dispatch(createBranch("INSERTLINK", repository, branch, callback)); //TODO
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);
const error =
getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state);
2019-04-03 13:12:07 +02:00
const branches = getBranches(state, repository);
return {
repository,
loading,
error,
branches
};
};
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(translate("repos")(CreateBranch))
);