Skip to content

Commit acc307a

Browse files
committed
Use sveltekit beforeNavigate navigation lifecycle hook
Prevent a user to navigate away from the page if there are unsaved changes in workflow task arguments.
1 parent 9219780 commit acc307a

File tree

1 file changed

+4
-7
lines changed
  • src/routes/projects/[projectId]/workflows/[workflowId]

1 file changed

+4
-7
lines changed

src/routes/projects/[projectId]/workflows/[workflowId]/+page.svelte

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { onMount } from 'svelte';
33
import { writable } from 'svelte/store';
44
import { enhance } from '$app/forms';
5-
import { goto } from '$app/navigation';
5+
import { goto, beforeNavigate } from '$app/navigation';
66
import { page } from '$app/stores';
77
import ArgumentForm from '$lib/components/workflow/ArgumentForm.svelte';
88
import ConfirmActionButton from '$lib/components/common/ConfirmActionButton.svelte';
@@ -46,15 +46,12 @@
4646
datasets = $page.data.datasets;
4747
});
4848
49-
// Add an event listener for the 'beforeunload' event
50-
window.addEventListener('beforeunload', function(e) {
49+
beforeNavigate((navigation) => {
5150
if (argumentsWithUnsavedChanges === true) {
52-
// Cancel the event
53-
e.preventDefault();
51+
// Prevent navigation
52+
navigation.cancel();
5453
// Toggle the modal
5554
toggleUnsavedChangesModal();
56-
// Chrome requires returnValue to be set
57-
e.returnValue = '';
5855
}
5956
});
6057

0 commit comments

Comments
 (0)