Last active
March 17, 2023 12:28
-
-
Save avidrucker/35b7118b65f64c989f24ae4e6d948da8 to your computer and use it in GitHub Desktop.
a Reagent, Hiccup, and ClojureScript to-do list app study
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
;; ## Maria Dot Cloud Code Sketch: todo-mvp.cljs | |
;; Instructions: Run this entire notebook twice to evaluate the code and load the tiny to-do list apps. There are two versions of it; one that currently works with `defcell` and one that doesn't (yet) work. | |
;; [This code snippet](https://gist.github.com/avidrucker/35b7118b65f64c989f24ae4e6d948da8) will work in Klipse *if and only if* the URL is [http://app.klipse.tech/?container=1](http://app.klipse.tech/?container=1) | |
;; Inspired originally by [this Hiccup/Reagent demo by OrgPad](https://orgpad.com/o/BSZ05PGN1PMbIjmwIT3Vhs?s=clojure-tutorial) | |
(cell (html ((g-todo-app)))) | |
(cell (html ((todo-app)))) | |
;; Question: Is the following code not available in Maria Dot Cloud? If it isn't available in Maria Dot Cloud. what is the next best alternative to use to get Reactive Atom behavior in Maria Dot Cloud? Must I use `defcell` or are there any ways I can use a form-3 component which manages its own state? | |
#_(require '[reagent.core :as r]) | |
*ns* | |
(doc html) | |
(doc atom) | |
(doc cell) | |
;; Question: How can I introspect Maria Dot Cloud's notebook environment to see what functions and libraries are available to me to use? | |
#_(ns-publics 'clojure.string) | |
(doc chia.view.hiccup/element) | |
(doc atom) | |
(what-is chia.view.hiccup) | |
chia.reactive | |
(defn todo-app [] | |
"Renders a list of all todos. | |
Done todos are crossed. | |
Deleted todos are hidden from display." | |
(let [todos (atom | |
[{:text "Start Clojure tutorial" :id 0} | |
{:text "Finish Clojure tutorial" :id 1} | |
{:text "Create a video for it" :id 2} | |
{:text "Fix bugs in OrgPad" :id 3}]) | |
done-items (atom #{0}) | |
input-val (atom "")] | |
(fn [] | |
(let [dummy1 @todos | |
dummy2 @done-items] | |
[:section {:style {:margin "0"}} | |
[:span {:style {:font-weight 600}} "[This does not work yet] List of todos for today:"] | |
[:ul | |
(for [todo @todos] | |
(when (not (:deleted todo)) | |
^{:key (:id todo)} | |
[:li (when (contains? @done-items (:id todo)) | |
{:style {:text-decoration "line-through"}}) | |
[:span {:style {:margin-right "0.25em"}} (:text todo)] | |
(if (contains? @done-items (:id todo)) | |
[:button | |
{:on-click #(swap! done-items disj (:id todo)) | |
:style {:padding "0.25em" | |
:margin-bottom "0.5em"}} "↺"] | |
[:button | |
{:on-click #(swap! done-items conj (:id todo)) | |
:style {:padding "0.25em" | |
:margin-bottom "0.5em"}} "✔"]) | |
[:button | |
{:on-click #(swap! todos update-in [(:id todo) :deleted] nil?) | |
:style {:padding "0.25em" | |
:margin-bottom "0.5em"}} "×"] | |
]))] | |
[:input {:type "text" | |
:placeholder "Enter new item text here" | |
:value @input-val | |
:on-change #(reset! input-val (-> % .-target .-value))}] | |
[:button | |
{:on-click #(let [new-todo {:text @input-val | |
:id (count @todos)}] | |
(swap! todos conj new-todo) | |
(reset! input-val ""))} "Add item"] | |
])))) | |
((todo-app)) | |
(defcell g-todos [{:text "Start Clojure tutorial" :id 0} | |
{:text "Finish Clojure tutorial" :id 1} | |
{:text "Create a video for it" :id 2} | |
{:text "Fix bugs in OrgPad" :id 3}]) | |
(defcell g-done-items #{0}) | |
(defcell g-input-val "") | |
(defn g-todo-app [] | |
"Renders a list of all todos. | |
Done todos are crossed. | |
Deleted todos are hidden from display." | |
(let [] | |
(fn [] | |
(let [dummy1 @g-todos | |
dummy2 @g-done-items] | |
[:section {:style {:margin "0"}} | |
[:span {:style {:font-weight 600}} "[working via global state] List of todos for today:"] | |
[:ul | |
(for [todo @g-todos] | |
(when (not (:deleted todo)) | |
^{:key (:id todo)} | |
[:li (when (contains? @g-done-items (:id todo)) | |
{:style {:text-decoration "line-through"}}) | |
[:span {:style {:margin-right "0.25em"}} (:text todo)] | |
(if (contains? @g-done-items (:id todo)) | |
[:button | |
{:on-click #(swap! g-done-items disj (:id todo)) | |
:style {:padding "0.25em" | |
:margin-bottom "0.5em"}} "↺"] | |
[:button | |
{:on-click #(swap! g-done-items conj (:id todo)) | |
:style {:padding "0.25em" | |
:margin-bottom "0.5em"}} "✔"]) | |
[:button | |
{:on-click #(swap! g-todos update-in [(:id todo) :deleted] nil?) | |
:style {:padding "0.25em" | |
:margin-bottom "0.5em"}} "×"] | |
]))] | |
[:input {:type "text" | |
:placeholder "Enter new item text here" | |
:value @g-input-val | |
:on-change #(reset! g-input-val (-> % .-target .-value))}] | |
[:button | |
{:on-click #(let [new-todo {:text @g-input-val | |
:id (count @g-todos)}] | |
(swap! g-todos conj new-todo) | |
(reset! g-input-val ""))} "Add item"] | |
])))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
remove global vars