Add grubs from recipe
This commit is contained in:
parent
cbd44a5756
commit
3a919a5c4c
5 changed files with 44 additions and 19 deletions
|
@ -16,7 +16,8 @@
|
|||
[org.clojure/tools.cli "0.3.1"]
|
||||
[clj-webdriver "0.6.1" :exclusions [org.clojure/core.cache]]
|
||||
[om "0.6.4"]
|
||||
[sablono "0.2.17"]]
|
||||
[sablono "0.2.17"]
|
||||
[cljs-uuid "0.0.4"]]
|
||||
:profiles {:uberjar {:aot :all}}
|
||||
:min-lein-version "2.1.2"
|
||||
:plugins [[lein-cljsbuild "1.0.3"]
|
||||
|
|
|
@ -20,16 +20,16 @@
|
|||
(let [grub (new-grub (:id event) (:grub event) (:completed event))]
|
||||
(assoc-in state [:grubs (:id grub)] grub)))
|
||||
|
||||
(defn assoc-new-grub [current new]
|
||||
(assoc current (:id new)
|
||||
(new-grub (:id new) (:grub new) (:completed new))))
|
||||
|
||||
(defn make-add-grubs-map [grub-events]
|
||||
(reduce assoc-new-grub {} grub-events))
|
||||
(defn map-by-key [key coll]
|
||||
(->> coll
|
||||
(map (fn [a] [(get a key) a]))
|
||||
(into {})))
|
||||
|
||||
(defmethod handle-event :add-grub-list [event state]
|
||||
(let [add-grub-events (:grubs event)
|
||||
add-grubs (make-add-grubs-map add-grub-events)]
|
||||
add-grubs (->> event
|
||||
:grubs
|
||||
(map-by-key :id))]
|
||||
(assoc state :grubs (merge (:grubs state) add-grubs))))
|
||||
|
||||
(defmethod handle-event :update-grub [event state]
|
||||
|
|
|
@ -29,7 +29,8 @@
|
|||
grub-update (chan)
|
||||
grub-clear-all (chan)
|
||||
recipe-add (chan)
|
||||
out (a/merge [grub-add grub-update grub-clear-all recipe-add])
|
||||
recipe-add-grubs (chan)
|
||||
out (a/merge [grub-add grub-update grub-clear-all recipe-add recipe-add-grubs])
|
||||
>events (chan)
|
||||
<events (a/pub >events :type)]
|
||||
(om/root app-view
|
||||
|
@ -39,6 +40,7 @@
|
|||
:grub-update grub-update
|
||||
:grub-clear-all grub-clear-all
|
||||
:recipe-add recipe-add
|
||||
:recipe-add-grubs recipe-add-grubs
|
||||
:>events >events
|
||||
:<events <events}})
|
||||
out))
|
||||
|
|
|
@ -2,16 +2,23 @@
|
|||
(:require [om.core :as om :include-macros true]
|
||||
[sablono.core :as html :refer-macros [html]]
|
||||
[cljs.core.async :as a :refer [<! put! chan]]
|
||||
[grub.view.dom :as dom])
|
||||
[grub.view.dom :as dom]
|
||||
[cljs-uuid.core :as uuid])
|
||||
(:require-macros [grub.macros :refer [log logs]]
|
||||
[cljs.core.async.macros :refer [go go-loop]]))
|
||||
|
||||
(defn add-event [grub]
|
||||
{:event :add-grub
|
||||
:id (str "grub-" (.now js/Date))
|
||||
(defn new-grub [grub]
|
||||
{:id (str "grub-" (uuid/make-random))
|
||||
:grub grub
|
||||
:completed false})
|
||||
|
||||
(defn add-event [grub]
|
||||
(assoc (new-grub grub) :event :add-grub))
|
||||
|
||||
(defn add-list-event [grubs]
|
||||
{:event :add-grub-list
|
||||
:grubs grubs})
|
||||
|
||||
(defn edit-event [id grub]
|
||||
{:event :update-grub
|
||||
:id id
|
||||
|
@ -119,7 +126,7 @@
|
|||
[:span.input-group-btn
|
||||
[:input.form-control#add-grub-input
|
||||
{:type "text"
|
||||
:placeholder "2 grubs"
|
||||
:placeholder "What do you need?"
|
||||
:value new-grub
|
||||
:on-key-up #(when (dom/enter-pressed? %)
|
||||
(add-grub add new-grub owner))
|
||||
|
|
|
@ -2,21 +2,35 @@
|
|||
(:require [om.core :as om :include-macros true]
|
||||
[sablono.core :as html :refer-macros [html]]
|
||||
[cljs.core.async :as a :refer [<! put! chan]]
|
||||
[grub.view.dom :as dom])
|
||||
[cljs-uuid.core :as uuid]
|
||||
[grub.view.dom :as dom]
|
||||
[grub.view.grub :as grub-view])
|
||||
(:require-macros [grub.macros :refer [log logs]]
|
||||
[cljs.core.async.macros :refer [go go-loop]]))
|
||||
|
||||
(defn add-event [name grubs]
|
||||
{:event :add-recipe
|
||||
:id (str "recipe-" (.now js/Date))
|
||||
:id (str "recipe-" (uuid/make-random))
|
||||
:name name
|
||||
:grubs grubs})
|
||||
|
||||
(defn parse-grubs-from-str [grubs-str]
|
||||
(->> grubs-str
|
||||
(clojure.string/split-lines)
|
||||
(map grub-view/new-grub)
|
||||
(into [])))
|
||||
|
||||
(defn add-grubs [add-grubs-ch grubs-str]
|
||||
(let [grubs (parse-grubs-from-str grubs-str)
|
||||
event (grub-view/add-list-event grubs)]
|
||||
(put! add-grubs-ch event)))
|
||||
|
||||
(defn recipe-view [recipe owner]
|
||||
(reify
|
||||
om/IRender
|
||||
(render [this]
|
||||
(let [{:keys [id name grubs]} recipe]
|
||||
(let [{:keys [id name grubs]} recipe
|
||||
add-grubs-ch (om/get-shared owner :recipe-add-grubs)]
|
||||
(html
|
||||
[:div.panel.panel-default.recipe-panel
|
||||
{:id id
|
||||
|
@ -28,7 +42,8 @@
|
|||
:placeholder "Grub pie"
|
||||
:value name}]
|
||||
[:button.btn.btn-primary.btn-sm.recipe-add-grubs-btn
|
||||
{:type "button"}
|
||||
{:type "button"
|
||||
:on-click #(add-grubs add-grubs-ch grubs)}
|
||||
"Add Grubs"]]
|
||||
[:div.panel-body.recipe-grubs.hidden
|
||||
[:textarea.form-control.recipe-grubs-input
|
||||
|
@ -43,7 +58,7 @@
|
|||
(when (and (not (empty? name))
|
||||
(not (empty? grubs)))
|
||||
(om/set-state! owner :new-recipe-name "")
|
||||
(om/set-state! owner :new-recipe-grubs [])
|
||||
(om/set-state! owner :new-recipe-grubs "")
|
||||
(put! ch (add-event name grubs))))
|
||||
|
||||
(defn recipes-view [recipes owner]
|
||||
|
|
Loading…
Reference in a new issue