add new stock

This commit is contained in:
Daniel O'Connell 2021-02-06 15:43:03 +01:00
parent d5562736f1
commit f361882a3f
5 changed files with 32 additions and 27 deletions

View File

@ -124,6 +124,7 @@
] ]
[:.customers-modal [:.customers-modal
[:details {:padding "0.5em"}]
[:details.customer-order {:margin-left "1em" :padding "0.5em"} [:details.customer-order {:margin-left "1em" :padding "0.5em"}
[:.order-date-picker {:display :inline-block :width "75%" :cursor :pointer}] [:.order-date-picker {:display :inline-block :width "75%" :cursor :pointer}]
[:.product-item-edit {:margin-left "1em"}]]] [:.product-item-edit {:margin-left "1em"}]]]

View File

@ -8,27 +8,12 @@
[chicken-master.html :as html] [chicken-master.html :as html]
[chicken-master.events :as event])) [chicken-master.events :as event]))
(defn item-adder [& {:keys [type value callback button class]
:or {type :text value "" button nil}}]
(let [state (reagent/atom value)]
(fn []
[:div {:class class :on-click #(.stopPropagation %)}
[:input {:type type :name :user-name :default value :value @state
:on-change #(let [val (-> % .-target .-value)]
(reset! state val)
(if-not button (callback val)))}]
(if button
[:button {:class :add-product
:type :button
:disabled (= @state "")
:on-click (if callback #(-> state (reset-vals! value) first callback))} button])])))
(defn order-adder [order] (defn order-adder [order]
(let [state (reagent/atom order)] (let [state (reagent/atom order)]
(fn [] (fn []
[:details {:class :customer-order :key (gensym) :open (:open @state)} [:details {:class :customer-order :key (gensym) :open (:open @state)}
[:summary {:on-click #(swap! state update :open not)} [:summary {:on-click #(swap! state update :open not)}
[item-adder [prod/item-adder
:type :date :type :date
:value (:day @state) :value (:day @state)
:class :order-date-picker :class :order-date-picker
@ -42,7 +27,7 @@
:clients :clients
[:div {:class :customers-modal} [:div {:class :customers-modal}
[:h2 "Clienci"] [:h2 "Clienci"]
[item-adder :callback #(re-frame/dispatch [::event/add-customer %]) :button "+"] [prod/item-adder :callback #(re-frame/dispatch [::event/add-customer %]) :button "+"]
(let [client-orders (->> @(re-frame/subscribe [::subs/orders]) (let [client-orders (->> @(re-frame/subscribe [::subs/orders])
vals vals
(group-by #(get-in % [:who :id])))] (group-by #(get-in % [:who :id])))]

View File

@ -209,10 +209,9 @@
(assoc-in db [:products product] i))) (assoc-in db [:products product] i)))
(re-frame/reg-event-db (re-frame/reg-event-db
::set-stock-amount ::delete-product
(fn [db [_ product i]] (fn [db [_ product]]
(prn i) (update db :products dissoc product)))
(assoc-in db [:products product] i)))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::save-stock ::save-stock

View File

@ -78,3 +78,18 @@
(if (settings :editable-number-inputs) (if (settings :editable-number-inputs)
(number-input (str "amount-" product) "" amount nil) (number-input (str "amount-" product) "" amount nil)
[:span {:class :product-amount} amount])]) [:span {:class :product-amount} amount])])
(defn item-adder [& {:keys [type value callback button class]
:or {type :text value "" button nil}}]
(let [state (reagent/atom value)]
(fn []
[:div {:class class :on-click #(.stopPropagation %)}
[:input {:type type :name :user-name :default value :value @state
:on-change #(let [val (-> % .-target .-value)]
(reset! state val)
(if-not button (callback val)))}]
(if button
[:button {:class :add-product
:type :button
:disabled (= @state "")
:on-click (if callback #(-> state (reset-vals! value) first callback))} button])])))

View File

@ -2,7 +2,7 @@
(:require (:require
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[chicken-master.config :refer [settings]] [chicken-master.config :refer [settings]]
[chicken-master.products :refer [num-or-nil number-input]] [chicken-master.products :as prod]
[chicken-master.subs :as subs] [chicken-master.subs :as subs]
[chicken-master.html :as html] [chicken-master.html :as html]
[chicken-master.events :as event])) [chicken-master.events :as event]))
@ -12,20 +12,25 @@
(html/modal (html/modal
:stock :stock
[:div {:class :stock-modal} [:div {:class :stock-modal}
[:button {:class :add-product :type :button :on-click #(re-frame/dispatch [::event/add-stock-product])} "dodaj"]
[:h2 "Magazyn"] [:h2 "Magazyn"]
(for [[product amount] @(re-frame/subscribe [::subs/available-products])] (for [[product amount] @(re-frame/subscribe [::subs/available-products])]
[:div {:key (gensym) :class :stock-product} [:div {:key (gensym) :class :stock-product}
[:span {:class :product-name} product] [:span {:class :product-name} product]
[:div {:class :stock-product-amount} [:div {:class :stock-product-amount}
[:button {:type :button :on-click #(re-frame/dispatch [::event/update-product-stock product -1])} "-"] [:button {:type :button :on-click #(re-frame/dispatch [::event/update-product-stock product -1])} "-"]
(number-input (name product) "" (or amount 0) (prod/number-input (name product) "" (or amount 0)
#(re-frame/dispatch [::event/set-stock-amount product (-> % .-target .-value num-or-nil)])) #(re-frame/dispatch [::event/set-stock-amount product (-> % .-target .-value prod/num-or-nil)]))
[:button {:type :button :on-click #(re-frame/dispatch [::event/update-product-stock product 1])} "+"] [:button {:type :button :on-click #(re-frame/dispatch [::event/update-product-stock product 1])} "+"]
]])] [:button {:type :button :on-click #(re-frame/dispatch [::event/delete-product product])} "x"]
]])
[prod/item-adder :callback #(re-frame/dispatch [::event/set-stock-amount % 0]) :button "+"]
]
;; On success ;; On success
(fn [form] (fn [form]
(->> form (->> form
(reduce-kv #(assoc %1 (keyword %2) (num-or-nil %3)) {}) (reduce-kv #(if-let [val (prod/num-or-nil %3)]
(assoc %1 (keyword %2) val)
%1)
{})
(conj [::event/save-stock]) (conj [::event/save-stock])
re-frame/dispatch)))) re-frame/dispatch))))