Created
August 18, 2023 10:12
-
-
Save slifin/5ef1774047e516efe11ef249b3ad1e9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns app.todo-list | |
(:require #?(:clj [app.xtdb-contrib :as db]) | |
[hyperfiddle.electric :as e] | |
[hyperfiddle.electric-dom2 :as dom] | |
[hyperfiddle.electric-ui4 :as ui] | |
[xtdb.api #?(:clj :as :cljs :as-alias) xt])) | |
(e/def !xtdb) | |
(e/def db) ; injected database ref; Electric defs are always dynamic | |
(e/defn TodoItem [id] | |
(e/server | |
(let [e (xt/entity db id) | |
!true-count (atom 0) | |
true-count (e/watch !true-count)] | |
(e/client | |
(dom/div | |
(dom/div (dom/props {:for id :class "count"}) | |
(dom/text (e/server (:task/count e)))) | |
(dom/div (dom/text (str true-count))) | |
(ui/button | |
(e/fn [] | |
(e/server | |
(swap! !true-count inc) | |
(e/discard (e/offload | |
#(xt/submit-tx !xtdb [[:xtdb.api/put | |
(update e :task/count inc)]]))))) | |
(dom/text "increment")) | |
(ui/button | |
(e/fn [] | |
(e/server | |
(reset! !true-count 0) | |
(e/discard (e/offload | |
#(xt/submit-tx !xtdb [[:xtdb.api/put | |
(assoc e :task/count 0)]]))))) | |
(dom/text "reset"))))))) | |
#?(:clj | |
(defn todo-records [db] | |
(->> (xt/q db '{:find [(pull ?e [:xt/id :task/description])] | |
:where [[?e :task/status]]}) | |
(map first) | |
(sort-by :task/description) | |
vec))) | |
(e/defn Todo-list [] | |
(e/server | |
(binding [!xtdb user/!xtdb | |
db (new (db/latest-db> user/!xtdb))] | |
(e/client | |
(dom/div | |
(ui/button | |
(e/fn [] | |
(e/server (e/discard (e/offload | |
#(xt/submit-tx !xtdb [[:xtdb.api/put {:xt/id (random-uuid) | |
:task/description "hello world" | |
:task/count 0 | |
:task/status :active}]]))))) | |
(dom/text "create record")) | |
(e/server | |
(e/for-by :xt/id [{:keys [xt/id]} (e/offload (partial todo-records db))] | |
(TodoItem. id)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment