From 0db764b1454c1985466e311cf25c2cf7a2650761 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Wed, 6 Aug 2014 00:09:25 +0300 Subject: [PATCH] Use state machine for new recipes --- src/cljs/grub/view/recipe_list.cljs | 108 +++++++++++++--------------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/src/cljs/grub/view/recipe_list.cljs b/src/cljs/grub/view/recipe_list.cljs index 78f9ad9..bdb5fe8 100644 --- a/src/cljs/grub/view/recipe_list.cljs +++ b/src/cljs/grub/view/recipe_list.cljs @@ -14,77 +14,69 @@ (not (empty? grubs))) (om/set-state! owner :new-recipe-name "") (om/set-state! owner :new-recipe-grubs "") - (om/set-state! owner :editing false) (put! ch (recipe/add-event name grubs)))) +(def transitions + {:waiting {:click :editing} + :editing {:body-mousedown :waiting + :save :waiting}}) + +(defn transition-state [owner event] + (let [current (om/get-state owner :edit-state) + next (or (get-in transitions [current event]) current)] + (condp = [current event next] + [:editing :save :waiting] (let [add-ch (om/get-shared owner :recipe-add) + name (om/get-state owner :new-recipe-name) + grubs (om/get-state owner :new-recipe-grubs)] + (add-recipe add-ch name grubs owner)) + nil) + (om/set-state! owner :edit-state next))) + (defn new-recipe-view [_ owner] (reify om/IInitState (init-state [_] (let [publisher (chan)] - {:editing false - :>local-events publisher - :local-events new-recipe-name new-recipe-grubs]}] - (let [add (om/get-shared owner :recipe-add)] - (html - [:div.panel.panel-default.recipe-panel - {:on-click #(put! >local-events :click)} - [:div.panel-heading.recipe-header - [:input.form-control.recipe-header-input - {:id "new-recipe-name" - :type "text" - :placeholder "New recipe" - :value new-recipe-name - :on-change #(om/set-state! owner :new-recipe-name (dom/event-val %))}]] - [:div.panel-body.recipe-grubs - {:class (when (not editing) "hidden")} - [:textarea.form-control.recipe-grubs-input - {:id "new-recipe-grubs" - :rows 3 - :placeholder "Recipe ingredients" - :value new-recipe-grubs - :on-change #(om/set-state! owner :new-recipe-grubs (dom/event-val %))}] - [:button.btn.btn-primary.pull-right.recipe-btn.recipe-done-btn - {:type "button" - :on-click #(put! >local-events :done)} - "Done"]]]))) - + (render-state [this {:keys [edit-state new-recipe-name new-recipe-grubs]}] + (html + [:div.panel.panel-default.recipe-panel + {:on-click #(when (not (dom/click-on-elem? % (om/get-node owner :save-btn))) + (transition-state owner :click))} + [:div.panel-heading.recipe-header + [:input.form-control.recipe-header-input + {:id "new-recipe-name" + :type "text" + :placeholder "New recipe" + :value new-recipe-name + :on-change #(om/set-state! owner :new-recipe-name (dom/event-val %))}]] + [:div.panel-body.recipe-grubs + {:class (when (= edit-state :waiting) "hidden")} + [:textarea.form-control.recipe-grubs-input + {:id "new-recipe-grubs" + :rows 3 + :placeholder "Recipe ingredients" + :value new-recipe-grubs + :on-change #(om/set-state! owner :new-recipe-grubs (dom/event-val %))}] + [:button.btn.btn-primary.pull-right.recipe-btn.recipe-done-btn + {:type "button" + :ref :save-btn + :on-click #(transition-state owner :save)} + "Save"]]])) + om/IWillMount (will-mount [_] - (let [