cerebral-legacy / cerebral-module-router

An opinionated URL change handler for Cerebral
http://cerebral-website.herokuapp.com/documentation/cerebral-module-router
19 stars 4 forks source link

is there a way to prevent routes from triggering? #93

Open awei01 opened 8 years ago

awei01 commented 8 years ago

Hi, I'm wondering if there's a way to prevent routes from triggering or addressbar url change.

Use case: Someone is filling out a form and doesn't save. But then they click on a different link or change address in address bar. Is there a best-practice way to handle this?

Guria commented 8 years ago

I can't see a good way to prevent such case. Link component triggers signals, not routes. You can add an action to your signal which will check that navigation currently unwanted. But from UX point of view I would recommend just have smart form state restore if user returns back after unwanted navigation.

christianalfoni commented 8 years ago

I would suggest the same approach as "authentication" again.

module.addSignals({
  homeClicked: preventUnfinished(homeClicked)
})

This factory has the logic for ensuring that something started is also finished:

function preventUnfinished(chain) {
  return [
    isNotFinished, {
      formA: [notifyFormAError],
      formB: [notifyFormBError],
      otherwise: chain
    }
  ];
}

This will actually also notify when the url is manually changed... which is not the case typically in these scenarios. The debugger will also show very clearly what is happening on these scenarios. Does that make sense?

awei01 commented 8 years ago

This makes sense. But, now it seems like any routeable signal will require these factories to work properly.

Just throwing out another idea: What if the cerebral-module-router had a services like preventRouting and stopPreventRouting which you could turn on and off. Then in my module which manages the form state, i can call preventRouting upon form change and then stopPreventRouting upon form completion?

christianalfoni commented 8 years ago

@awei01 That is an interesting suggestion! Though I believe you would still need to use factories on all the routed signals, because I suppose you want to give an error if the user tries to route and it is prevented? Hm hm :)

awei01 commented 8 years ago

Good point... I'll play around with your suggestion. If I find anything useful, I'll post it here.

awei01 commented 8 years ago

Took some of @christianalfoni's suggestions and created a POC here: https://github.com/awei01/cerebral-module-router-POC