basic error handling msgs

This commit is contained in:
Daniel O'Connell 2021-02-07 17:32:26 +01:00
parent 5110316617
commit 3a340fd801
6 changed files with 43 additions and 54 deletions

View File

@ -12,7 +12,7 @@
(defn add-customer [request] {:body (some-> request :body :name mocks/add-customer)})
(defn delete-customer [id] {:body (mocks/delete-customer (edn/read-string id))})
(defn get-products [_] (prn _){:body (mocks/get-all-products)})
(defn get-products [_] {:body (mocks/get-all-products)})
(defn save-products [request] {:body (some-> request :body mocks/save-stocks)})
(defn get-orders [params] {:body {:orders (mocks/get-orders params)}})
@ -22,7 +22,7 @@
{:body (mocks/replace-order id body)}))
(defn delete-order [id] {:body (mocks/delete-order (edn/read-string id))})
(defn set-order-state [id status] (prn "asd"){:body (mocks/order-state (edn/read-string id) status)})
(defn set-order-state [id status] {:body (mocks/order-state (edn/read-string id) status)})
(defn get-stock [params]
{:body

View File

@ -8,10 +8,6 @@
[chicken-master.time :as time]))
(defn format-raw-order [{:strs [who who-id notes] :as raw-values}]
(prn who who-id)
(prn (some->> @(re-frame/subscribe [::subs/available-customers])
(filter (comp #{who} :name))
first :id))
{:who {:name who
:id (if (prod/num-or-nil who-id)
(prod/num-or-nil who-id)

View File

@ -39,7 +39,6 @@
(defn- settings [key]
(get @(re-frame/subscribe [::subs/settings]) key))
(re-frame/reg-event-db
::change-setting
(fn [{settings :settings :as db} [_ key val]]
@ -49,7 +48,6 @@
(defn change-setting [key val]
(set-item! :settings (assoc (get-setting :settings) key val))
(prn (get-setting :settings))
(re-frame/dispatch [::change-setting key val]))
(defn input [id label opts]
@ -86,7 +84,7 @@
[:h3 "Ustawienia tyłu"]
[:label {:for :http-dispatch} "re-frame http dispatcher"]
[:select {:id :http-dispatch :name :http-dispatch
[:select {:id :http-dispatch :name :http-dispatch :value (settings :http-dispatch)
:on-change #(change-setting :http-dispatch (-> % .-target .-value keyword))}
[:option {:value :http} "client side mock"]
[:option {:value :http-xhrio} "re-frame-http-fx"]]

View File

@ -12,7 +12,7 @@
(defn http-request [method endpoint & {:keys [params body on-success on-failure]
:or {on-success ::process-fetched-days
on-failure ::failed-blah}}]
on-failure ::failed-request}}]
{:method method
:uri (str (settings :backend-url) endpoint)
:headers {"Content-Type" "application/edn"
@ -22,7 +22,7 @@
:params params
:response-format (edn/edn-response-format)
:on-success [on-success]
:on-fail [on-failure]})
:on-failure [on-failure]})
(defn http-get [endpoint params on-success]
(http-request :get endpoint :params params :on-success on-success))
@ -55,6 +55,15 @@
(fn [_ [_ id]]
{(settings :http-dispatch) (http-request :delete (str "orders/" id))}))
(re-frame/reg-event-db
::failed-request
(fn [db [_ response]]
(.error js/console (str response))
(js/alert "Wystąpił błąd")
(assoc db :loading false)))
(re-frame/reg-event-fx
::move-order
(fn [{{orders :orders start-date :start-date} :db} [_ id day]]
@ -171,25 +180,9 @@
(assoc-if :products products)
(assoc-if :customers customers)
(assoc-if :orders orders))
:dispatch [::process-fetched-days (group-by :day (vals orders))]
:dispatch [::scroll-weeks 0]
}))
(re-frame/reg-event-db
::update-product-stock
(fn [db [_ product i]]
(update-in db [:products product] + i)))
(re-frame/reg-event-db
::set-stock-amount
(fn [db [_ product i]]
(prn i)
(assoc-in db [:products product] i)))
(re-frame/reg-event-db
::delete-product
(fn [db [_ product]]
(update db :products dissoc product)))
(re-frame/reg-event-fx
::save-stock
(fn [_ [_ products]]

View File

@ -40,31 +40,30 @@
[:div {:class :product-item-edit :key (gensym)}
[:div {:class :input-item}
;; [:label {:for :product} "co"]
[:select {:name (str "product-" id) :id :product :defaultValue what
[:select {:name (str "product-" id) :id :product :defaultValue (some-> what name)
:on-change #(let [prod (-> % .-target .-value keyword)]
(swap! state assoc prod (+ (@state prod) (@state what)))
(if-not (= prod :-)
(swap! state assoc prod (+ (@state prod) (@state what))))
(swap! state dissoc what)
(if (not (contains? @state nil))
(swap! state assoc nil 0))
)}
[:option {:value ""} "-"]
(for [[product _] available]
[:option {:key (gensym) :value product} (name product)])]
(for [product (-> available (conj what) sort vec (conj nil))]
[:option {:key (gensym) :value product} (if product (name product) "-")])]
]
(number-input (str "amount-" id) nil (@state what)
#(do (swap! state assoc what (-> % .-target .-value num-or-nil))
(prn @state)))
#(swap! state assoc what (-> % .-target .-value num-or-nil)))
]))
(defn products-edit [selected-prods & {:keys [available-prods getter-fn]
:or {available-prods @(re-frame/subscribe [::subs/available-products])}}]
(let [state (reagent/atom (assoc selected-prods nil 0))]
(let [state (reagent/atom (or selected-prods {}))]
(fn []
(let [products (->> @state
(let [available (remove (partial get @state) (keys available-prods))
products (->> @state
keys
sort
(map (partial product-item available-prods state))
(into [:div {:class :product-items-edit}]))]
(map (partial product-item available state))
(into [:div {:class :product-items-edit}]))
products (conj products (product-item available state nil))]
(if getter-fn
(conj products
[:button {:type :button

View File

@ -1,30 +1,33 @@
(ns chicken-master.stock
(:require
[re-frame.core :as re-frame]
[chicken-master.config :refer [settings]]
[reagent.core :as reagent]
[chicken-master.products :as prod]
[chicken-master.subs :as subs]
[chicken-master.html :as html]
[chicken-master.events :as event]))
(defn stock-form [stock]
(let [state (reagent/atom stock)]
(fn []
[:div
(for [[product amount] @state]
[:div {:key (gensym) :class :stock-product}
[:span {:class :product-name} product]
[:div {:class :stock-product-amount}
[:button {:type :button :on-click #(swap! state update product dec)} "-"]
(prod/number-input (name product) "" (or amount 0)
#(swap! state assoc product (-> % .-target .-value prod/num-or-nil)))
[:button {:type :button :on-click #(swap! state update product inc)} "+"]
[:button {:type :button :on-click #(swap! state dissoc product)} "x"]]])
[prod/item-adder :callback #(swap! state assoc (keyword %) 0) :button "+"]])))
(defn show-available []
(html/modal
:stock
[:div {:class :stock-modal}
[:h2 "Magazyn"]
(for [[product amount] @(re-frame/subscribe [::subs/available-products])]
[:div {:key (gensym) :class :stock-product}
[:span {:class :product-name} product]
[:div {:class :stock-product-amount}
[:button {:type :button :on-click #(re-frame/dispatch [::event/update-product-stock product -1])} "-"]
(prod/number-input (name product) "" (or amount 0)
#(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/delete-product product])} "x"]
]])
[prod/item-adder :callback #(re-frame/dispatch [::event/set-stock-amount % 0]) :button "+"]
]
[stock-form @(re-frame/subscribe [::subs/available-products])]]
;; On success
(fn [form]
(->> form