51 lines
1.4 KiB
Clojure
51 lines
1.4 KiB
Clojure
(ns auto-ap.effects
|
|
(:require-macros [cljs.core.async.macros :refer [go]])
|
|
(:require [re-frame.core :as re-frame]
|
|
[cljs-http.client :as http]
|
|
[cljs-time.coerce :as c]
|
|
[cljs-time.core :as time]
|
|
[cljs.core.async :refer [<!]]
|
|
[auto-ap.history :as p]
|
|
[pushy.core :as pushy]))
|
|
|
|
(re-frame/reg-fx
|
|
:redirect
|
|
(fn [uri]
|
|
(println uri)
|
|
(pushy/set-token! p/history uri)))
|
|
|
|
(re-frame/reg-fx
|
|
:set-local-storage
|
|
(fn [[name value]]
|
|
(if value
|
|
(.setItem js/localStorage name value)
|
|
(.removeItem js/localStorage name ))))
|
|
|
|
(defn dates->date-times [x]
|
|
(cond (map? x)
|
|
(into {} (map (fn [[k v]]
|
|
[k (if (instance? js/Date v)
|
|
(time/to-default-time-zone (c/from-date v))
|
|
v)])
|
|
x))
|
|
(list? x)
|
|
(map dates->date-times x)))
|
|
|
|
(re-frame/reg-fx
|
|
:http
|
|
(fn [{:keys [method uri on-success body headers token]}]
|
|
(go
|
|
(let [headers (if token
|
|
(assoc headers "Authorization" (str "Token " token))
|
|
headers)]
|
|
(println headers)
|
|
(->> (http/request {:method method
|
|
:body body
|
|
:headers headers
|
|
:url uri})
|
|
(<! )
|
|
:body
|
|
(dates->date-times)
|
|
(conj on-success)
|
|
(re-frame/dispatch))))))
|