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

155 lines
5.0 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 { WithTranslation, withTranslation } from "react-i18next";
import { History } from "history";
import { NamespaceStrategies, Repository, RepositoryCreation, RepositoryType } from "@scm-manager/ui-types";
import { Page } from "@scm-manager/ui-components";
2018-08-02 16:09:58 +02:00
import {
fetchRepositoryTypesIfNeeded,
getFetchRepositoryTypesFailure,
getRepositoryTypes,
isFetchRepositoryTypesPending
} from "../modules/repositoryTypes";
import RepositoryForm from "../components/form";
2019-10-21 10:57:56 +02:00
import { createRepo, createRepoReset, getCreateRepoFailure, isCreateRepoPending } from "../modules/repos";
import { getRepositoriesLink } from "../../modules/indexResource";
import {
fetchNamespaceStrategiesIfNeeded,
getFetchNamespaceStrategiesFailure,
getNamespaceStrategies,
isFetchNamespaceStrategiesPending
} from "../../admin/modules/namespaceStrategies";
2018-08-02 16:09:58 +02:00
type Props = WithTranslation & {
repositoryTypes: RepositoryType[];
namespaceStrategies: NamespaceStrategies;
pageLoading: boolean;
createLoading: boolean;
error: Error;
repoLink: string;
2020-09-07 15:40:14 +02:00
indexResources: any;
2018-08-02 16:09:58 +02:00
// dispatch functions
fetchNamespaceStrategiesIfNeeded: () => void;
fetchRepositoryTypesIfNeeded: () => void;
2020-03-31 17:15:09 +02:00
createRepo: (
link: string,
repository: RepositoryCreation,
2020-03-31 17:15:09 +02:00
initRepository: boolean,
2020-09-09 08:54:38 +02:00
callback: (repo: Repository) => void
2020-03-31 17:15:09 +02:00
) => void;
resetForm: () => void;
2018-08-02 16:09:58 +02:00
// context props
history: History;
2018-08-02 16:09:58 +02:00
};
class Create extends React.Component<Props> {
componentDidMount() {
2018-08-03 08:52:02 +02:00
this.props.resetForm();
2018-08-02 16:09:58 +02:00
this.props.fetchRepositoryTypesIfNeeded();
this.props.fetchNamespaceStrategiesIfNeeded();
2018-08-02 16:09:58 +02:00
}
2019-01-22 10:50:45 +01:00
repoCreated = (repo: Repository) => {
2018-08-03 08:52:02 +02:00
const { history } = this.props;
2019-01-22 11:58:31 +01:00
history.push("/repo/" + repo.namespace + "/" + repo.name);
2018-08-03 08:52:02 +02:00
};
2018-08-02 16:09:58 +02:00
render() {
2020-11-24 11:15:42 +01:00
const {
pageLoading,
createLoading,
repositoryTypes,
namespaceStrategies,
createRepo,
error,
indexResources
} = this.props;
2018-08-02 16:09:58 +02:00
const { t, repoLink } = this.props;
2018-08-02 16:09:58 +02:00
return (
<Page
title={t("create.title")}
loading={pageLoading}
2018-08-02 16:09:58 +02:00
error={error}
2018-08-03 08:52:02 +02:00
showContentOnError={true}
2018-08-02 16:09:58 +02:00
>
2018-08-03 08:52:02 +02:00
<RepositoryForm
repositoryTypes={repositoryTypes}
loading={createLoading}
namespaceStrategy={namespaceStrategies.current}
2020-11-24 12:50:22 +01:00
createRepository={(repo, initRepository) => {
createRepo(repoLink, repo, initRepository, (repo: Repository) => this.repoCreated(repo));
2018-08-03 08:52:02 +02:00
}}
2020-09-07 15:40:14 +02:00
indexResources={indexResources}
2018-08-03 08:52:02 +02:00
/>
2018-08-02 16:09:58 +02:00
</Page>
);
}
}
2020-01-08 13:40:07 +01:00
const mapStateToProps = (state: any) => {
2018-08-02 16:09:58 +02:00
const repositoryTypes = getRepositoryTypes(state);
const namespaceStrategies = getNamespaceStrategies(state);
2019-10-21 10:57:56 +02:00
const pageLoading = isFetchRepositoryTypesPending(state) || isFetchNamespaceStrategiesPending(state);
2018-08-03 08:52:02 +02:00
const createLoading = isCreateRepoPending(state);
const error =
2019-10-21 10:57:56 +02:00
getFetchRepositoryTypesFailure(state) || getCreateRepoFailure(state) || getFetchNamespaceStrategiesFailure(state);
const repoLink = getRepositoriesLink(state);
2020-09-07 15:40:14 +02:00
const indexResources = state?.indexResources;
2018-08-02 16:09:58 +02:00
return {
repositoryTypes,
namespaceStrategies,
pageLoading,
2018-08-03 08:52:02 +02:00
createLoading,
error,
2020-09-07 15:40:14 +02:00
repoLink,
indexResources
2018-08-02 16:09:58 +02:00
};
};
2020-01-08 13:40:07 +01:00
const mapDispatchToProps = (dispatch: any) => {
2018-08-02 16:09:58 +02:00
return {
fetchRepositoryTypesIfNeeded: () => {
dispatch(fetchRepositoryTypesIfNeeded());
2018-08-03 08:52:02 +02:00
},
fetchNamespaceStrategiesIfNeeded: () => {
dispatch(fetchNamespaceStrategiesIfNeeded());
},
2020-09-07 15:40:14 +02:00
createRepo: (link: string, repository: RepositoryCreation, initRepository: boolean, callback: () => void) => {
dispatch(createRepo(link, repository, initRepository, callback));
2018-08-03 08:52:02 +02:00
},
resetForm: () => {
dispatch(createRepoReset());
}
2018-08-02 16:09:58 +02:00
};
};
2020-01-08 13:40:07 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(withTranslation("repos")(Create));