handling errors

This commit is contained in:
Bryce Covert
2018-04-09 14:46:10 -07:00
parent d69b1f2b81
commit 3da61b0057
3 changed files with 48 additions and 30 deletions

View File

@@ -33,18 +33,24 @@
(re-frame/reg-fx (re-frame/reg-fx
:http :http
(fn [{:keys [method uri on-success body headers token]}] (fn [{:keys [method uri on-success on-error body headers token]}]
(go (go
(let [headers (if token (let [headers (if token
(assoc headers "Authorization" (str "Token " token)) (assoc headers "Authorization" (str "Token " token))
headers)] headers)
(println headers) response (<! (http/request {:method method
(->> (http/request {:method method
:body body :body body
:headers headers :headers headers
:url uri}) :url uri}))]
(<! ) (if (>= (:status response) 400)
(when on-error
(->> response
:body
(dates->date-times)
(conj on-error)
(re-frame/dispatch)))
(->> response
:body :body
(dates->date-times) (dates->date-times)
(conj on-success) (conj on-success)
(re-frame/dispatch)))))) (re-frame/dispatch)))))))

View File

@@ -32,13 +32,15 @@
:body (pr-str edited-vendor) :body (pr-str edited-vendor)
:headers {"Content-Type" "application/edn"} :headers {"Content-Type" "application/edn"}
:uri (str "/api/vendors/" (::entity/id edited-vendor)) :uri (str "/api/vendors/" (::entity/id edited-vendor))
:on-success [::save-complete]}) :on-success [::save-complete]
:on-error [::save-error]})
(assoc fx :http {:method :post (assoc fx :http {:method :post
:token (:user db) :token (:user db)
:body (pr-str edited-vendor) :body (pr-str edited-vendor)
:headers {"Content-Type" "application/edn"} :headers {"Content-Type" "application/edn"}
:uri (str "/api/vendors") :uri (str "/api/vendors")
:on-success [::save-complete]})))))) :on-success [::save-complete]
:on-error [::save-error]}))))))
(re-frame/reg-event-db (re-frame/reg-event-db
::save-complete ::save-complete
@@ -48,6 +50,13 @@
(assoc-in [:admin :vendor] nil) (assoc-in [:admin :vendor] nil)
(assoc-in [:vendors (::entity/id vendor)] vendor)))) (assoc-in [:vendors (::entity/id vendor)] vendor))))
(re-frame/reg-event-db
::save-error
(fn [db [_ vendor]]
(-> db
(assoc-in [:admin :vendor :error] true)
(assoc-in [:admin :vendor :saving?] false))))
(re-frame/reg-event-db (re-frame/reg-event-db
::change ::change
(fn [db [_ path value]] (fn [db [_ path value]]

View File

@@ -90,6 +90,9 @@
(if (::entity/id editing-vendor) (if (::entity/id editing-vendor)
(str "Edit " (or (::entity/name editing-vendor) "<vendor>")) (str "Edit " (or (::entity/name editing-vendor) "<vendor>"))
(str "Add " (or (::entity/name editing-vendor) "<new vendor>")))] (str "Add " (or (::entity/name editing-vendor) "<new vendor>")))]
(when (:error editing-vendor)
[:span.icon.has-text-danger
[:i.fa.fa-exclamation-triangle]])
[:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]] [:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]]
[:section.modal-card-body [:section.modal-card-body
[horizontal-field [horizontal-field
@@ -215,8 +218,8 @@
:value "Weekly" :value "Weekly"
:field ::entity/invoice-reminder-schedule :field ::entity/invoice-reminder-schedule
:subscription editing-vendor}]] :subscription editing-vendor}]]
" Send weekly"] " Send weekly"]
[:label.radio [:label.radio
[bind-field [bind-field
[:input {:type "radio" [:input {:type "radio"