mirror of
https://github.com/mruwnik/chicken-master.git
synced 2025-06-08 21:34:43 +02:00
fix display of orders
This commit is contained in:
parent
01da5c90f4
commit
ff3559269b
@ -1,5 +1,6 @@
|
|||||||
(ns chicken-master.calendar
|
(ns chicken-master.calendar
|
||||||
(:require
|
(:require
|
||||||
|
[clojure.string :as str]
|
||||||
[re-frame.core :as re-frame]
|
[re-frame.core :as re-frame]
|
||||||
[chicken-master.config :refer [settings]]
|
[chicken-master.config :refer [settings]]
|
||||||
[chicken-master.subs :as subs]
|
[chicken-master.subs :as subs]
|
||||||
@ -8,6 +9,18 @@
|
|||||||
[chicken-master.events :as event]
|
[chicken-master.events :as event]
|
||||||
[chicken-master.time :as time]))
|
[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 []
|
(defn edit-order []
|
||||||
(html/modal
|
(html/modal
|
||||||
@ -15,9 +28,8 @@
|
|||||||
(html/input :who "kto"
|
(html/input :who "kto"
|
||||||
{:required true
|
{:required true
|
||||||
:default @(re-frame/subscribe [::subs/order-edit-who])})
|
:default @(re-frame/subscribe [::subs/order-edit-who])})
|
||||||
(html/input :when "kiedy"
|
(html/input :notes "notka"
|
||||||
{:type :time :step 60
|
{:default @(re-frame/subscribe [::subs/order-edit-notes])})
|
||||||
:default @(re-frame/subscribe [::subs/order-edit-when])})
|
|
||||||
(let [available-prods @(re-frame/subscribe [::subs/available-products])
|
(let [available-prods @(re-frame/subscribe [::subs/available-products])
|
||||||
selected-prods @(re-frame/subscribe [::subs/order-edit-products])]
|
selected-prods @(re-frame/subscribe [::subs/order-edit-products])]
|
||||||
[:div {}
|
[:div {}
|
||||||
@ -26,10 +38,12 @@
|
|||||||
(prod/product-item product amount available-prods i))])
|
(prod/product-item product amount available-prods i))])
|
||||||
[:button {:type :button :on-click #(re-frame/dispatch [::event/add-product])} "+"]]
|
[:button {:type :button :on-click #(re-frame/dispatch [::event/add-product])} "+"]]
|
||||||
;; On success
|
;; 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]}]
|
(defn format-order [{:keys [id who day hour notes products state]}]
|
||||||
[:li {:class [:order state] :key (gensym)}
|
[:div {:class [:order state] :key (gensym)}
|
||||||
[:div {:class :actions}
|
[:div {:class :actions}
|
||||||
(condp = state
|
(condp = state
|
||||||
:waiting [:button {:on-click #(re-frame/dispatch [::event/fulfill-order id])} "✓"]
|
:waiting [:button {:on-click #(re-frame/dispatch [::event/fulfill-order id])} "✓"]
|
||||||
@ -41,9 +55,11 @@
|
|||||||
[:div {:class :who} who]
|
[:div {:class :who} who]
|
||||||
(if (settings :show-order-time)
|
(if (settings :show-order-time)
|
||||||
[:div {:class :when} hour])
|
[:div {:class :when} hour])
|
||||||
|
(if (and (settings :show-order-notes) notes)
|
||||||
|
[:div {:class :notes} notes])
|
||||||
(->> products
|
(->> products
|
||||||
(map prod/format-product)
|
(map prod/format-product)
|
||||||
(into [:ul {:class :products}]))])
|
(into [:div {:class :products}]))])
|
||||||
|
|
||||||
(defn day [{:keys [date customers]}]
|
(defn day [{:keys [date customers]}]
|
||||||
[:div {:class [:day (when (time/today? date) :today)]}
|
[:div {:class [:day (when (time/today? date) :today)]}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
:show-day-name-with-date true ; add the day name to each date
|
: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-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
|
:editable-number-inputs false ; only allow number modifications in the edit modal
|
||||||
:hide-fulfilled-orders false
|
:hide-fulfilled-orders false
|
||||||
})
|
})
|
||||||
|
@ -51,12 +51,14 @@
|
|||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::save-order
|
::save-order
|
||||||
(fn [{{order :order-edit} :db} _]
|
(fn [{{order :order-edit} :db} [_ form]]
|
||||||
(println "saving")
|
(println "saving" form)
|
||||||
{:fx [[:dispatch [::hide-modal]]]
|
{:fx [[:dispatch [::hide-modal]]]
|
||||||
:http {:method :post
|
:http {:method :post
|
||||||
:url "save-order"
|
: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-success [::process-fetched-days]
|
||||||
:on-fail [::failed-blah]}}))
|
:on-fail [::failed-blah]}}))
|
||||||
|
|
||||||
|
@ -2,6 +2,24 @@
|
|||||||
(:require [re-frame.core :as re-frame]
|
(:require [re-frame.core :as re-frame]
|
||||||
[chicken-master.events :as event]))
|
[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
|
(defn input
|
||||||
([id label] (input id label {}))
|
([id label] (input id label {}))
|
||||||
@ -19,9 +37,16 @@
|
|||||||
[:form {:action "#"
|
[:form {:action "#"
|
||||||
:on-submit (fn [e]
|
:on-submit (fn [e]
|
||||||
(.preventDefault e)
|
(.preventDefault e)
|
||||||
(when (on-submit)
|
(when (-> e .-target form-values on-submit)
|
||||||
(re-frame/dispatch [::event/hide-modal])))}
|
(re-frame/dispatch [::event/hide-modal])))}
|
||||||
content
|
content
|
||||||
[:div {:class :form-buttons}
|
[:div {:class :form-buttons}
|
||||||
[:button "add"]
|
[:button "add"]
|
||||||
[:button {:type :button :on-click #(re-frame/dispatch [::event/hide-modal])} "cancel"]]]])
|
[:button {:type :button :on-click #(re-frame/dispatch [::event/hide-modal])} "cancel"]]]])
|
||||||
|
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(->> (.getElementsByTagName js/document "form")
|
||||||
|
first
|
||||||
|
.-elements
|
||||||
|
(map extract-input)))
|
||||||
|
@ -1,16 +1,6 @@
|
|||||||
(ns chicken-master.orders
|
(ns chicken-master.orders
|
||||||
(:require [chicken-master.time :as time]))
|
(: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
|
;;;;;;;; Backend mocks
|
||||||
|
|
||||||
(def id-counter (atom -1))
|
(def id-counter (atom -1))
|
||||||
@ -20,6 +10,11 @@
|
|||||||
(map (fn [date]
|
(map (fn [date]
|
||||||
[(time/iso-date date) (repeatedly (rand-int 6) #(swap! id-counter inc))]))
|
[(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 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"]))
|
(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
|
(->> @days
|
||||||
(map (fn [[day ids]]
|
(map (fn [[day ids]]
|
||||||
(map (fn [i]
|
(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)
|
:who (rand-nth @customer-names)
|
||||||
:products (->> @products
|
:products (->> @products
|
||||||
(random-sample 0.4)
|
(random-sample 0.4)
|
||||||
@ -45,6 +42,9 @@
|
|||||||
(time/days-range
|
(time/days-range
|
||||||
(int (/ (- (time/parse-date to) (time/parse-date from)) (* 24 3600000)))
|
(int (/ (- (time/parse-date to) (time/parse-date from)) (* 24 3600000)))
|
||||||
(time/parse-date from)))
|
(time/parse-date from)))
|
||||||
|
|
||||||
|
;;; Actual stuff
|
||||||
|
|
||||||
(defn fetch-days [{:keys [from to]}]
|
(defn fetch-days [{:keys [from to]}]
|
||||||
(->> (days-between from to)
|
(->> (days-between from to)
|
||||||
(map time/iso-date)
|
(map time/iso-date)
|
||||||
|
@ -6,22 +6,23 @@
|
|||||||
[chicken-master.events :as event]))
|
[chicken-master.events :as event]))
|
||||||
|
|
||||||
(defn product-item [what amount available product-no]
|
(defn product-item [what amount available product-no]
|
||||||
|
(let [id (gensym)]
|
||||||
[:div {:key (gensym)}
|
[:div {:key (gensym)}
|
||||||
[:div {:class :input-item}
|
[:div {:class :input-item}
|
||||||
[:label {:for :product} "co"]
|
[: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])}
|
:on-change #(re-frame/dispatch [::event/selected-product (-> % .-target .-value) product-no])}
|
||||||
[:option {:value nil} "-"]
|
[:option {:value nil} "-"]
|
||||||
(for [product available]
|
(for [product available]
|
||||||
[:option {:key (gensym) :value product} (name product)])]]
|
[:option {:key (gensym) :value product} (name product)])]]
|
||||||
(html/input :amount "ile"
|
(html/input (str "amount-" id) "ile"
|
||||||
{:type :number :default amount :min 0
|
{:type :number :default amount :min 0
|
||||||
:on-blur #(re-frame/dispatch [::event/changed-amount (-> % .-target .-value) product-no])
|
:on-blur #(re-frame/dispatch [::event/changed-amount (-> % .-target .-value) product-no])
|
||||||
;; :on-change #(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]]
|
(defn format-product [[product amount]]
|
||||||
[:li {:key (gensym) :class :product}
|
[:div {:key (gensym) :class :product}
|
||||||
(if (settings :editable-number-inputs)
|
(if (settings :editable-number-inputs)
|
||||||
[:input {:class :product-amount :type :number :min 0 :defaultValue amount}]
|
[:input {:class :product-amount :type :number :min 0 :defaultValue amount}]
|
||||||
[:span {:class :product-amount} amount])
|
[:span {:class :product-amount} amount])
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
(re-frame/reg-sub ::show-edit-modal (fn [db] (-> db :order-edit :show)))
|
(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-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 ::order-edit-products (fn [db] (-> db :order-edit :products)))
|
||||||
|
|
||||||
(re-frame/reg-sub ::current-days (fn [db] (:current-days db)))
|
(re-frame/reg-sub ::current-days (fn [db] (:current-days db)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user