Feature/import navigation lock (#1536)

Use navigation lock on repository import to prevent that the current user accidentally leaves the page and aborts the running import.
This commit is contained in:
Eduard Heimbuch
2021-02-16 11:35:06 +01:00
committed by GitHub
parent ac3d9c9fa1
commit 1e1b73ace5
9 changed files with 82 additions and 25 deletions

View File

@@ -0,0 +1,2 @@
- type: added
description: Warn user to not leave page during repository import ([#1536](https://github.com/scm-manager/scm-manager/pull/1536))

View File

@@ -35,3 +35,4 @@ export { MenuContext, StateMenuContextProvider } from "./MenuContext";
export { default as SecondaryNavigationItem } from "./SecondaryNavigationItem";
export { default as ExternalLink } from "./ExternalLink";
export { default as ExternalNavLink } from "./ExternalNavLink";
export { default as useNavigationLock } from "./useNavigationLock";

View File

@@ -0,0 +1,43 @@
/*
* 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 { useEffect } from "react";
// This hook can be used to warn the user on reloading or closing the current page if the navigation lock is enabled.
const useNavigationLock = (enabled: boolean) => {
useEffect(() => {
if (enabled) {
window.onbeforeunload = () => true;
} else {
// @ts-ignore We need to reset this listener if the lock was disabled
window.onbeforeunload = undefined;
}
return () => {
// @ts-ignore Remove this listener when the hook will be unmounted
window.onbeforeunload = undefined;
};
}, [enabled]);
};
export default useNavigationLock;

View File

@@ -82,7 +82,7 @@
},
"pending": {
"subtitle": "Repository wird importiert...",
"infoText": "Ihr Repository wird gerade importiert. Dies kann einen Moment dauern. Sie werden weitergeleitet, sobald der Import abgeschlossen ist. Wenn Sie diese Seite verlassen, können Sie nicht zurückkehren, um den Import-Status zu erfahren."
"infoText": "Ihr Repository wird gerade importiert. Dies kann einen Moment dauern. Sie werden weitergeleitet, sobald der Import abgeschlossen ist. Wenn Sie diese Seite verlassen, können Sie nicht zurückkehren, um den Import-Status zu erfahren. Sie können den SCM-Manager in einem anderem Tab/Fenster weiter verwenden."
},
"importTypes": {
"label": "Import Modus",
@@ -98,7 +98,8 @@
"label": "Import aus Archiv mit Metadaten (Experimentell)",
"helpText": "Das Repository inkl. der Metadaten wird aus einem archivierten Dump importiert. Das Import Archiv muss von einem anderen SCM-Manager generiert worden sein."
}
}
},
"navigationWarning": "Es wird dringend empfohlen auf dieser Seite zu warten bis der Import abgeschlossen ist. Beim Verlassen der Seite könnte der Import abbrechen. Seite trotzdem verlassen?"
},
"branches": {
"overview": {

View File

@@ -82,7 +82,7 @@
},
"pending": {
"subtitle": "Importing Repository...",
"infoText": "Your repository is currently being imported. This may take a moment. You will be forwarded as soon as the import is finished. If you leave this page you cannot return to find out the import status."
"infoText": "Your repository is currently being imported. This may take a moment. You will be forwarded as soon as the import is finished. If you leave this page you cannot return to find out the import status. But you can continue using the SCM-Manager in another tab/window."
},
"importTypes": {
"label": "Import Mode",
@@ -98,7 +98,8 @@
"label": "Import from archive with metadata (experimental)",
"helpText": "The repository will be imported with metadata. The archive containing the data must be generated by an SCM-Manager instance."
}
}
},
"navigationWarning": "It is strongly recommend that you stay on this page until the import is finished. Leaving this page could abort your import. Leave page anyway?"
},
"branches": {
"overview": {

View File

@@ -67,21 +67,22 @@ const ImportFullRepository: FC<Props> = ({ url, repositoryType, setImportPending
setError(undefined);
handleImportLoading(true);
apiClient
.postBinary(url, (formData) => {
.postBinary(url, formData => {
formData.append("bundle", file, file?.name);
formData.append("repository", JSON.stringify(repo));
})
.then((response) => {
.then(response => {
const location = response.headers.get("Location");
return apiClient.get(location!);
})
.then((response) => response.json())
.then((repo) => {
.then(response => response.json())
.then(repo => {
handleImportLoading(false);
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}
})
.catch((error) => {
.catch(error => {
setError(error);
handleImportLoading(false);
});

View File

@@ -43,7 +43,7 @@ const ImportRepositoryFromBundle: FC<Props> = ({ url, repositoryType, setImportP
type: repositoryType,
contact: "",
description: "",
_links: {},
_links: {}
});
const [valid, setValid] = useState({ namespaceAndName: false, contact: true, file: false });
@@ -59,7 +59,7 @@ const ImportRepositoryFromBundle: FC<Props> = ({ url, repositoryType, setImportP
setLoading(loading);
};
const isValid = () => Object.values(valid).every((v) => v);
const isValid = () => Object.values(valid).every(v => v);
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
@@ -67,21 +67,22 @@ const ImportRepositoryFromBundle: FC<Props> = ({ url, repositoryType, setImportP
setError(undefined);
handleImportLoading(true);
apiClient
.postBinary(compressed ? url + "?compressed=true" : url, (formData) => {
.postBinary(compressed ? url + "?compressed=true" : url, formData => {
formData.append("bundle", file, file?.name);
formData.append("repository", JSON.stringify(repo));
})
.then((response) => {
.then(response => {
const location = response.headers.get("Location");
return apiClient.get(location!);
})
.then((response) => response.json())
.then((repo) => {
.then(response => response.json())
.then(repo => {
handleImportLoading(false);
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}
})
.catch((error) => {
.catch(error => {
setError(error);
handleImportLoading(false);
});

View File

@@ -75,6 +75,7 @@ const ImportRepositoryFromUrl: FC<Props> = ({ url, repositoryType, setImportPend
})
.then(response => response.json())
.then(repo => {
handleImportLoading(false);
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}

View File

@@ -29,18 +29,19 @@ import { useTranslation } from "react-i18next";
import ImportRepositoryTypeSelect from "../components/ImportRepositoryTypeSelect";
import ImportTypeSelect from "../components/ImportTypeSelect";
import ImportRepositoryFromUrl from "../components/ImportRepositoryFromUrl";
import { Loading, Notification, Page } from "@scm-manager/ui-components";
import { Loading, Notification, Page, useNavigationLock } from "@scm-manager/ui-components";
import RepositoryFormSwitcher from "../components/form/RepositoryFormSwitcher";
import {
fetchRepositoryTypesIfNeeded,
getFetchRepositoryTypesFailure,
getRepositoryTypes,
isFetchRepositoryTypesPending,
isFetchRepositoryTypesPending
} from "../modules/repositoryTypes";
import { connect } from "react-redux";
import { fetchNamespaceStrategiesIfNeeded } from "../../admin/modules/namespaceStrategies";
import ImportRepositoryFromBundle from "../components/ImportRepositoryFromBundle";
import ImportFullRepository from "../components/ImportFullRepository";
import { Prompt } from "react-router-dom";
type Props = {
repositoryTypes: RepositoryType[];
@@ -69,7 +70,7 @@ const ImportRepository: FC<Props> = ({
pageLoading,
error,
fetchRepositoryTypesIfNeeded,
fetchNamespaceStrategiesIfNeeded,
fetchNamespaceStrategiesIfNeeded
}) => {
const [importPending, setImportPending] = useState(false);
const [repositoryType, setRepositoryType] = useState<RepositoryType | undefined>();
@@ -81,6 +82,8 @@ const ImportRepository: FC<Props> = ({
fetchNamespaceStrategiesIfNeeded();
}, [repositoryTypes]);
useNavigationLock(importPending);
const changeRepositoryType = (repositoryType: RepositoryType) => {
setRepositoryType(repositoryType);
setImportType(repositoryType?._links ? ((repositoryType!._links?.import as Link[])[0] as Link).name! : "");
@@ -110,7 +113,9 @@ const ImportRepository: FC<Props> = ({
if (importType === "fullImport") {
return (
<ImportFullRepository
url={((repositoryType!._links.import as Link[])!.find((link: Link) => link.name === "fullImport") as Link).href}
url={
((repositoryType!._links.import as Link[])!.find((link: Link) => link.name === "fullImport") as Link).href
}
repositoryType={repositoryType!.name}
setImportPending={setImportPending}
/>
@@ -129,6 +134,7 @@ const ImportRepository: FC<Props> = ({
error={error}
showContentOnError={true}
>
<Prompt when={importPending} message={t("import.navigationWarning")} />
<ImportPendingLoading importPending={importPending} />
<ImportRepositoryTypeSelect
repositoryTypes={repositoryTypes}
@@ -161,7 +167,7 @@ const mapStateToProps = (state: any) => {
return {
repositoryTypes,
pageLoading,
error,
error
};
};
@@ -172,7 +178,7 @@ const mapDispatchToProps = (dispatch: any) => {
},
fetchNamespaceStrategiesIfNeeded: () => {
dispatch(fetchNamespaceStrategiesIfNeeded());
},
}
};
};