I want to write a re-frame event, but I don't know exactly how I should do it. This is the behavior I'm looking for:
;; after backend gives response with token (already implemented)
(fn [token]
(go
(let [err (<p! (ass/set-item! "token" token))]
(if (nil? err)
(rf/dispatch [:get-profile-data-and-store-it token]) ;; side-effect: redirects user to home-page (view: home-page customized with user-data)
(rf/dispatch [:warn-user-to-retry-to-login-and-logout-user]))))) ;; side-effect: redirects user to home-page (view: generic + warning modal)
Haha. It reminds me that sometimes when debugging some map destructuration, I use something like :
(let [{:keys [id name] :as all}]
(prn all)
...)
And in french, we tend to pronounce as with "s" not "z", so... 🙂ass them all! @admin055
Yeah more :as all -> ass hole 🙂
Heh, was playing Red Dead Redemption 2 recently. There's a French guy whose English is so-so. "People tell me I am... how do you say it?.. whole ass!"
That's a job not for an event but for an effect.
Also, unless you already use core.async in other places for purposes other than promise handling, I would definitely recommend not using it for promises and instead relying on plain interop.
In this case, I'd make ass/set-item! (heh, questionable namespace alias) return a promise that either gets resolved with nil or gets rejected with an error. Then I'd write something like this:
(rf/reg-fx :set-item
(fn [{:keys [id value on-success on-error]}]
(-> (ass/set-item! k v)
(.then (fn [result]
(when on-success
(rf/dispatch (conj on-success result)))))
(.catch (fn [error]
(when on-error
(rf/dispatch (conj on-error error))))))))
(rf/reg-event-fx :set-token
(fn [_ [token]]
{:set-item {:id "token"
:value token
:on-success [:get-profile-data-and-store-it token]
:on-error [:warn-user-to-retry-to-login-and-logout-user]}}))Alternatively, if you never use set-item! anywhere else, you can just use it where you need it without wrapping the call in a re-frame effect.
I didn't notice the irony in the namespace together of how it sounds with it's function 😂 Language barrier anti-serendipity patterns! Thanks for the help! That explanation was insightful. I will try it out. I have read this section of the re-frame documentation, but I didn't know how to apply it 🙌 🙏
Also don't shorted any namespaces that deal with some kind of analysis. :D
Haha excellent 🙂 https://www.youtube.com/watch?v=lsMKXTXRWIM