import {
Blocker,
PlatformServiceName,
Transition,
} from '@limetech/lime-web-components';
const navigator = platform.get(PlatformServiceName.Navigator);
let hasUnsavedChanges = false;
const blocker: Blocker = (transition: Transition) => {
if (!hasUnsavedChanges) {
return false;
}
void confirmNavigation(transition);
// Block for now, `retry()` resumes the navigation if the user confirms
return true;
};
async function confirmNavigation(transition: Transition) {
const shouldNavigate = await showConfirmDialog();
if (!shouldNavigate) {
return;
}
hasUnsavedChanges = false;
navigator.removeBlocker(blocker);
transition.retry();
}
// Stands in for the dialog the component would open
async function showConfirmDialog(): Promise<boolean> {
return confirm('You have unsaved changes. Leave anyway?');
}
navigator.addBlocker(blocker);
Function to retry blocked navigation
Provided by Transition to resume navigation after it was blocked by a Blocker. Call this after user confirmation or other conditions are met.