re-frame

jherrlin 2024-10-10T14:26:32.742199Z

Hi, I'm working in a Re-frame app with a lot of fetching, using https://github.com/superstructor/re-frame-fetch-fx I would like to create a higher level abstraction on top of the:

(rf/reg-event-fx ::fetch-A)
(rf/reg-event-fx ::fetch-A-success)
(rf/reg-event-fx ::fetch-A-failure)
Anyone done something like that?

p-himik 2024-10-10T14:29:03.876319Z

Yes: https://github.com/ingesolvoll/re-chain

👍 1
p-himik 2024-10-10T14:29:10.368549Z

Haven't used myself though.

jherrlin 2024-10-10T14:31:19.149169Z

Thx! Will take a look

isak 2024-10-10T19:27:10.657059Z

We made one. Here is an example usage:

(xf/reg-request-event-fx
  ::sign-out
  (fn [_]
    {:path "/auth/signout"
     :method "POST"})
  (fn [{:keys [db]} success? resp]
    (cond
      success?
      (set! (.-location js/window) "/")

      :else
      (log/error :when "signing out" :response resp))
    {:db db}))
I think it cleans things up a lot because you don't want stuff like that strewn across 3 different top level definitions.

jherrlin 2024-10-10T20:04:48.243479Z

@isak Interesting! Does that register 3 events?

isak 2024-10-10T20:08:37.092629Z

@jherrlin We just did 2 for ours, since success and fail was handled by the same function. But if you wanted them separate, then yea 3.

adriel 2024-10-15T11:02:01.713999Z

I built something similar on top of re-frame-http-fx — but I haven’t looked at it in years. Might serve as some inspiration if nothing else. Example usage can be found at https://github.com/adrielsantiago/re-frame-helpers/blob/master/docs/simplified-http-requests.md. Main difference is I wanted the include the loading state to make the ui easier to build:

;; dispatchers.cljs file
(ns some.dispatchers
  (:require [re-frame-helpers.macros :refer-macros [defxhrio]))

(defxhrio
  :get-something
  (fn []
    {:http-xhrio {:uri "fetch/some/data"}})

;; core.cljs file
(ns some.core)

(defn frontend-component []
  (let [{:keys [response loading? error]} @(rf/subscribe [:get-something])]
    [:button
      {:on-click #(rf/dispatch [:get-something])}
      "Show me some data"]
    (cond
      loading? ;; show progress bar
      error    ;; show the user an error message
      response ;; show the response data