Fork me on GitHub
#re-frame
<
2019-10-30
>
chadhs15:10:42

when using secretary with reframe is it a common pattern to dispatch multiple events? such as

(defroute "/:id-token" [id-token]
  (re-frame/dispatch [::events/set-id-token id-token])
  (re-frame/dispatch [::events/set-active-panel :home-panel))

valtteri15:10:44

In my opinion it’s better to dispatch just one event. That event can have other events as side-effects (for instance :dispatch [::events/set-active-panel]).

xfyre16:10:12

I think it’s more or less the same thing - depends on the level where you want to control it

dominicm16:10:05

My understanding is that you're supposed to dispatch based on the user's intent, rather than the internal meaning (e.g. of panels). So you should dispatch a "page-navigate-event", and the dispatch handler for that will have conditional behavior for setting the id token.

chadhs16:10:22

so that single event handler could in theory update multiple db fields?

chadhs16:10:43

if that id-token field is nil im making a call, but i only want to make that call if it’s nil

valtteri16:10:06

What is the user doing at that point?

valtteri16:10:16

Why is she navigating to that route

chadhs16:10:34

the user is going to the home page, if the id-token is not present in the db the page redirects to an sso auth page and redirects back to the home-page with an #id-token1234 appended to it

chadhs16:10:44

so it’s on initial load really

chadhs16:10:17

so when visiting / the first time, you’ll end up returning to /#token-id and i want that to load the same content as visiting / but with that token-id stored in the app state db

valtteri16:10:43

In that case I’d probably have single event called ::show-home-page and it can take id-token as a param. Event handler can then set active panel to the db and trigger other side-effects via effects-map.

valtteri16:10:45

In the event handler you can do something like (when id-token [… do something conditional ])

valtteri16:10:05

I hope this makes sense to you!

chadhs16:10:36

thanks @valtteri i think that helps; will be working on this throughout the week and will definitely post an update. 🙂

👍 4
valtteri16:10:53

Your event handlers could look something like this

(re-frame/reg-event-fx
 ::show-home-page
 (fn [_ [_ id-token]]
   {:dispatch-n
    [[::set-active-panel :main-panel]
     (when id-token [::save-id-token id-token])]}))

(re-frame/reg-event-db
 ::set-active-panel
 (fn [db [_ panel]]
   (assoc db :active-panel panel)))

(re-frame/reg-event-db
 ::save-id-token
 (fn [db [_ id-tokenl]]
   (assoc db :id-token id-token)))

chadhs16:10:44

oh cool thanks, will take a look at this