Skip to content
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

Is there a canonical way to block navigation to warn about unsaved changes? #459

Closed
JWorthe opened this issue May 20, 2020 · 3 comments · Fixed by #460
Closed

Is there a canonical way to block navigation to warn about unsaved changes? #459

JWorthe opened this issue May 20, 2020 · 3 comments · Fixed by #460
Assignees
Labels
bug Something isn't working

Comments

@JWorthe
Copy link

JWorthe commented May 20, 2020

Hi there. I'm relatively new to Seed, but have been having a great time so far.

I'm trying to add a feature to my Seed app that warns you if you're about to navigate away from a page and lose work that you haven't saved yet. Is there a way to intercept the url changing event and block it?

Looking at the docs, I came across the UrlRequested message. I expected something like this to work, but url_request.handled_and_prevent_refresh() doesn't seem to prevent the default behaviour of navigating to the requested page.

fn init(orders: &mut impl Orders<Msg>) -> PageModel {
    let url_request_handle = orders.subscribe_with_handle(Msg::UrlRequested);
    PageModel {
        url_request_handle
    }
}

enum Msg {
    UrlRequested(subs::UrlRequested),
}

pub fn update(
    msg: Msg,
    model: &mut PageModel,
    orders: &mut impl Orders<Msg>,
) {
    Msg::UrlRequested(subs::UrlRequested(url, url_request)) => {
        let unsaved_changes: bool = model.has_unsaved_changed();
        if unsaved_changes {
            model.show_the_are_you_sure_modal();
            url_request.handled_and_prevent_refresh();
        }
    }
}

Am I on completely the wrong path? I've tried digging into the Seed code to understand why this doesn't do what I expect, and I suspect the problem is that orders.subscribe doesn't let me set the priority of my subscription to ensure that it happens before the routing engine.

@MartinKavik MartinKavik self-assigned this May 21, 2020
@MartinKavik MartinKavik added the bug Something isn't working label May 21, 2020
@MartinKavik
Copy link
Member

MartinKavik commented May 21, 2020

@JWorthe Congratulations, you found two Seed bugs 👍

  1. I wrote a function to setup low priority for system handlers, but forgot to integrate it.
  2. It still won't work after fixed (1.) because all subscribe handlers are invoked before the generated messages are processed by your update function (in other words - all handlers are called and collected into message queue). It was ok when we expected that all handlers are almost without side-effects and return messaged, but that's no longer true.

Working on it.

@MartinKavik
Copy link
Member

@JWorthe #460 should resolve it - please test it if you have time.
I've also added new example unsaved_changes (you can run it from the root with cargo make start unsaved_changes). There there are two modal dialogs - browser's one (when user wants to go to external address); and confirm invoked from the app (when user wants to change page within the website).

@JWorthe
Copy link
Author

JWorthe commented May 21, 2020

Hi @MartinKavik. Thank you so much for jumping on this. I've played with your example and that's exactly what I was after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants