Last active
April 29, 2019 08:15
-
-
Save vbedegi/b7292af981020d128d48dd2218468746 to your computer and use it in GitHub Desktop.
elm in cljs
This file contains hidden or 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
; util | |
(defn tag-dispatch [dispatch tag] | |
(fn [msg] | |
(let [tagged (if (vector? tag) | |
(conj tag msg) | |
[tag msg])] | |
(dispatch tagged)))) | |
; counter | |
(defn init-counter [value] | |
value) | |
(defn render-counter [model dispatch] | |
[:div (str model) | |
[:button {:on-click #(dispatch :inc)} "INC!"] | |
[:button {:on-click #(dispatch :dec)} "DEC!"]]) | |
(defn update-counter [model msg] | |
(match msg | |
:inc | |
(inc model) | |
:dec | |
(dec model))) | |
; container | |
(defn init-container [top bottom] | |
{:top (init-counter top) | |
:bottom (init-counter bottom)}) | |
(defn render-container [model dispatch] | |
[:div | |
(render-counter (:top model) (tag-dispatch dispatch :top)) | |
(render-counter (:bottom model) (tag-dispatch dispatch :bottom)) | |
[:button {:on-click #(dispatch :reset) "reset!"}]]) | |
(defn update-container [model msg] | |
(match msg | |
:reset | |
(init-container 0 0) | |
[:top msg] | |
(assoc model :top (update-counter (:top model) msg)) | |
[:bottom msg] | |
(assoc model :bottom (update-counter (:bottom model) msg)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment