-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Per-block custom user confirm #484
Conversation
Adds capability to customize user confirm on a per-block level.
#489. Ok... i'm not the only one thinking like this :D |
Not really sure why the tests fail though, it's only on the Mobile Safari 8.0.0 browser and says in the log: |
I believe that this is exactly what is needed to solve remix-run/react-router#5254 by providing a callback-like API to react-router's RR could, inside the |
I don't like the fact that |
@mjackson the current way that Allowing the passing of a function (or maybe a Promise) would enable user-space APIs to configure the block deeper in the application. Such as in the following psuedo react app which needs to save data if it's not already saved prior to allowing the route to update: // main
<Router>
<div>
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</div>
</Router>
// Foo.jsx
class Foo {
componentDidMount() {
this.props.history.block((callback) => {
if (someDataNeedsSaving) {
displaySomeCustomUi()
saveSomeData()
.then(() => callback(true)))
.catch(() => callback(false))
}
callback(true)
})
}
render() {
<Link to="/bar" />
}
}
export default withRouter(Foo)
// Bar.jsx
class Foo {
componentDidMount() {
this.props.history.block((callback) => {
if (someOTHERDataNeedsSaving) {
displaySomeDIFFERENTCustomUi()
saveSomeOTHERData()
.then(() => callback(true)))
.catch(() => callback(false))
}
callback(true)
})
}
render() {
<Link to="/foo" />
}
}
export default withRouter(Bar) |
@mjackson My goal is to be able to use a dialog with three buttons to:
Since the getUserConfirmation is created very early, you have inject pre-callback functions by possibly storing them in global variables, that then need to be updated anytime a React-Router is mounted and unmounted. I haven't tried this solution but I wouldn't make it this way unless I had to. |
I think a per-block confirmation function is probably the right way to go. A little history: I created the So I'd definitely welcome a change to this API :) However, @Karagoth I'm a little confused about the API you're going with here. Instead of history.block((location, action) => (callback) => ...) why don't we just go with history.block((location, action, callback) => ...) ? |
Can users register multiple blocker functions? let unblock1 = history.block((location, action, callback) => ...)
let unblock2 = history.block((location, action, callback) => ...) |
No, they can't currently. I think that's part of the problem this PR is trying to address. |
Apologies, I wasn't clear — I was asking about the proposed API. Would you allow for the |
Yes, I think so. I need to go through the use cases. |
@mjackson glad to hear back from you. The suggested In principal I have no issue with the I will ponder it and see if I can find some time in the weekend to make the change to make sure the suggested API still solves my problem (it likely will). |
Anything I can do to help move this along? Seems like an elegant solution as long as the user is comfortable with a curried function. |
@aselbie You can make a PR with tests that pass and a bunch of clearly defined use cases. The lack of use cases here is what's stalling this one for me. I'm closing this PR since it's stalled. But I'm definitely open to having a per-block confirmation function. Just needs someone who's willing to do the work to push it through. |
The use cases were fleshed out in #493, most fully in this comment. The only material differences between this proposal is backwards compatibility. This keeps the name "block", and enables the desired API through a new argument type so no one has to change their code. You weren't for that proposal, I didn't think you'd be for this one either. I still believe an async pre-change hook would be a positive addition to the library. The router docs for scroll restoration have described the shortcoming for a long time. |
I found the need to have more information available at user-confirm time than a string could provide, and also the need to customize further than only a different message. This change adds the capability to curry a custom user-confirm function when creating the block, which makes the task easy.