fix display of orders

This commit is contained in:
Daniel O'Connell 2021-01-24 14:47:55 +01:00
parent 01da5c90f4
commit ff3559269b
7 changed files with 83 additions and 38 deletions

View File

@ -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)]}

View File

@ -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
})

View File

@ -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]}}))

View File

@ -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)))

View File

@ -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))
@ -20,6 +10,11 @@
(map (fn [date]
[(time/iso-date date) (repeatedly (rand-int 6) #(swap! id-counter inc))]))
(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)

View File

@ -6,22 +6,23 @@
[chicken-master.events :as event]))
(defn product-item [what amount available product-no]
(let [id (gensym)]
[:div {:key (gensym)}
[:div {:class :input-item}
[:label {:for :product} "co"]
[:select {:name :product :id :product :defaultValue what
[: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 :amount "ile"
(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])

View File

@ -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)))