Fork me on GitHub
#re-frame
<
2021-07-18
>
zackteo22:07:19

(rf/reg-event-fx
 :filter
 (fn [{:keys [db]} _]
   {:http-xhrio {:method :get
                 :uri ""
                 :params {:request @(rf/subscribe [:request])
                          :constraints @(rf/subscribe [:constraints])}
                 :response-format (ajax/json-response-format {:keywords? true})
                 :on-success [:success-http-result]
                 :on-failure [:failure-http-result]}}))
Hello, I have a event that makes a database call as shown. I believe there are issues with what I am doing in the :params . May I know what should I be doing instead of :constraints @(rf/subscribe [:constraints])} ? Do I need to write it as json first? Am thinking I am doing something wrong as it seems my constraints map in the re-frame db ...
[{:name "Land Take"
  :constraint :land-take
  :details {:min "100", :max "500"}}
 {:name "Total GFA"
  :constraint :land-gfa
  :details {:min "100", :max "500"}}]
seems to be passed as
{:name ["Land Take" "Total GFA"]
 :constraint ["land-take" "total-gfa"]
 :details {:min ["100" "100"], :max ["500" "1000"]}}

p-himik22:07:55

First of all, don't use subscribe inside event handlers: http://day8.github.io/re-frame/FAQs/UseASubscriptionInAnEventHandler/ Second of all, IIRC :params is a map which for :get requests without any request format will be converted into query parameters, so the values there must be plain scalars, like numbers, strings, etc.

p-himik22:07:45

Ah, my bad - seems like the values can be vectors. By default, {:a [1 2]} will be converted into a=1&a=2.

zackteo23:07:47

Right, so I should use the 3rd party library?

p-himik23:07:36

I simply wouldn't use subscriptions there at all. Just extract the values from the db directly.

zackteo23:07:05

Meaning ... {:filter request constraints] ?

p-himik23:07:37

I have no idea what you mean by that. Given the DB above, what query string do you want to get?

zackteo23:07:58

{:constraints [{:name "Land Take"
                :constraint :land-take
                :details {:min "100", :max "500"}}
               {:name "Total GFA"
                :constraint :land-gfa
                :details {:min "100", :max "500"}}]
 :request nil}
My db is like this? I want both :constraints and :request I don't quite understand what you mean by "Just extract the values from the db directly"

zackteo23:07:56

I was trying to ask if you mean for me to pass the data to the my filter event like {:filter request constraints]

p-himik23:07:07

How do you expect :constraints to be represented in a query string?

zackteo23:07:33

I was hoping there would be a way to translate :constraints as it is, like

[{:name "Land Take"
  :constraint :land-take
  :details {:min "100", :max "500"}}
 {:name "Total GFA"
  :constraint :land-gfa
  :details {:min "100", :max "500"}}]
or a way for me to perhaps write to json and read such that my backend receives the data such a format

p-himik23:07:35

There are many such ways. I'd argue that query string is not a good fit here. If you have control over the type of the response you can use, switch from GET to POST and just encode it as regular Transit.

zackteo23:07:46

so a content-type of {"Content-type" "application/transit+json"} ?