-
Notifications
You must be signed in to change notification settings - Fork 0
/
effects.cljs
123 lines (101 loc) · 4.29 KB
/
effects.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(ns appkit.effects
(:require [appkit.citrus.core :as citrus]
[appkit.api :as api]
[appkit.storage :as storage]
[appkit.cookie :as cookie]
[goog.net.cookies]
[clojure.string :as str]))
(defmulti dispatch! (fn [_ effect]
(type effect)))
(defmethod dispatch! Keyword [r event & args]
(apply citrus/dispatch! r event args))
(defmethod dispatch! PersistentArrayMap [r effects & oargs]
(doseq [[effect [event & args]] effects]
(apply dispatch! r event (concat args oargs))))
(defmethod dispatch! :default [r & args]
)
;; cookie ==========================================================
(defn cookie [opts r [op k v]]
(case op
;; session
:set (cookie/cookie-set k v)
:set-forever (cookie/cookie-set-forever k v opts)
:remove (cookie/cookie-remove k)))
;; http ==========================================================
(def refresh-times (atom 0))
(defn http [api-host r {:keys [endpoint params on-load on-error method type headers
on-progress on-upload on-download keywordize?]
:or {method :post
type :transit
on-error :citrus/default-error
on-progress nil
on-upload nil
on-download nil
keywordize? true
}
:as options}]
(api/fetch api-host {:endpoint endpoint
:params params
:method method
:type type
:headers headers
:on-progress on-progress
:on-upload on-upload
:on-download on-download
:keywordize? keywordize?
:on-success (fn [result]
(if (vector? on-load)
(apply dispatch! r (conj on-load result))
(dispatch! r on-load result)))
:on-error (fn [{:keys [status] :as resp}]
(cond
(= status 401)
(citrus/dispatch! r :user/show-signin-modal?)
:else
(if (vector? on-error)
(apply dispatch! r (conj on-error resp))
(dispatch! r on-error resp))))
}))
(defmulti local-storage (fn [_ params] (:action params)))
(defmethod local-storage :get [r {:keys [key on-success on-error]}]
(if-let [data (storage/get key)]
(dispatch! r on-success data)
(dispatch! r on-error)))
(defmethod local-storage :set [r {:keys [key value]}]
(storage/set key value))
(defmethod local-storage :conj [_ {:keys [key value]}]
(let [old (storage/get key)]
(storage/set key (vec (remove nil? (distinct (conj old value)))))))
(defmethod local-storage :disj [_ {:keys [key value]}]
(when-let [old (storage/get key)]
(storage/set key (vec (remove #(= % value) old)))))
(defmethod local-storage :assoc [_ {:keys [key assoc-key assoc-value]}]
(let [old (storage/get key)]
(when (or (nil? old)
(map? old))
(storage/set key (assoc old assoc-key assoc-value)))))
(defmethod local-storage :dissoc [_ {:keys [key assoc-key]}]
(when-let [old (storage/get key)]
(when (map? old)
(storage/set key (dissoc old assoc-key)))))
(defmethod local-storage :remove [_ {:keys [key]}]
(storage/remove key))
(defmethod local-storage :default [r & args]
(prn "local-storage debug: " {:args args}))
(defn redirect
[r route]
(citrus/dispatch! r :router/push route true))
(defn- set-timeout [t f]
(js/setTimeout f t))
(defn timeout
[r {:keys [duration events]}]
(set-timeout duration
(fn []
(apply citrus/dispatch! r events))))
(defn dispatch [r events]
(let [events (if (and (vector? events)
(keyword? (first events)))
[events]
events)]
(doseq [event-vector events]
(apply citrus/dispatch! r event-vector))))