-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathservices.cljs
142 lines (128 loc) · 5.95 KB
/
services.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(ns todomvc.services
(:require [todomvc.transact :as t]
[ajax.core :refer [GET POST PUT] :as ajax-core]
[goog.dom :as gdom]
[datascript :as d]))
(defn DELETE
"Not yet in cljs-ajax 0.2.3"
[uri & [opts]]
(ajax-core/ajax-request uri "DELETE" (ajax-core/transform-opts opts)))
(defn todos-url []
(str (.. js/window -location -pathname) "/todos"))
(defn csrf-token []
(-> (goog.dom.getElement "csrf-token")
(.getAttribute "value")))
(defn error-handler [conn]
(d/transact! conn [[:db.fn/call t/error "fail"]]))
(defmulti handle
(fn [event args db conn] event))
(defmethod handle :create-item
[_ [temp-id text] db conn]
(POST (todos-url)
{:params {:id temp-id
:text text}
:handler (fn [res]
(.log js/console (str "Succesful res: " res))
(let [id (:id res)]
(d/transact! conn [[:db.fn/call t/commit-item temp-id id]])))
:error-handler (fn [res]
(.log js/console (str "FAil res: " res))
(error-handler conn))
:headers {"X-CSRF-Token" (csrf-token)}}))
(defmethod handle :complete-edit
[event [id text] db conn]
(PUT (todos-url)
{:params {:id id
:text text}
:handler (fn [res]
(.log js/console (str "Succesful res for complete-edit: " res " id is " id " text is " text))
(d/transact! conn [[:db.fn/call t/commit-edit id]]))
:error-handler (fn [res]
(.log js/console (str "Fail res: " res))
(error-handler conn))
:format (merge (ajax-core/edn-request-format)
{:read (fn [res]
(let [res-text (.getResponseText res)]
(when (pos? (count res-text))
(throw (js/Error. (str "Assumed no content response has content: " res-text))))))
:description "EDN (CUSTOM)"})
:headers {"X-CSRF-Token" (csrf-token)}}))
(defmethod handle :toggle-item
[event [id completed] db conn]
(PUT (todos-url)
{:params {:id id
:completed completed}
:handler (fn [res]
;; nothing todo
(.log js/console (str "Succesful res: " res)))
:error-handler (fn [res]
(.log js/console (str "Fail res: " res))
(error-handler conn))
:format (merge (ajax-core/edn-request-format)
{:read (fn [res]
(let [res-text (.getResponseText res)]
(when (pos? (count res-text))
(throw (js/Error. (str "Assumed no content response has content: " res-text))))))
:description "EDN (CUSTOM)"})
:headers {"X-CSRF-Token" (csrf-token)}}))
(defmethod handle :remove-item
[event [id] db conn]
(DELETE (todos-url)
{:params {:id id}
:handler (fn [res]
;; nothing todo
(.log js/console (str "Succesful res: " res)))
:error-handler (fn [res]
(.log js/console (str "Fail res: " res))
(error-handler conn))
:format (merge (ajax-core/edn-request-format)
{:read (fn [res]
(let [res-text (.getResponseText res)]
(when (pos? (count res-text))
(throw (js/Error. (str "Assumed no content response has content: " res-text))))))
:description "EDN (CUSTOM)"})
:headers {"X-CSRF-Token" (csrf-token)}}))
(defmethod handle :clear-completed
[event [ids] db conn]
;; todo make batch delete enpoint and use that
(doseq [id ids]
(DELETE (todos-url)
{:params {:id id}
:handler (fn [res]
;; nothing todo
(.log js/console (str "Succesful res: " res)))
:error-handler (fn [res]
(.log js/console (str "Fail res: " res))
(error-handler conn))
:format (merge (ajax-core/edn-request-format)
{:read (fn [res]
(let [res-text (.getResponseText res)]
(when (pos? (count res-text))
(throw (js/Error. (str "Assumed no content response has content: " res-text))))))
:description "EDN (CUSTOM)"})
:headers {"X-CSRF-Token" (csrf-token)}})))
(defmethod handle :toggle-all
[event _ db conn]
(doseq [[id completed] (d/q '{:find [?id ?completed]
:in [$ ?tx]
:where [[?e :id ?id]
[?e :completed ?completed ?tx]]}
db (:max-tx db))]
(handle :toggle-item [id completed] db conn)))
(defmethod handle :default
[_ _] nil)
(defn start-services [app]
(let [{:keys [conn]} app]
(d/listen! conn (fn [{:keys [db-after] :as report}]
(let [[event args] (first (d/q '{:find [?event ?args]
:in [$ ?tx]
:where [[?e :event ?event ?tx]
[?e :args ?args]]}
db-after (:max-tx db-after)))]
(handle event args db-after conn))))
(GET (todos-url)
{:handler (fn [res]
(doseq [{:keys [id text completed] :as todo} (:todos res)]
(d/transact! conn [[:db.fn/call t/seed-item id text completed]])))
:error-handler (fn [res]
(error-handler conn))})))