Render recipe list
This commit is contained in:
parent
24632d99ce
commit
f11621c501
3 changed files with 69 additions and 17 deletions
|
@ -7,17 +7,17 @@
|
|||
(def default-app-state {:grubs {}
|
||||
:recipes {}})
|
||||
|
||||
(defmulti handle-event (fn [event state] (:event event))
|
||||
(defmulti handle-grub-event (fn [event state] (:event event))
|
||||
:default :unknown-event)
|
||||
|
||||
(defmethod handle-event :unknown-event [event grubs]
|
||||
(defmethod handle-grub-event :unknown-event [event grubs]
|
||||
(logs "Cannot handle unknown event:" event)
|
||||
grubs)
|
||||
|
||||
(defn new-grub [id grub completed]
|
||||
{:id id :grub grub :completed completed})
|
||||
|
||||
(defmethod handle-event :add-grub [event grubs]
|
||||
(defmethod handle-grub-event :add-grub [event grubs]
|
||||
(let [grub (new-grub (:id event) (:grub event) (:completed event))]
|
||||
(assoc grubs (:id grub) grub)))
|
||||
|
||||
|
@ -28,32 +28,59 @@
|
|||
(defn make-add-grubs-map [grub-events]
|
||||
(reduce assoc-new-grub {} grub-events))
|
||||
|
||||
(defmethod handle-event :add-grub-list [event grubs]
|
||||
(defmethod handle-grub-event :add-grub-list [event grubs]
|
||||
(let [add-grub-events (:grubs event)
|
||||
add-grubs (make-add-grubs-map add-grub-events)]
|
||||
(merge grubs add-grubs)))
|
||||
|
||||
(defmethod handle-event :complete-grub [event grubs]
|
||||
(defmethod handle-grub-event :complete-grub [event grubs]
|
||||
(assoc-in grubs [(:id event) :completed] true))
|
||||
|
||||
(defmethod handle-event :uncomplete-grub [event grubs]
|
||||
(defmethod handle-grub-event :uncomplete-grub [event grubs]
|
||||
(assoc-in grubs [(:id event) :completed] false))
|
||||
|
||||
(defmethod handle-event :update-grub [event grubs]
|
||||
(defmethod handle-grub-event :update-grub [event grubs]
|
||||
(assoc-in grubs [(:id event) :grub] (:grub event)))
|
||||
|
||||
(defmethod handle-event :clear-all-grubs [event grubs]
|
||||
(defmethod handle-grub-event :clear-all-grubs [event grubs]
|
||||
{})
|
||||
|
||||
(defmulti handle-recipe-event (fn [event recipes] (:event event))
|
||||
:default :unknown-event)
|
||||
|
||||
(defmethod handle-recipe-event :unknown-event [event recipes]
|
||||
(logs "Cannot handle unknown event:" event)
|
||||
recipes)
|
||||
|
||||
(defn new-recipe [id name grubs]
|
||||
{:id id :name name :grubs grubs})
|
||||
|
||||
(defmethod handle-recipe-event :add-recipe [event recipes]
|
||||
(let [recipe (new-recipe (:id event) (:name event) (:grubs event))]
|
||||
(assoc recipes (:id recipe) recipe)))
|
||||
|
||||
(defmethod handle-recipe-event :add-recipe-list [event recipes]
|
||||
(->> (:recipes event)
|
||||
(map #(new-recipe (:id %) (:name %) (:grubs %)))
|
||||
(reduce (fn [recipes r] (assoc recipes (:id r) r)) recipes)))
|
||||
|
||||
(defmethod handle-recipe-event :update-recipe [event recipes]
|
||||
(->> recipes
|
||||
(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-event event (:grubs state))
|
||||
new-state (assoc state :grubs new-grubs)]
|
||||
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-body new-state)
|
||||
(view/render-app new-state)
|
||||
(recur new-state)))
|
||||
out))
|
||||
|
|
|
@ -296,6 +296,33 @@
|
|||
(dommy/append! recipe-list recipe)
|
||||
recipe))
|
||||
|
||||
(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}
|
||||
[:div.panel-heading.recipe-header
|
||||
[:input.form-control.recipe-header-input
|
||||
{:id "recipe-name"
|
||||
:type "text"
|
||||
:placeholder "Grub pie"
|
||||
:value name}]
|
||||
[:button.btn.btn-primary.btn-sm.recipe-add-grubs-btn
|
||||
{:type "button"}
|
||||
"Add Grubs"]]
|
||||
[:div.panel-body.recipe-grubs.hidden
|
||||
[:textarea.form-control.recipe-grubs-input
|
||||
{:id "recipe-grubs"
|
||||
:rows 3
|
||||
:placeholder "2 grubs"}
|
||||
grubs]
|
||||
[:button.btn.btn-primary.hidden.pull-right.recipe-btn.recipe-done-btn
|
||||
{:type "button"} "Done"]]])))))
|
||||
|
||||
(defn grub-view [grub-state owner]
|
||||
(reify
|
||||
om/IRender
|
||||
|
@ -323,9 +350,6 @@
|
|||
om/IRender
|
||||
(render [this]
|
||||
(let [sorted-grubs (sort-grubs (:grubs state))]
|
||||
(logs "render app-view state:" state)
|
||||
(logs "render app-view owner:" owner)
|
||||
(logs "render grubs:" sorted-grubs)
|
||||
(html
|
||||
[:div.container
|
||||
[:div.row
|
||||
|
@ -357,8 +381,10 @@
|
|||
:placeholder "2 grubs"}]
|
||||
[:button.btn.btn-primary.hidden.pull-right.recipe-btn.recipe-done-btn
|
||||
{:type "button"} "Done"]]]
|
||||
[:ul#recipe-list.list-group.recipe-list]]]])))))
|
||||
[:ul#recipe-list.list-group.recipe-list
|
||||
(for [recipe (vals (:recipes state))]
|
||||
(om/build recipe-view recipe))]]]])))))
|
||||
|
||||
(defn render-body [state]
|
||||
(defn render-app [state]
|
||||
(logs state)
|
||||
(om/root app-view state {:target (.getElementById js/document "container")}))
|
||||
|
|
|
@ -103,7 +103,6 @@
|
|||
(recur))
|
||||
out))
|
||||
|
||||
|
||||
(defmulti handle-event (fn [event recipes] (:event event))
|
||||
:default :unknown-event)
|
||||
|
||||
|
|
Loading…
Reference in a new issue