-
Notifications
You must be signed in to change notification settings - Fork 16
/
transact.cljs
120 lines (106 loc) · 4.08 KB
/
transact.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
(ns todomvc.transact
"Contains functions for manipulating the application data through DataScript"
(:require [datascript :as d]
[cljs-uuid-utils :as uuid]))
(defn log-event [db event & args]
(let [evente {:db/id -100
:event event
:args args}]
(if-let [prev-event-eid (ffirst (d/q '{:find [?e]
:where [[?e :last-event]]}
db))]
[(assoc evente :prev-event prev-event-eid)
[:db.fn/retractAttribute prev-event-eid :last-event]]
[evente])))
(defn set-filter [db this-filter]
[[:db.fn/call log-event :set-filter this-filter]
(let [e (ffirst (d/q '{:find [?e]
:where [[?e :filter]]}
db))]
[:db/add e :filter this-filter])])
(defn seed-item [db id text completed]
[[:db.fn/call log-event :seed-item id text completed]
{:db/id -1
:id id
:text text
:completed completed
:commited true}])
(defn remove-item [db id]
[[:db.fn/call log-event :remove-item id]
(let [e (ffirst (d/q '{:find [?e]
:in [$ ?id]
:where [[?e :id ?id]]}
db id))]
[:db.fn/retractEntity e])])
(defn clear-completed [db]
(let [eids+ids (d/q '{:find [?e ?id]
:where [[?e :completed true]
[?e :id ?id]]}
db)]
(into [[:db.fn/call log-event :clear-completed (map second eids+ids)]]
(for [e (map first eids+ids)]
[:db.fn/retractEntity e]))))
(defn toggle-item
;; Given an application state, toggle the completion status of the
;; item with the specified ID
[db id]
(let [[e completed] (first (d/q '{:find [?e ?completed]
:in [$ ?id]
:where [[?e :id ?id]
[?e :completed ?completed]]}
db id))
completed (not completed)]
[[:db.fn/call log-event :toggle-item id completed]
[:db/add e :completed completed]]))
(defn toggle-all [db]
(let [e+id+completed (d/q '{:find [?e ?id ?completed]
:where [[?e :id ?id]
[?e :completed ?completed]]}
db)
target (not (every? (fn [[_ _ completed]] completed) e+id+completed))]
(into [[:db.fn/call log-event :toggle-all]]
(for [[e id completed] e+id+completed
:when (= completed (not target))]
[:db/add e :completed target]))))
(defn error [db msg]
[[:db.fn/call log-event :error msg]
{:db/id -1
:error msg}])
(defn create-item [db text]
(let [temp-id (uuid/make-random-uuid)]
[[:db.fn/call log-event :create-item temp-id text]
{:db/id -1
:id temp-id
:commited false
:completed false
:text text}]))
(defn commit-item [db temp-id id]
[[:db.fn/call log-event :commit-item temp-id id]
(let [e (ffirst (d/q '{:find [?e]
:in [$ ?id]
:where [[?e :id ?id]]}
db temp-id))]
{:db/id e :id id :commited true})])
(defn start-edit [db id]
[[:db.fn/call log-event :start-edit id]
(let [e (ffirst (d/q '{:find [?e]
:in [$ ?id]
:where [[?e :id ?id]]}
db id))]
[:db/add e :editing true])])
(defn complete-edit [db id text]
(into [[:db.fn/call log-event :complete-edit id text]]
(let [e (ffirst (d/q '{:find [?e]
:in [$ ?id]
:where [[?e :id ?id]]}
db id))]
[[:db/add e :editing false]
[:db/add e :commited false]
[:db/add e :text text]])))
(defn commit-edit [db id]
[[:db.fn/call log-event :commit-edit id]
(let [e (ffirst (d/q '{:find [?e]
:in [$ ?id]
:where [[?e :id ?id]]}
db id))]
[:db/add e :commited true])])