Fork me on GitHub
#fulcro
<
2021-06-12
>
lgessler20:06:09

anybody got a solution for using http query params with fulcro? path params ("/user/123") jive well with fulcros dynamic router but im not immediately sure how to handle a query param ("/user?id=123")

dvingo20:06:43

I think you have to use a separate routing/URL lib - I've used them successfully via reitit . In will-enter I call a fn in reitit to get the current match and the query params are supplied by reitit

👍 2
lgessler21:06:28

how do you incorporate the query params into fulcro's state atom after you've grabbed them like that? triggering a mutation on the load for route-deferred perhaps?

dvingo15:06:36

yea I think that makes sense. I've also broken the rules by transacting a mutation from within :will-enter because in my case it is an idempotent mutation and calling route-immediate after the mutation, but probably not the best idea. I still have trouble figuring out the lifecycles of the routing system...

nivekuil15:06:17

personally I just bypassed nearly all of fulcro's routing, using only route-immediate and handling everything else (loading, caching, etc.) through a reitit layer

nivekuil15:06:17

one reason iirc is that the url <-> route segment system breaks down when you want nested routers sharing the same param, something like /user/:id/panel1, your route segments have to be like ["user" :id] ["panel1" :id]

dvingo16:06:52

do you use reitit controllers?

nivekuil16:06:39

haven't seen those actually. I just have a big (50loc) on-navigate, not too complex though it was tricky to get right, e.g. for clerk and bfcache. I have each route tell me what it needs to load, and then on-navigate does the loading/caching/scrolling logic with a top-level view of what it needs

dvingo16:06:55

ah cool, that makes sense

lgessler14:06:17

thanks both, that's interesting. probably i'll just do it like @U051V5LLP did for now but @U797MAJ8M's solution is attractive for its consistency

dvingo14:06:15

I found the code I was thinking of, :pre-merge is another way to get query param state into the fulcro db:

:pre-merge     (fn [{:keys [data-tree current-normalized]}]
                    (let [{{{:keys [task-id]} :path
                            {:keys [open?]}   :query} :parameters
                           {route-name :name}         :data} (fr/current-reitit-match SPA)]
                      (merge
                        {:ui/open? (and
                                     (= :task-detail route-name)
                                     (= task-id (:task/id data-tree)) open?)}
                        current-normalized
                        data-tree)))