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

158 lines
4.8 KiB
TypeScript
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from "react";
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 { Branch, BranchRequest, Repository } from "@scm-manager/ui-types";
2019-10-21 10:57:56 +02:00
import { ErrorNotification, Loading, Subtitle } from "@scm-manager/ui-components";
import BranchForm from "../components/BranchForm";
2019-04-03 13:12:07 +02:00
import {
createBranch,
createBranchReset,
fetchBranches,
getBranchCreateLink,
getBranches,
getCreateBranchFailure,
getFetchBranchesFailure,
isCreateBranchPending,
isFetchBranchesPending
} from "../modules/branches";
import { compose } from "redux";
type Props = WithTranslation & {
loading?: boolean;
error?: Error;
repository: Repository;
branches: Branch[];
createBranchesLink: string;
isPermittedToCreateBranches: boolean;
2019-04-03 13:12:07 +02:00
// dispatcher functions
fetchBranches: (p: Repository) => void;
2019-04-10 15:33:50 +02:00
createBranch: (
createLink: string,
repository: Repository,
branch: BranchRequest,
callback?: (p: Branch) => void
) => void;
resetForm: (p: Repository) => void;
2019-04-03 13:12:07 +02:00
// context objects
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;
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) => {
const params = queryString.parse(url);
return params.name;
};
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) {
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
/>
</>
);
}
}
const mapDispatchToProps = (dispatch: any) => {
2019-04-03 13:12:07 +02:00
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: any, ownProps: Props) => {
2019-04-03 13:12:07 +02:00
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, repository);
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 compose(
withTranslation("repos"),
connect(mapStateToProps, mapDispatchToProps),
withRouter
)(CreateBranch);