reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
serg 2021-03-31T09:56:53.018300Z

Hi, I am trying to setup nested UI routes with reitit, I want to achieve having following structure:

<TopMenuNav> --> contains top level routes, for example `/home` and `/settings`
<Router>

Then I want to have Settings page with it's own MenuNav and child routes, for example `/settings/users`, `/settings/roles` etc.
<TopMenuNav>
<SettingsMenuNav>
<SettingsChildRouter>
Is there is a way to define it like this:
(def routes
  [["/"
    {:name ::home
     :public? true
     :view pages/home-page}]

   ["/settings"
    {:name ::settings
     :view pages/settings-page}
     ["/users" {:name ::users
                :view pages/users-page}]
     ]
   ])
Then when I push state (rfe/push-state :my-app.routes/users) it navigates me to the child route. Basically, I want to achieve having nested routes with it’s own shared menus without defining it inside each child component. I couldn’t find something similar, any help appreciated.

serg 2021-03-31T10:03:06.019500Z

Currently the error I have is missing route :my-app.routes/users the router doesn’t recognise nested routes definitions. I also tried set it up like this:

(def routes
  [["/"
    {:name ::home
     :public? true
     :view pages/home-page}]

   ["/settings"
    {:name ::settings
     :view pages/settings-page
     :router (rf/router [
                         ["/users" {:name ::users
                                    :view pages/users-page}]
                         ])
     }]
   ])

serg 2021-03-31T10:04:52.020100Z

The app is started this way:

(rfe/start!
    (rf/router routes {:data {:coercion rss/coercion}})
    set-route!
    {:use-fragment true})

lassemaatta 2021-03-31T10:09:47.021700Z

I don't have a solution to your problem, but have you checked out https://github.com/metosin/reitit/blob/master/doc/advanced/composing_routers.md#nesting-routers ? your latter example with :router looks similar to what's proposed in the doc, but the docs also describe a recursive-match-by-path method for resolving such nested routes

serg 2021-03-31T10:12:25.022Z

Yes, I saw it, and used as it was suggested there with :router however, I don’t use explicitly match-by-path , in my case I am just passing the router to the rfe/start! which is reitit.frontend.easy

serg 2021-03-31T10:13:16.022300Z

and then I use (rfe/push-state :my-app.routes/users) to navigate to routes

serg 2021-03-31T10:16:27.023800Z

Is there is an example of using nested routes with recursive-match-by-path for front end, since I have found only passing the router to reitit.frontend.easy/start! and match being done somewhere underneath.

dharrigan 2021-03-31T11:29:28.024100Z

Okay, I'm having some strange behaviour...

dharrigan 2021-03-31T11:29:35.024400Z

I have this defined as a route:

dharrigan 2021-03-31T11:29:39.024600Z

(defn routes
  [app-config]
  ["/session" {:parameters {:query specs/company-id}}
   ["" {:get {:handler (create app-config)
              :parameters {:query specs/create}
              :swagger {:produces [session-create-api-version]}}}]])

dharrigan 2021-03-31T11:29:54.024900Z

firing up the app, I get this error:

dharrigan 2021-03-31T11:30:12.025100Z

; (err) Execution error (ExceptionInfo) at reitit.exception/exception (exception.cljc:19).
; (err) count not supported on this type: Keyword

dharrigan 2021-03-31T11:30:27.025500Z

if I comment out the :parameters on the :get, it starts up

dharrigan 2021-03-31T11:30:45.026Z

which leads me to suspect that my spec is wrong, yet it's very simple:

dharrigan 2021-03-31T11:30:57.026200Z

(def create [:map
             {:closed true}
             [:repId string?]
             [:clientPhoneNumber string?]])

dharrigan 2021-03-31T11:31:47.026600Z

No idea why this is failing to start, if :parameters is part of the :get

dharrigan 2021-03-31T11:33:28.026900Z

The m/validate passes: (m/validate create {:repId "ac56a265-6b79-4da2-8438-c5bcf0bb0798" :clientPhoneNumber "1234567890"}) ;; true

juhoteperi 2021-03-31T11:43:04.027800Z

@dharrigan Reitit isn't currently able to merge Malli schemas in route-data: https://github.com/metosin/reitit/issues/422 Probably something related to this.

dharrigan 2021-03-31T11:44:11.028100Z

oooh

dharrigan 2021-03-31T11:44:12.028300Z

thank you

dharrigan 2021-03-31T11:44:36.028600Z

I shall adapt 🙂

dharrigan 2021-03-31T11:54:14.028900Z

It seems to be okay if the key is different, i.e.,

dharrigan 2021-03-31T11:54:20.029100Z

(defn routes
  [app-config]
  ["/session" {:parameters {:query specs/company-id}}
   ["" {:post {:handler (create app-config)
               :parameters {:body specs/create}
               :swagger {:produces [session-create-api-version]}}}]])

dharrigan 2021-03-31T11:54:30.029400Z

that works, since parameters is a query and a body

juhoteperi 2021-03-31T11:57:38.031300Z

Yeah it can merge :parameters map using normal merge. Just merging two Mallis requires malli.core/merge call, which Reitit doesn't yet know how to do. Other ways to avoid is to just move all parameters to the route directly. And you can also use ^:replace on route to avoid merging if there is some schemas on the tree.

dharrigan 2021-03-31T11:58:00.031600Z

thank you 🙂

dharrigan 2021-03-31T11:59:17.032500Z

btw, I was thinking of inviting the chat logging bot (I think it's a zulip bot) to this channel, as I don't see it logged, therefore helpful messages disappear after a while (I was scrolling back to see if I could find information on the empty map handling)

dharrigan 2021-03-31T11:59:23.032700Z

I suppose that would be okay?

juhoteperi 2021-03-31T12:45:47.032900Z

Sure

dharrigan 2021-03-31T12:46:17.033200Z

actually, they are already here - the zulip bot and the log bot

dharrigan 2021-03-31T12:46:22.033400Z

so all is well 😄