Fork me on GitHub
#fulcro
<
2022-08-15
>
Ernesto Garcia15:08:44

[still beginner] In a tip at @holyjak’s https://fulcro-community.github.io/guides/tutorial-advanced-minimalist-fulcro/index.html#_routing, it says that you can build a basic router component by doing a simple case. I am trying to do that, but I am unsure how to query and pass the proper entity for a parameterized route. Like:

(defsc MyRouter [this {:ui/keys [route] :keys [persons] :as props}]
  {:query [:ui/route
           {:persons (comp/get-query Person)}
           ??? any additional query in here. I can't just write (comp/get-query Person) ???]}
  (case (first route)
    :persons (ui-persons {:persons persons})
    :person (ui-person ??? how to pass the proper person here, depending on the route [:person <id>] ???)))
Would anybody maybe have an example on how this is done?

Ernesto Garcia17:08:24

Is it proper if I use follow-ref like this?

(case (first route)
    :persons (ui-persons {:persons persons})
    :person (fdn/follow-ref (fulcro-app/current-state this)
                 [:person/id (second route)])))

Ernesto Garcia17:08:10

Or maybe:

(fdn/db->tree 
    (comp/get-query Person)
    [:person/id (second route)]
    (current-state)

Jakub Holý (HolyJak)12:08:17

I said it is simple, not efficient :-) You need to query for all persons and in the body pluck the right one.

Ernesto Garcia12:08:00

Right. So fdn/db->tree is a good way to do that?

Jakub Holý (HolyJak)12:08:24

I'd rather not pick things manually from the client DB inside a component. It breaks the simplicity of Fulcro (Root query + DB -> props, each comp a pure fn of props). You already have :persons and can simply (->> persons (find #(= (:id %) <id from route>)) first), no?

Ernesto Garcia12:08:49

Yeah, that's feasible. And yes, querying directly the DB at this point assumes that MyRouter would know that the Person object down the line is normalized, which is not really among its responsibilities.

Ernesto Garcia12:08:11

But well, I guess these things are taken care of by using Fulcro Routing.

1
Ernesto Garcia12:08:49

Thanks for your response, btw!

🙏 1