-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.cljs
114 lines (94 loc) · 3.25 KB
/
commands.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
(ns todomvc.commands
(:require [clojure.spec.alpha :as s]
[datascript.core :as d]
[mook.core :as m]
[orchestra.core :refer-macros [defn-spec]]
[promesa.core :as p]
[todomvc.boundaries.ui :as b-ui]
[todomvc.boundaries.todo :as b-todo]
[todomvc.stores :as stores]))
(defn-spec create-new-todo>> p/promise?
{:private true}
[data (s/merge ::stores/stores*
(s/keys :req [:todo/title]))]
(d/transact! (::stores/app-db* data)
[(merge (select-keys data [:todo/title])
{:todo/completed? false
:todo/created-at (js/Date.)})])
(p/resolved (dissoc data :todo/title)))
(def <create-new-todo>>
(m/wrap create-new-todo>>))
;; ---
(defn-spec toggle-todo-status>> p/promise?
{:private true}
[data (s/merge ::stores/stores*
(s/keys :req [:db/id]))]
(let [app-db* (::stores/app-db* data)]
(when-let [{:todo/keys [completed?]} (d/pull @app-db* [:todo/completed?] (:db/id data))]
(d/transact! app-db* [(merge (select-keys data [:db/id])
{:todo/completed? (not completed?)})])))
(p/resolved (dissoc data :db/id)))
(def <toggle-todo-status>>
(m/wrap toggle-todo-status>>))
;; ---
(defn-spec destroy-todo>> p/promise?
{:private true}
[data (s/merge ::stores/stores*
(s/keys :req [:db/id]))]
(d/transact! (::stores/app-db* data)
[[:db.fn/retractEntity (:db/id data)]])
(p/resolved (dissoc data :db/id)))
(def <destroy-todo>>
(m/wrap destroy-todo>>))
;; ---
(defn-spec clear-completed-todos>> p/promise?
{:private true}
[data ::stores/stores*]
;; ---
(let [app-db* (::stores/app-db* data)]
(as-> @app-db* <>
(d/q '[:find [?e ...]
:where [?e :todo/completed? true]]
<>)
(mapv #(do [:db.fn/retractEntity %]) <>)
(d/transact! app-db* <>)))
(p/resolved data))
(defn <clear-completed-todos>> []
(as-> (m/wrap clear-completed-todos>>) <>
(<> {})))
;; ---
(defn-spec toggle-all>> p/promise?
{:private true}
[data (s/merge ::stores/stores*
(s/keys :req [::b-ui/all-completed?]))]
(let [app-db* (::stores/app-db* data)
all-completed? (::b-ui/all-completed? data)]
(as-> @app-db* <>
(d/q '[:find [?e ...]
:where [?e :todo/completed?]]
<>)
(mapv #(do {:db/id %
:todo/completed? (not all-completed?)})
<>)
(d/transact! app-db* <>)))
(p/resolved (dissoc data ::b-ui/all-completed?)))
(def <toggle-all>>
(m/wrap toggle-all>>))
;; ---
(defn-spec set-filter>> p/promise?
{:private true}
[data (s/merge ::stores/stores*
(s/keys :req [::b-ui/active-filter]))]
(swap! (::stores/local-store* data) merge (select-keys data [::b-ui/active-filter]))
(p/resolved (dissoc data ::b-ui/active-filter)))
(def <set-filter>>
(m/wrap set-filter>>))
;; ---
(defn-spec update-todo>> p/promise?
{:private true}
[data (s/merge ::stores/stores*
(s/keys :req [:db/id :todo/title]))]
(d/transact! (::stores/app-db* data) [(select-keys data [:db/id :todo/title])])
(p/resolved (dissoc data :db/id :todo/title)))
(def <update-todo>>
(m/wrap update-todo>>))