Fork me on GitHub
#re-frame
<
2020-05-15
>
Spaceman01:05:31

I'm trying to simplify the re-frame reg-event-db function, and I have the following macro so far:

(defmacro db-event [event-key params & body]
    `(do
     (re-frame.core/reg-event-db ~event-key
                                 (fn [~'db [_# ~@params]]
                                   ~@body))
     (defn ~(symbol event-key) ~params (re-frame.core/dispatch [~event-key ~@params]))))
And it works pretty well, in that I can use it like so:
(db-event :remove-from-cart [product-id]
          (assoc-in db [:cart product-id] (-> db :cart (get product-id) dec)))
However, I want to further simply it, and there shouldn't be a need to write assoc-in. So I would like a functionality like this:
(db-event :foo [resp] {:abc {:def resp :efg (more-stuff resp)} :foo1 {:bar1 (stuff resp)}})
instead of :
(db-event :foo [resp]
            (->
           (assoc-in db [:abc :def] resp)
           (assoc-in db [:abc :efg] (more-stuff resp))
           (assoc-in [:foo1 :bar1] (stuff resp))))
So basically, I want to give a map which tells which values are to be changed rather than writing a bunch of assoc-ins in succession. How can I do that?

👍 4
dima16:05:57

db is a map, so the {:abc {:def resp ..} why not just merge them then?

mikethompson01:05:53

Perhaps look up the path interceptor

Spaceman01:05:56

what's that and why would that be needed?

mikethompson01:05:26

It is used in the todomvc sample

Spaceman01:05:29

how does it work?

mikethompson01:05:03

Best if you read the links

Spaceman01:05:33

but can you give an example how path interceptor is used?

mikethompson01:05:45

That's EXACTLY what the links do

Spaceman01:05:22

yeah okay, that's not the point is it? The example shows a single path (path :todos), but what I need is to give a map which contains all the keys and the values that are to be changed. Really more to do with macros than with re-frame.