-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathex3.cljs
65 lines (58 loc) · 2.61 KB
/
ex3.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
(ns todos-async.ex3
(:require
[cljs.core.async :as async
:refer [<! >! chan close! sliding-buffer put! alts!]]
[jayq.core :refer [$ append ajax html $deferred $when done resolve pipe on] :as jq]
[jayq.util :refer [log]]
[crate.core :as crate]
[clojure.string :refer [join blank?]]
[todos-async.chan-utils :refer [click-chan form-submit-chan async-some get-next-message]])
(:require-macros [cljs.core.async.macros :as m :refer [go alt!]]))
(defn modal-form [{:keys [mode task-form] :as state}]
(if mode
[:div.modal-form
[:h4 "Add Task"]
[:form.new-task-form
[:input.new-task-name.form-control {:type "text"
:value (:content task-form)
:name "content"
:placeholder "New Task"}]
[:p
[:input {:type "submit" :value "Save" :class "btn btn-primary"}]
[:a {:href "#" :class " cancel-new-todo btn btn-default"} "cancel"]]]]))
(defn todo-task [idx task]
[:li (:content task)])
(defn todo-list [{:keys [todo-list] :as state}]
[:div
[:p
[:a {:href "#" :class "new-todo btn btn-primary"} "Add task"]]
[:ul {:class "todo-list list-unstyled"}
(map-indexed todo-task todo-list)]
(modal-form state)])
(defn render-templates [state]
(-> ($ "#example3")
(html (crate/html (todo-list state)))))
(defn add-task [{:keys [todo-list] :as state} task]
(assoc state :todo-list (conj todo-list task)))
(defn app-loop [start-state]
(let [ new-todo-click (click-chan "#example3 a.new-todo" :new-task)
cancel-new-form-click (click-chan "#example3 a.cancel-new-todo" :cancel-new-form)
task-form-submit (form-submit-chan "#example3 .new-task-form"
:task-form-submit [:content])
input-chan (async/merge [new-todo-click
cancel-new-form-click
task-form-submit])]
(go
(loop [state start-state]
(render-templates state)
(<! (get-next-message #{:new-task} input-chan))
(render-templates (assoc state :mode :add-todo-form))
(let [[msg-name msg-data] (<! (get-next-message #{:task-form-submit :cancel-new-form}
input-chan))]
(recur
(condp = msg-name
:cancel-new-form (dissoc state :mode)
:task-form-submit (-> state
(add-task msg-data)
(dissoc :mode))
)))))))