Fork me on GitHub
#re-frame
<
2020-11-07
>
Lucy Wang02:11:27

@p-himik I just found that yesterday and wrote a custom version of reg-global-interceptor to work around that

(ns demo
  (:require [re-frame.core :as rf]
            [re-frame.settings :as rf.settings]))

(defn safe-reg-global-interceptor
  "rf/reg-global-interceptor appends instead of replacing. This wrapper
  function ensures replacing the existing global interceptor with the
  same id, if any."
  [{:keys [id] :as interceptor}]
  (let [global-interceptors (rf.settings/get-global-interceptors)]
    (->> global-interceptors
         (remove #(= (:id %) id))
         ;; global-interceptors is a queue
         (into (empty global-interceptors))
         (swap! rf.settings/store assoc :global-interceptors))
    (rf/reg-global-interceptor interceptor)))

p-himik06:11:09

Nice! One issue though - your code removes and appends the interceptor. It doesn't preserve the order.

p-himik06:11:44

Here's what I've come up with (didn't test it that well but seems to work just fine):

(def reg-global-interceptor
  (if re-frame.interop/debug-enabled?
    (fn [interceptor]
      (let [existing-interceptors (re-frame.settings/get-global-interceptors)]
        (if (some #(= (:id %) (:id interceptor)) existing-interceptors)
          (do
            (rf/clear-global-interceptor)
            (doseq [i existing-interceptors]
              (rf/reg-global-interceptor
                (if (= (:id i) (:id interceptor))
                  interceptor
                  i))))
          (rf/reg-global-interceptor interceptor))))
    rf/reg-global-interceptor))

p-himik10:11:05

And it was fixed rather quickly. :)

bastilla19:11:52

Hi there. Can anyone point my to a blog entry / article / whatever with code snippets on how to upload a file to a server? Would greatly appreciate it. (As so often I fail utterly in finding actual / official docs on clojure libs.

bastilla19:11:53

What I found so far on re-frame is not going into details. Just superficial stuff with lots of text but little of documentation and actual code snippets.

p-himik20:11:00

I assume you mean HTTP POST. Do you know how to upload files using vanilla JS?

bastilla09:11:39

Hey @p-himik Thanks for responding! Well, yes and no. I know but not from the top of my head. I need to look up the details. I am pretty sure I could implement it in plain JS in like 15 minutes. And I am sure could do it in Python in like 15 minutes - and I don't know Python - since it's easy to find the docs.

herald10:11:11

You do it the same way as you do with JS. Just use ClojureScript's JS interop https://cljs.info/cheatsheet/

herald10:11:59

Only difference is with re-frame, you'd probably have the fetch function (i.e. the HTTP POST) as an effect called from an event handler (ex. https://github.com/day8/re-frame-http-fx). The process of creating formdata and passing it onto the body of the POST is the same though.

herald11:11:17

And yes, you won't be able to search for specific problems and find snippets ready for copy-and-paste with Clojure, as you can with the mainstream languages. You're kind of on your own to put the pieces together, but don't worry as this is a skill you'll get better at with practice!

bastilla12:11:53

thanks so far guys. I'll post my solution.

👍 3
p-himik13:11:38

I use re-frame-http-fx and in the :body parameter I put a manually created js/FormData.

👍 3