dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

Redirect to a component with URL path parameters #150

Closed bugged84 closed 5 years ago

bugged84 commented 5 years ago

@dsuryd I found your comments in this other issue, but they don't quite address my scenario. Do you have an example of redirecting to a template such as the one below?

this.RegisterRoutes("chat-rooms", new List<RouteTemplate>
{
    new RouteTemplate("ChatRoomList") { UrlPattern = string.Empty },
    new RouteTemplate("ChatRoom") { UrlPattern = "(/:id)" }
});

Using a <RouteLink> component works fine to route to a chat room from a list of links, but what if I want to redirect the user to a specific chat room based on a message sent from a MultiCaseVM?

componentDidUpdate() {
    if (this.state.NewChatRoomId) {
        let chatRoomId = this.state.NewChatRoomId;
        dotnetify.react.router.redirect( ??? );
    }
}

What should I replace the question marks with?

bugged84 commented 5 years ago

I see this in the console log when arriving at my landing page.

router> map /chat-rooms to template id=ChatRoomList
router> map /chat-rooms(/:id) to template id=ChatRoom
router> routing /chat-rooms
router> route '' to template id=ChatRoomList
router> routed
bugged84 commented 5 years ago

I can make it work with the following.

window.location.replace("chat-rooms/" + chatRoomId);

This seems undesirable, though, when I have route templates configured.

dsuryd commented 5 years ago

The redirect accepts a path. Try: dotnetify.react.router.redirect('chat-rooms/' + chatRoomId).

dsuryd commented 5 years ago

The other way is to use vm.$routeTo(<route object>) where is a predefined object created in the server-side view model using this.GetRoute(<template name>, <url pattern>).

bugged84 commented 5 years ago

@dsuryd I had tried passing that same path to dotnetify.react.router.redirect but the function fails saying that length cannot be read on an undefined viewModels parameter.

bugged84 commented 5 years ago

image

image

dsuryd commented 5 years ago

Now that I thought about it, redirect is more of an internal method, not meant to be used directly. Can you try the vm.$routeTo instead.

On Fri, Nov 23, 2018 at 11:31 AM bugged84 notifications@github.com wrote:

[image: image] https://user-images.githubusercontent.com/23638686/48958352-fc43dd80-ef23-11e8-8b9b-4be3e20593fb.png

[image: image] https://user-images.githubusercontent.com/23638686/48958364-18477f00-ef24-11e8-87df-1baa562a680b.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dsuryd/dotNetify/issues/150#issuecomment-441307779, or mute the thread https://github.com/notifications/unsubscribe-auth/AOS8koaSmS83C1vtQ7iHGqmj40YiROlzks5uyE0lgaJpZM4Yw-ZC .

bugged84 commented 5 years ago

Not sure vm.$routeTo will work for me then, since you said it requires a predefined route object on the view model. My route to a new chat room can't be predefined, as it can't be known until a user creates it at run-time.

dsuryd commented 5 years ago

You can create an initial Route object that you store in the component state. Then clone the object, modify its urlPattern property to point to the desired chat room Id , and pass it to vm.$routeTo.

On Fri, Nov 23, 2018 at 12:05 PM bugged84 notifications@github.com wrote:

Not sure vm.$routeTo will work for me then, since you said it requires a predefined route object on the view model. My route to a new chat room can't be predefined, as it can't be known until a user creates it at run-time.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dsuryd/dotNetify/issues/150#issuecomment-441311468, or mute the thread https://github.com/notifications/unsubscribe-auth/AOS8kndWXnRwuNZ3mHq_fw1YICxIImerks5uyFT3gaJpZM4Yw-ZC .

dsuryd commented 5 years ago

Closing this as the question has been answered.