zambezi / caballo-vivo

Glue code for pure RxJS applications to connect with React-Router, and other operators
2 stars 7 forks source link

closes #35, closes #63; ability to prevent and restore app navigation #64

Closed robertgorecki closed 1 year ago

robertgorecki commented 1 year ago

Use case: User is filling in a form and before saving the details, he's clicking a link to navigate to different page. I'd like to be able to show him a notification that unsaved changes will be lost, so that he can decide if he wants to stay on this page or navigate away.

Problem: caballo-vivo is already using history.block() to control managed paths, so using history.block() within consuming app is breaking navigation for managed paths.

Solution: caballo-vivo could export a method to block/unblock navigation

Example usage as react hook:

import React, {useEffect} from 'react'
import { preventNavigation } from '@zambezi/caballo-vivo'

...

  useEffect(() => {
    if (isFormDirty) {
      const unblockNavigation = preventNavigation(callback => {
        callback(window.confirm("There are unsaved changes, are you sure you want to leave?"))
      })

      return unblockNavigation 
    }
  }, [isFormDirty])

...
robertgorecki commented 1 year ago

@gabrielmontagne @cristiano-belloni what do you think about this guys?

gabrielmontagne commented 1 year ago

@robertgorecki , very cool. Taking a look. Thanks. And yes, it'd be good to close that history.lock old not-anymore-issue.

gabrielmontagne commented 1 year ago

I pinged also @yerlanyr for an extra pair of eyes.

gabrielmontagne commented 1 year ago

also @mamapitufo

cristiano-belloni commented 1 year ago

I don't remember all the details, but lgtm

yerlanyr commented 1 year ago

Looks good to me. I would also suggest to add a test to cover said use case.

robertgorecki commented 1 year ago

Looks good to me. I would also suggest to add a test to cover said use case.

tests added

gabrielmontagne commented 1 year ago

Cool, @robertgorecki -- I'm merging this. We can publish to NPM early next week.