Skip to content

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

Closed
@JWorthe

Description

@JWorthe

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions