Skip to content

Improve navigation with query parameters #14013

Closed

Description

@mvysny

Describe your motivation

Vaadin 14.8.11

Currently, if I want to navigate to a view by java class name and I want to add a query parameter, I need to write the following code:

RouteConfiguration configuration = RouteConfiguration
        .forRegistry(UI.getCurrent().getInternals().getRouter().getRegistry());
String url = configuration.getUrl(BookingFreightView.class, HasUrlParameterFormat.getParameters(bookingReference));
Map<String, List<String>> queryParameters = Collections.singletonMap("prev_view", Collections.singletonList(prevView.name()));
UI.getCurrent().navigate(url, new QueryParameters(queryParameters));

Describe the solution you'd like

Please provide an additional overload for navigation: UI.navigate(Class, Object, QueryParameters) and also a static factory QueryParameters.of(String, String) so that the code can read:

UI.getCUrrent().navigate(BookingFreightView.class, bookingReference, QueryParameters.of("prev_view", prevView.name()));

Also provide QueryParameters.toString() and equals()/hashCode(), similar to RouteParameters.

Alternatives considered

QueryParameter.simple() still needs a map even though it's a bit better; QueryParameter.fromString() could work but will suffer from escaping issues.

For the massive UI.navigate() overload - perhaps a builder pattern could help? Not sure though, they all require a trailing go() call, which, when omitted, will simply run no navigation - that will be a frequent source of bugs. Not recommended:

UI.getCurrent().navigateTo(it -> it.view(Foo.class, param).queryParam("foo", "bar"))

or

new Navigation().route(Foo.class, param).queryParam("foo", "bar").go();

Activity

  1. knoobie commented on Jun 21, 2022

    @knoobie
    Contributor

    @mstahv while you are currently cleaning up the router api ;) This could probably be a really interesting small topic for you as well with a huge benefit in DX.

  2. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    Let's consider 😎 I noticed in my previous patch that working with QueryParameter would indeed have some low hanging fruits for DX enhancements.

    @mvysny What is the Object in UI.navigate(Class, Object, QueryParameters) that you are proposing?

  3. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    BTW. I'd guess the code examples by @mvysny might not be there if the latest patch was already in a release 🤓

  4. knoobie commented on Jun 22, 2022

    @knoobie
    Contributor

    I partly agree :) With your addition really complex objects can be easily exchanged without leveraging the URLs - that's true! But there are still use-cases where a bookmark-able URLs with query parameter are desired, like for example online portals with deep linking, search or filtering forms.

  5. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    There sure is! And even if the "new approach was used" one might still want to update the deep linking URL and use QueryParameters with enhanced API.

  6. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    Now I actually figured out what the second object in UI.navigate(Class, Object, QueryParameters)was. @mvysny is using both "route parameters" and query parameters at the same time.

  7. mvysny commented on Jun 22, 2022

    @mvysny
    MemberAuthor

    Eh, sorry, my bad, typing too fast. It's the parameter for the view. Should be T or something :)

  8. added a commit that references this issue on Jun 22, 2022
    3ae6311
  9. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    @mvysny Would this work 🤔 Didn't test yet or write any tests, just drafted the APIs and implementation. #14034

  10. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    Looks like navigate(Class,QueryParameters) is missing as well. Should maybe add that in the same UX improvement patch as well 🤔

  11. mvysny commented on Jun 22, 2022

    @mvysny
    MemberAuthor

    @mstahv That would work very well for my use-case, thank you. While you're working on it, could you please add:

    1. hashCode()/equals()/toString() for QueryParameters
    2. navigate(BookingFreightView.class, QueryParameters.of("prev_view", prevView.name())); - parameterless navigation which would help for other use-cases. Even though this will most probably clash with other navigate() overloads.
  12. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    Added the missing navigate method.

    Why do you need the equals/hashCode implementations? toString definitely would ease debugging.

  13. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    toString ✅

  14. knoobie commented on Jun 22, 2022

    @knoobie
    Contributor

    Having proper equals and hashCode methods could probably be really helpful for the upcoming unit testbench to allow checking of params before and after navigation

  15. mstahv commented on Jun 22, 2022

    @mstahv
    Member

    Fair enough. Added simple solution delegating to the parameter maps.

  16. mstahv commented on Jun 23, 2022

    @mstahv
    Member

    Added tests, PR ready. At the enhancement didn't come directly from needs, would appreciate if @mvysny & @knoobie can check the improvements are as expected!

  17. mvysny commented on Jun 23, 2022

    @mvysny
    MemberAuthor

    Looks perfect from my side, thanks a ton @mstahv

  18. added a commit that references this issue on Jun 29, 2022
    c7e5ebe
  19. vaadin-bot commented on Sep 7, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 23.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions