Files
SCM-Manager/scm-ui/ui-webapp/src/repos/components/ImportRepositoryFromUrl.tsx

126 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-11-30 16:20:39 +01:00
/*
* 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.
*/
2020-12-01 11:35:49 +01:00
import React, { FC, FormEvent, useState } from "react";
import { RepositoryCreation, RepositoryUrlImport } from "@scm-manager/ui-types";
2020-11-30 16:20:39 +01:00
import ImportFromUrlForm from "./ImportFromUrlForm";
import { apiClient, ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { SubFormProps } from "../types";
2020-11-30 16:20:39 +01:00
type Props = {
url: string;
repositoryType: string;
2020-11-30 16:20:39 +01:00
setImportPending: (pending: boolean) => void;
nameForm: React.ComponentType<SubFormProps>;
informationForm: React.ComponentType<SubFormProps>;
2020-11-30 16:20:39 +01:00
};
const ImportRepositoryFromUrl: FC<Props> = ({
url,
repositoryType,
setImportPending,
nameForm: NameForm,
informationForm: InformationForm
}) => {
2020-11-30 16:20:39 +01:00
const [repo, setRepo] = useState<RepositoryUrlImport>({
name: "",
namespace: "",
type: repositoryType,
2020-11-30 16:20:39 +01:00
contact: "",
description: "",
importUrl: "",
username: "",
password: "",
contextEntries: []
2020-11-30 16:20:39 +01:00
});
const [valid, setValid] = useState({ namespaceAndName: false, contact: true, importUrl: false });
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | undefined>();
const history = useHistory();
const [t] = useTranslation("repos");
const isValid = () => Object.values(valid).every(v => v);
2020-11-30 16:20:39 +01:00
const handleImportLoading = (loading: boolean) => {
setImportPending(loading);
setLoading(loading);
2020-11-30 16:20:39 +01:00
};
2020-12-01 11:35:49 +01:00
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
setError(undefined);
2020-12-01 11:35:49 +01:00
const currentPath = history.location.pathname;
2020-11-30 16:20:39 +01:00
handleImportLoading(true);
apiClient
.post(url, repo, "application/vnd.scmm-repository+json;v=2")
.then(response => {
const location = response.headers.get("Location");
2020-12-02 10:42:26 +01:00
return apiClient.get(location!);
2020-11-30 16:20:39 +01:00
})
.then(response => response.json())
2020-12-01 11:35:49 +01:00
.then(repo => {
handleImportLoading(false);
2020-12-01 11:35:49 +01:00
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}
})
2020-11-30 16:20:39 +01:00
.catch(error => {
setError(error);
handleImportLoading(false);
});
};
return (
<form onSubmit={submit}>
2020-12-03 08:54:56 +01:00
<ErrorNotification error={error} />
2020-11-30 16:20:39 +01:00
<ImportFromUrlForm
repository={repo}
onChange={setRepo}
setValid={(importUrl: boolean) => setValid({ ...valid, importUrl })}
2020-12-01 11:35:49 +01:00
disabled={loading}
2020-11-30 16:20:39 +01:00
/>
<hr />
<NameForm
2020-11-30 16:20:39 +01:00
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
2020-11-30 16:20:39 +01:00
setValid={(namespaceAndName: boolean) => setValid({ ...valid, namespaceAndName })}
2020-12-01 11:35:49 +01:00
disabled={loading}
2020-11-30 16:20:39 +01:00
/>
<InformationForm
2020-11-30 16:20:39 +01:00
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
2020-12-01 11:35:49 +01:00
disabled={loading}
2020-11-30 16:20:39 +01:00
setValid={(contact: boolean) => setValid({ ...valid, contact })}
/>
<Level
right={<SubmitButton disabled={!isValid()} loading={loading} label={t("repositoryForm.submitImport")} />}
/>
</form>
);
};
export default ImportRepositoryFromUrl;