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

Advanced Routing Panel in "Other" tab #705

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open

Conversation

jacob-xhio
Copy link
Contributor

This PR addresses the advanced case of the following issue: #458. A video sample of the functionality working is shown below, along with a screenshot of the final appearance of the tab.

2024-03-15.11-55-04.mov
Screenshot 2024-03-15 at 12 10 32 PM

Copy link
Member

@haynesjm42 haynesjm42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional things to consider for our advanced routing example(s):


A pattern I've been using for the obfuscated/encoded route params is this:

Using these encoder/decoder methods on the route to automatically handle the encoding without needing to do anything in the tab/component.

// Encoding of json route params as base64
export const routeParamEncoders = {
    encodeParams: params => {
        if (isEmpty(params)) return {};
        return {q: window.btoa(JSON.stringify(params))};
    },
    decodeParams: params => {
        if (!params.q) return {};
        return JSON.parse(window.atob(params.q));
    }
};

This also requires putting the router query params in "loose" mode (which just means you can put whatever you want in the params and router5 wont validate/modify/etc.)

XH.router.setOption('queryParamsMode', 'loose');

Then in the actual route definition you can do this:

...

{
    name: 'myRoute',
    path: '/my-route',
    ...routeParamEncoders
},
...

With this your component code doesn't need to worry about the encoding at all, or even what the route is configured to use for params - it just gets the json object params and puts whatever json object it wants into the params.


Another thing that can be important for tabs which allow you to modify something in a non-modal way is to add handlers to intercept deactivation of a route so you can prompt the user to save changes, which can be done like this:

XH.router.canDeactivate(route, () => (toState, fromState, doneFn) => {
    if (this.isModified) {
        XH.confirm(...).then(confirmed => {
            if (confirmed) doneFn();
        }
    } else {
        doneFn();
    }
});

Also note that this apparently needs to be added each time the route is activated, so it should go into a reaction on the route after checking that the current route is the one for your tab/component (just like when parsing route params - would go in the same place)

This is usually done in combination with adding a beforeunload handler on the window to make a similar check for avoiding losing unsaved changes.

window.addEventListener('beforeunload', e => {
    // Ignore if we aren't the current page or if we don't have pending changes
    if (!XH.routerState.name.endsWith('myRoute') || !this.isModified) {
        delete e.returnValue;
        return;
    }

    e.preventDefault();
    e.returnValue = '';
});

Copy link
Member

@amcclain amcclain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, a few small comments but looking very close / let's get it in today 👍

client-app/src/desktop/AppModel.ts Outdated Show resolved Hide resolved
client-app/src/desktop/AppModel.ts Show resolved Hide resolved
@amcclain
Copy link
Member

If I open the advanced example, adjust some params, then swap to another tab, I see the decoded route params now in my URL:
Screenshot 2024-03-26 at 4 32 52 PM

@amcclain
Copy link
Member

@jacob-xhio - it looks like the "revent route deactivation" switch is not working, or at least I could not work out what it was supposed to do while testing out the app. Have a look please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants