From ff3559269ba9db877884b487bc77ca3aa54d794e Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Sun, 24 Jan 2021 14:47:55 +0100 Subject: [PATCH] fix display of orders --- src/cljs/chicken_master/calendar.cljs | 30 ++++++++++++++++++++------- src/cljs/chicken_master/config.cljs | 1 + src/cljs/chicken_master/events.cljs | 8 ++++--- src/cljs/chicken_master/html.cljs | 27 +++++++++++++++++++++++- src/cljs/chicken_master/orders.cljs | 24 ++++++++++----------- src/cljs/chicken_master/products.cljs | 29 +++++++++++++------------- src/cljs/chicken_master/subs.cljs | 2 +- 7 files changed, 83 insertions(+), 38 deletions(-) diff --git a/src/cljs/chicken_master/calendar.cljs b/src/cljs/chicken_master/calendar.cljs index ecc96a4..6c6052f 100644 --- a/src/cljs/chicken_master/calendar.cljs +++ b/src/cljs/chicken_master/calendar.cljs @@ -1,5 +1,6 @@ (ns chicken-master.calendar (:require + [clojure.string :as str] [re-frame.core :as re-frame] [chicken-master.config :refer [settings]] [chicken-master.subs :as subs] @@ -8,6 +9,18 @@ [chicken-master.events :as event] [chicken-master.time :as time])) +(defn format-raw-order [{:strs [who notes] :as raw-values}] + {:who who + :notes notes + :products (->> raw-values + (remove (comp #{"who" "notes"} first)) + (map (fn [[k v]] [(str/split k "-") v])) + (group-by (comp last first)) + (map #(sort-by first (second %))) + (map (fn [[[_ amount] [_ product]]] [(keyword product) (js/parseInt amount)])) + (group-by first) + (map (fn [[product items]] [product (->> items (map last) (reduce +))])) + (into {}))}) (defn edit-order [] (html/modal @@ -15,9 +28,8 @@ (html/input :who "kto" {:required true :default @(re-frame/subscribe [::subs/order-edit-who])}) - (html/input :when "kiedy" - {:type :time :step 60 - :default @(re-frame/subscribe [::subs/order-edit-when])}) + (html/input :notes "notka" + {:default @(re-frame/subscribe [::subs/order-edit-notes])}) (let [available-prods @(re-frame/subscribe [::subs/available-products]) selected-prods @(re-frame/subscribe [::subs/order-edit-products])] [:div {} @@ -26,10 +38,12 @@ (prod/product-item product amount available-prods i))]) [:button {:type :button :on-click #(re-frame/dispatch [::event/add-product])} "+"]] ;; On success - (fn [] (re-frame/dispatch [::event/save-order])))) + (fn [form] + (re-frame/dispatch [::event/save-order (format-raw-order form)]) + ))) -(defn format-order [{:keys [id who day hour products state]}] - [:li {:class [:order state] :key (gensym)} +(defn format-order [{:keys [id who day hour notes products state]}] + [:div {:class [:order state] :key (gensym)} [:div {:class :actions} (condp = state :waiting [:button {:on-click #(re-frame/dispatch [::event/fulfill-order id])} "✓"] @@ -41,9 +55,11 @@ [:div {:class :who} who] (if (settings :show-order-time) [:div {:class :when} hour]) + (if (and (settings :show-order-notes) notes) + [:div {:class :notes} notes]) (->> products (map prod/format-product) - (into [:ul {:class :products}]))]) + (into [:div {:class :products}]))]) (defn day [{:keys [date customers]}] [:div {:class [:day (when (time/today? date) :today)]} diff --git a/src/cljs/chicken_master/config.cljs b/src/cljs/chicken_master/config.cljs index 6f63915..da58581 100644 --- a/src/cljs/chicken_master/config.cljs +++ b/src/cljs/chicken_master/config.cljs @@ -10,6 +10,7 @@ :show-day-name-with-date true ; add the day name to each date :show-order-time false ; display the time of each order + :show-order-notes true ; display notes :editable-number-inputs false ; only allow number modifications in the edit modal :hide-fulfilled-orders false }) diff --git a/src/cljs/chicken_master/events.cljs b/src/cljs/chicken_master/events.cljs index bdb5459..d2e36b3 100644 --- a/src/cljs/chicken_master/events.cljs +++ b/src/cljs/chicken_master/events.cljs @@ -51,12 +51,14 @@ (re-frame/reg-event-fx ::save-order - (fn [{{order :order-edit} :db} _] - (println "saving") + (fn [{{order :order-edit} :db} [_ form]] + (println "saving" form) {:fx [[:dispatch [::hide-modal]]] :http {:method :post :url "save-order" - :params (orders/clean-order order) + :params (merge + (select-keys order [:id :day :hour :state]) + (select-keys form [:who :notes :products])) :on-success [::process-fetched-days] :on-fail [::failed-blah]}})) diff --git a/src/cljs/chicken_master/html.cljs b/src/cljs/chicken_master/html.cljs index 7c8e92c..cf71041 100644 --- a/src/cljs/chicken_master/html.cljs +++ b/src/cljs/chicken_master/html.cljs @@ -2,6 +2,24 @@ (:require [re-frame.core :as re-frame] [chicken-master.events :as event])) +(defn extract-input [elem] + (condp = (.-tagName elem) + "CHECKBOX" [elem (.-checked elem)] + "INPUT" [(.-name elem) (if (-> (.-type elem) clojure.string/lower-case #{"checkbox"}) + (.-checked elem) + (.-value elem))] + "SELECT" [(.-name elem) (some->> elem + (filter #(.-selected %)) + first + .-value)] + nil)) + +(defn form-values [form] + (some->> form + .-elements + (map extract-input) + (remove nil?) + (into {}))) (defn input ([id label] (input id label {})) @@ -19,9 +37,16 @@ [:form {:action "#" :on-submit (fn [e] (.preventDefault e) - (when (on-submit) + (when (-> e .-target form-values on-submit) (re-frame/dispatch [::event/hide-modal])))} content [:div {:class :form-buttons} [:button "add"] [:button {:type :button :on-click #(re-frame/dispatch [::event/hide-modal])} "cancel"]]]]) + + +(comment + (->> (.getElementsByTagName js/document "form") + first + .-elements + (map extract-input))) diff --git a/src/cljs/chicken_master/orders.cljs b/src/cljs/chicken_master/orders.cljs index a78b159..4131e56 100644 --- a/src/cljs/chicken_master/orders.cljs +++ b/src/cljs/chicken_master/orders.cljs @@ -1,16 +1,6 @@ (ns chicken-master.orders (:require [chicken-master.time :as time])) -(defn clean-order [order] - (-> order - (update :products #(->> % - (group-by :prod) - (reduce-kv (fn [m k v] - (assoc m k (->> v - (map :amount) - (reduce +)))) {}))) - (select-keys [:id :who :day :hour :products :state]))) - ;;;;;;;; Backend mocks (def id-counter (atom -1)) @@ -19,7 +9,12 @@ (time/days-range 90) (map (fn [date] [(time/iso-date date) (repeatedly (rand-int 6) #(swap! id-counter inc))])) - (into {})))) + (into {})))) +(def notes ["bezglutenowy" + "tylko z robakami" + "przyjdzie wieczorem" + "wisi 2.50" + "chciała ukraść kozę"]) (def products (atom [:eggs :milk :cabbage :carrots])) (def customer-names (atom ["mr.blobby (649 234 234)" "da police (0118 999 881 999 119 725 123123 12 3123 123 )" "johnny"])) @@ -28,7 +23,9 @@ (->> @days (map (fn [[day ids]] (map (fn [i] - {:id i :day day :hour "02:12" :state :waiting + {:id i :day day + :notes (when (> (rand) 0.7) (rand-nth notes)) + :state :waiting :who (rand-nth @customer-names) :products (->> @products (random-sample 0.4) @@ -45,6 +42,9 @@ (time/days-range (int (/ (- (time/parse-date to) (time/parse-date from)) (* 24 3600000))) (time/parse-date from))) + +;;; Actual stuff + (defn fetch-days [{:keys [from to]}] (->> (days-between from to) (map time/iso-date) diff --git a/src/cljs/chicken_master/products.cljs b/src/cljs/chicken_master/products.cljs index 816a0d4..41dc60c 100644 --- a/src/cljs/chicken_master/products.cljs +++ b/src/cljs/chicken_master/products.cljs @@ -6,22 +6,23 @@ [chicken-master.events :as event])) (defn product-item [what amount available product-no] - [:div {:key (gensym)} - [:div {:class :input-item} - [:label {:for :product} "co"] - [:select {:name :product :id :product :defaultValue what - :on-change #(re-frame/dispatch [::event/selected-product (-> % .-target .-value) product-no])} - [:option {:value nil} "-"] - (for [product available] - [:option {:key (gensym) :value product} (name product)])]] - (html/input :amount "ile" - {:type :number :default amount :min 0 - :on-blur #(re-frame/dispatch [::event/changed-amount (-> % .-target .-value) product-no]) - ;; :on-change #(re-frame/dispatch [::event/changed-amount (-> % .-target .-value) product-no]) - })]) + (let [id (gensym)] + [:div {:key (gensym)} + [:div {:class :input-item} + [:label {:for :product} "co"] + [:select {:name (str "product-" id) :id :product :defaultValue what + :on-change #(re-frame/dispatch [::event/selected-product (-> % .-target .-value) product-no])} + [:option {:value nil} "-"] + (for [product available] + [:option {:key (gensym) :value product} (name product)])]] + (html/input (str "amount-" id) "ile" + {:type :number :default amount :min 0 + :on-blur #(re-frame/dispatch [::event/changed-amount (-> % .-target .-value) product-no]) + ;; :on-change #(re-frame/dispatch [::event/changed-amount (-> % .-target .-value) product-no]) + })])) (defn format-product [[product amount]] - [:li {:key (gensym) :class :product} + [:div {:key (gensym) :class :product} (if (settings :editable-number-inputs) [:input {:class :product-amount :type :number :min 0 :defaultValue amount}] [:span {:class :product-amount} amount]) diff --git a/src/cljs/chicken_master/subs.cljs b/src/cljs/chicken_master/subs.cljs index d56212f..6b1a7db 100644 --- a/src/cljs/chicken_master/subs.cljs +++ b/src/cljs/chicken_master/subs.cljs @@ -6,7 +6,7 @@ (re-frame/reg-sub ::show-edit-modal (fn [db] (-> db :order-edit :show))) (re-frame/reg-sub ::order-edit-who (fn [db] (println (:order-edit db)) (-> db :order-edit :who))) -(re-frame/reg-sub ::order-edit-when (fn [db] (-> db :order-edit :hour))) +(re-frame/reg-sub ::order-edit-notes (fn [db] (-> db :order-edit :notes))) (re-frame/reg-sub ::order-edit-products (fn [db] (-> db :order-edit :products))) (re-frame/reg-sub ::current-days (fn [db] (:current-days db)))