Add grubs on enter or click

This commit is contained in:
Nicholas Kariniemi 2014-06-29 23:26:31 +03:00
parent b1d1f99231
commit fe6514a5f5
2 changed files with 63 additions and 34 deletions

View file

@ -11,7 +11,6 @@
:default :unknown-event)
(defmethod handle-grub-event :unknown-event [event grubs]
(logs "Cannot handle unknown event:" event)
grubs)
(defn new-grub [id grub completed]
@ -49,7 +48,6 @@
:default :unknown-event)
(defmethod handle-recipe-event :unknown-event [event recipes]
(logs "Cannot handle unknown event:" event)
recipes)
(defn new-recipe [id name grubs]
@ -69,18 +67,14 @@
(assoc-in [(:id event) :name] (:name event))
(assoc-in [(:id event) :grubs] (:grubs event))))
(defn update-state-and-render [remote-chan]
(let [out (chan)]
(go-loop [state default-app-state]
(let [event (<! remote-chan)
new-grubs (handle-grub-event event (:grubs state))
new-recipes (handle-recipe-event event (:recipes state))
new-state (assoc state
:grubs new-grubs
:recipes new-recipes)]
(logs "event:" event)
(logs "new-state")
(logs new-state)
(view/render-app new-state)
(recur new-state)))
out))
(defn update-state-and-render [remote]
(go-loop [state default-app-state]
(let [event (<! (a/merge [remote view/out]))
new-grubs (handle-grub-event event (:grubs state))
new-recipes (handle-recipe-event event (:recipes state))
new-state (assoc state
:grubs new-grubs
:recipes new-recipes)]
(view/render-app new-state)
(recur new-state)))
view/out)

View file

@ -1,14 +1,18 @@
(ns grub.view
(:require [om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]])
(:require-macros [grub.macros :refer [log logs]]))
[sablono.core :as html :refer-macros [html]]
[cljs.core.async :as a :refer [<! put! chan]])
(:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]]))
(def add (chan))
(def out add)
(defn recipe-view [recipe owner]
(reify
om/IRender
(render [this]
(let [{:keys [id name grubs]} recipe]
(log "render recipe view: " recipe)
(html
[:div.panel.panel-default.recipe-panel
{:id id}
@ -79,23 +83,51 @@
(defn sort-grubs [grubs]
(sort-by (juxt :completed get-grub-ingredient) (vals grubs)))
(defn add-grub-event [grub]
{:event :add-grub
:id (str "grub-" (.now js/Date))
:grub grub
:completed false})
(defn add-grub [add owner]
(let [new-grub (.-value (om/get-node owner :new-grub))]
(when (not (empty? new-grub))
(put! add (add-grub-event new-grub)))))
(defn enter-pressed? [event]
(let [enter-keycode 13]
(= (.-which event) enter-keycode)))
(defn add-grub-on-enter [add owner event]
(when (enter-pressed? event)
(add-grub add owner)))
(defn grubs-view [grubs owner]
(reify
om/IRender
(render [this]
(html
[:div
[:h3 "Grub List"]
[:div.input-group.add-grub-input-form
[:span.input-group-btn
[:input.form-control#add-grub-input {:type "text" :placeholder "2 grubs"}]]
[:button.btn.btn-primary {:id "add-grub-btn" :type "button"} "Add"]]
[:ul#grub-list.list-group
(for [grub (sort-grubs grubs)]
(om/build grub-view grub))]
[:button.btn.hidden.pull-right
{:id "clear-all-btn" :type "button"}
"Clear all"]]))))
(let [add (:add (om/get-shared owner))]
(html
[:div
[:h3 "Grub List"]
[:div.input-group.add-grub-input-form
[:span.input-group-btn
[:input.form-control#add-grub-input
{:ref :new-grub
:type "text"
:placeholder "2 grubs"
:on-key-up #(add-grub-on-enter add owner %)}]]
[:button.btn.btn-primary
{:id "add-grub-btn"
:type "button"
:on-click #(add-grub add owner)}
"Add"]]
[:ul#grub-list.list-group
(for [grub (sort-grubs grubs)]
(om/build grub-view grub))]
[:button.btn.hidden.pull-right
{:id "clear-all-btn" :type "button"}
"Clear all"]])))))
(defn app-view [state owner]
(reify
@ -110,4 +142,7 @@
(om/build recipes-view (:recipes state))]]]))))
(defn render-app [state]
(om/root app-view state {:target (.getElementById js/document "container")}))
(om/root app-view
state
{:target (.getElementById js/document "container")
:shared {:add add}}))