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?Haven't used myself though.
Thx! Will take a look
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.@isak Interesting! Does that register 3 events?
@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.
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