Add grubs from recipe

This commit is contained in:
Nicholas Kariniemi 2014-07-28 06:32:01 +03:00
parent cbd44a5756
commit 3a919a5c4c
5 changed files with 44 additions and 19 deletions

View file

@ -16,7 +16,8 @@
[org.clojure/tools.cli "0.3.1"] [org.clojure/tools.cli "0.3.1"]
[clj-webdriver "0.6.1" :exclusions [org.clojure/core.cache]] [clj-webdriver "0.6.1" :exclusions [org.clojure/core.cache]]
[om "0.6.4"] [om "0.6.4"]
[sablono "0.2.17"]] [sablono "0.2.17"]
[cljs-uuid "0.0.4"]]
:profiles {:uberjar {:aot :all}} :profiles {:uberjar {:aot :all}}
:min-lein-version "2.1.2" :min-lein-version "2.1.2"
:plugins [[lein-cljsbuild "1.0.3"] :plugins [[lein-cljsbuild "1.0.3"]

View file

@ -20,16 +20,16 @@
(let [grub (new-grub (:id event) (:grub event) (:completed event))] (let [grub (new-grub (:id event) (:grub event) (:completed event))]
(assoc-in state [:grubs (:id grub)] grub))) (assoc-in state [:grubs (:id grub)] grub)))
(defn assoc-new-grub [current new] (defn map-by-key [key coll]
(assoc current (:id new) (->> coll
(new-grub (:id new) (:grub new) (:completed new)))) (map (fn [a] [(get a key) a]))
(into {})))
(defn make-add-grubs-map [grub-events]
(reduce assoc-new-grub {} grub-events))
(defmethod handle-event :add-grub-list [event state] (defmethod handle-event :add-grub-list [event state]
(let [add-grub-events (:grubs event) (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)))) (assoc state :grubs (merge (:grubs state) add-grubs))))
(defmethod handle-event :update-grub [event state] (defmethod handle-event :update-grub [event state]

View file

@ -29,7 +29,8 @@
grub-update (chan) grub-update (chan)
grub-clear-all (chan) grub-clear-all (chan)
recipe-add (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 (chan)
<events (a/pub >events :type)] <events (a/pub >events :type)]
(om/root app-view (om/root app-view
@ -39,6 +40,7 @@
:grub-update grub-update :grub-update grub-update
:grub-clear-all grub-clear-all :grub-clear-all grub-clear-all
:recipe-add recipe-add :recipe-add recipe-add
:recipe-add-grubs recipe-add-grubs
:>events >events :>events >events
:<events <events}}) :<events <events}})
out)) out))

View file

@ -2,16 +2,23 @@
(:require [om.core :as om :include-macros true] (:require [om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]] [sablono.core :as html :refer-macros [html]]
[cljs.core.async :as a :refer [<! put! chan]] [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]] (:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]])) [cljs.core.async.macros :refer [go go-loop]]))
(defn add-event [grub] (defn new-grub [grub]
{:event :add-grub {:id (str "grub-" (uuid/make-random))
:id (str "grub-" (.now js/Date))
:grub grub :grub grub
:completed false}) :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] (defn edit-event [id grub]
{:event :update-grub {:event :update-grub
:id id :id id
@ -119,7 +126,7 @@
[:span.input-group-btn [:span.input-group-btn
[:input.form-control#add-grub-input [:input.form-control#add-grub-input
{:type "text" {:type "text"
:placeholder "2 grubs" :placeholder "What do you need?"
:value new-grub :value new-grub
:on-key-up #(when (dom/enter-pressed? %) :on-key-up #(when (dom/enter-pressed? %)
(add-grub add new-grub owner)) (add-grub add new-grub owner))

View file

@ -2,21 +2,35 @@
(:require [om.core :as om :include-macros true] (:require [om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]] [sablono.core :as html :refer-macros [html]]
[cljs.core.async :as a :refer [<! put! chan]] [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]] (:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]])) [cljs.core.async.macros :refer [go go-loop]]))
(defn add-event [name grubs] (defn add-event [name grubs]
{:event :add-recipe {:event :add-recipe
:id (str "recipe-" (.now js/Date)) :id (str "recipe-" (uuid/make-random))
:name name :name name
:grubs grubs}) :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] (defn recipe-view [recipe owner]
(reify (reify
om/IRender om/IRender
(render [this] (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 (html
[:div.panel.panel-default.recipe-panel [:div.panel.panel-default.recipe-panel
{:id id {:id id
@ -28,7 +42,8 @@
:placeholder "Grub pie" :placeholder "Grub pie"
:value name}] :value name}]
[:button.btn.btn-primary.btn-sm.recipe-add-grubs-btn [:button.btn.btn-primary.btn-sm.recipe-add-grubs-btn
{:type "button"} {:type "button"
:on-click #(add-grubs add-grubs-ch grubs)}
"Add Grubs"]] "Add Grubs"]]
[:div.panel-body.recipe-grubs.hidden [:div.panel-body.recipe-grubs.hidden
[:textarea.form-control.recipe-grubs-input [:textarea.form-control.recipe-grubs-input
@ -43,7 +58,7 @@
(when (and (not (empty? name)) (when (and (not (empty? name))
(not (empty? grubs))) (not (empty? grubs)))
(om/set-state! owner :new-recipe-name "") (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)))) (put! ch (add-event name grubs))))
(defn recipes-view [recipes owner] (defn recipes-view [recipes owner]