Home > other >  Routing dilemma - what's the best architectural approach?
Routing dilemma - what's the best architectural approach?

Time:10-19

I have a dilemma I'm trying to resolve. What do you think the best approach is from best architectural practice and maintainability point of view? We have Module A and Module B. Module B has form components that are being reused in the application, specifically by Module A. Module A has to route to Module B's forms. For reasons too long to explain, relative routing doesn't work. So when you cancel or save the form, it takes you to the parent B. This works fine for when user routes from B, but not from A. We want the user to go back to the page they were on before.

Solution 1: Break form components into a separate module C. Since it's a reusable component it deserves its own module. A routes to C, B routes to C, relative route will take them back. Downsides: a lot of code changes.

Solution 2: Pass variable for parent in query params. The form will read query param and know where to route after it saves or get canceled, don't have to worry about relative routes. For example: www.example.com/products/product-id-1?redirectRoute=products-page

Downsides: there are many links to add query params to, so quite a few code changes, as well. We can't use forward slash in query params so we would have to use a variable. Also, we now we have only A and B using the forms, but what if we will have Modules D and F in the future, we would need to create an enum for this variable.

Solution 3: Try to fix existing relative routing. It may not be possible.

CodePudding user response:

Do you need to create relative routing? You can't use /path/to/moduleB in A?

When I construct my route I always think in terms of relation of those routes. So if forms in Module A and Module B somehow related, maybe there goes another form and what you gonna do about it? I would go with this module C that contains domain logic of those forms and maybe create a state/service that is responsible for managing state of your forms. For the sake of Single Responsibility Principle I would go that way.

CodePudding user response:

The best solution and one that you can build on to manage all routing as regards your form and their respective component is to use state. From the actions you can pass the next route or previous route and route your application in the state.

Also, you should consider putting the form in a shared module that way it makes your code neater and easier to read, understand and build upon by other developers

  • Related