useAbortNavigation.ts 938 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { onBeforeRouteLeave, onBeforeRouteUpdate } from 'vue-router'
  3. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  4. export const useAbortNavigation = ({
  5. confirmCallback,
  6. shouldConfirmNavigation,
  7. }: {
  8. confirmCallback: () => void
  9. shouldConfirmNavigation: () => boolean
  10. }) => {
  11. const { waitForVariantConfirmation } = useConfirmation()
  12. onBeforeRouteUpdate(async () => {
  13. if (!shouldConfirmNavigation()) return true
  14. const confirmed = await waitForVariantConfirmation('unsaved')
  15. if (confirmed) {
  16. confirmCallback()
  17. return true
  18. }
  19. return false
  20. })
  21. onBeforeRouteLeave(async () => {
  22. if (!shouldConfirmNavigation()) return true
  23. const confirmed = await waitForVariantConfirmation('unsaved')
  24. if (confirmed) {
  25. confirmCallback()
  26. return true
  27. }
  28. return false
  29. })
  30. }