re-frame

BuddhiLW 2024-06-18T20:37:16.913409Z

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)

2024-06-19T12:20:13.473479Z

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... 🙂

BuddhiLW 2024-06-19T16:09:24.230569Z

ass them all! @admin055

2024-06-19T19:40:11.368029Z

Yeah more :as all -> ass hole 🙂

😆 1
p-himik 2024-06-19T21:03:03.404179Z

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!"

p-himik 2024-06-18T22:29:56.818819Z

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]}}))

p-himik 2024-06-18T22:30:52.992339Z

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.

BuddhiLW 2024-06-18T22:36:51.163609Z

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 🙌 🙏

👍 1
p-himik 2024-06-18T22:49:37.102969Z

Also don't shorted any namespaces that deal with some kind of analysis. :D

😄 1
2024-06-20T07:27:25.622489Z

Haha excellent 🙂 https://www.youtube.com/watch?v=lsMKXTXRWIM