Skip to content

Commit

Permalink
Prevent leaving the page when the update is in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Dec 12, 2024
1 parent c4a358f commit 8cc51e2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions _dev/src/ts/pages/UpdatePageUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default class UpdatePageUpdate extends UpdatePage {
const stepContent = document.getElementById('ua_step_content')!;
const updateAction = stepContent.dataset.initialProcessAction!;

this.#enableExitConfirmation();

const process = new Process({
onProcessResponse: this.#onProcessResponse,
onProcessEnd: this.#onProcessEnd,
Expand All @@ -36,6 +38,7 @@ export default class UpdatePageUpdate extends UpdatePage {
this.#restoreAlertForm?.removeEventListener('submit', this.#handleSubmit);
this.#restoreButtonForm?.removeEventListener('submit', this.#handleSubmit);
this.#submitErrorReportForm?.removeEventListener('submit', this.#handleSubmit);
this.#disableExitConfirmation();
};

get #progressTrackerContainer(): HTMLDivElement {
Expand All @@ -55,6 +58,7 @@ export default class UpdatePageUpdate extends UpdatePage {
};

#onProcessEnd = async (response: ApiResponseAction): Promise<void> => {
this.#disableExitConfirmation();
if (response.error) {
this.#onError(response);
} else {
Expand All @@ -63,6 +67,7 @@ export default class UpdatePageUpdate extends UpdatePage {
};

#onError = (response: ApiResponseAction): void => {
this.#disableExitConfirmation();
this.#progressTracker.updateProgress(response);
this.#progressTracker.endProgress();
this.#displayErrorAlert();
Expand Down Expand Up @@ -105,4 +110,19 @@ export default class UpdatePageUpdate extends UpdatePage {

await api.post(form.dataset.routeToSubmit!);
};

#enableExitConfirmation = () => {
window.addEventListener('beforeunload', this.#handleBeforeUnload);
};

#disableExitConfirmation = () => {
window.removeEventListener('beforeunload', this.#handleBeforeUnload);
};

#handleBeforeUnload = (event: Event) => {
event.preventDefault();

// Included for legacy support, e.g. Chrome/Edge < 119
event.returnValue = true;
};
}

0 comments on commit 8cc51e2

Please sign in to comment.