greatly simplifying how states are handled.

This commit is contained in:
Bryce Covert
2020-08-03 12:11:56 -07:00
parent bb5196b9f8
commit a22eb01008
7 changed files with 71 additions and 28 deletions

View File

@@ -24,7 +24,8 @@
:else :else
nil)] nil)]
(log/info "csv type was determined to be" csv-type))) (log/info "csv type was determined to be" csv-type)
csv-type))
(defmulti parse-csv (defmulti parse-csv
determine determine

View File

@@ -207,6 +207,7 @@
(defn import-uploaded-invoice [client forced-location forced-vendor imports] (defn import-uploaded-invoice [client forced-location forced-vendor imports]
(lc/with-context {:area "upload-invoice"} (lc/with-context {:area "upload-invoice"}
(log/info "Number of invoices to import is" (count imports) "sample: " (first imports))
(let [clients (d-clients/get-all) (let [clients (d-clients/get-all)
transactions (reduce (fn [result {:keys [invoice-number customer-identifier account-number total date vendor-code text full-text] :as info}] transactions (reduce (fn [result {:keys [invoice-number customer-identifier account-number total date vendor-code text full-text] :as info}]
@@ -481,6 +482,7 @@
:body (pr-str {}) :body (pr-str {})
:headers {"Content-Type" "application/edn"}} :headers {"Content-Type" "application/edn"}}
(catch Exception e (catch Exception e
(log/warn e)
{:status 500 {:status 500
:body (pr-str {:message (.getMessage e) :body (pr-str {:message (.getMessage e)
:error (.toString e) :error (.toString e)

View File

@@ -162,6 +162,9 @@
(go (go
(when (:multi owns-state) (when (:multi owns-state)
(re-frame/dispatch [::status/loading-multi (:multi owns-state) (:which owns-state)])) (re-frame/dispatch [::status/loading-multi (:multi owns-state) (:which owns-state)]))
(when (:single owns-state)
(re-frame/dispatch [::status/loading (:single owns-state)]))
(let [headers (if token (let [headers (if token
{"Authorization" (str "Token " token)} {"Authorization" (str "Token " token)}
{}) {})
@@ -195,6 +198,8 @@
)] )]
(when (:multi owns-state) (when (:multi owns-state)
(re-frame/dispatch [::status/error-multi (:multi owns-state) (:which owns-state) error])) (re-frame/dispatch [::status/error-multi (:multi owns-state) (:which owns-state) error]))
(when (:single owns-state)
(re-frame/dispatch [::status/error (:single owns-state) error]))
(when on-error (when on-error
(->> error (->> error
(conj on-error) (conj on-error)
@@ -203,6 +208,8 @@
(do (do
(when (:multi owns-state) (when (:multi owns-state)
(re-frame/dispatch [::status/completed-multi (:multi owns-state) (:which owns-state)])) (re-frame/dispatch [::status/completed-multi (:multi owns-state) (:which owns-state)]))
(when (:single owns-state)
(re-frame/dispatch [::status/completed (:single owns-state)]))
(->> response (->> response
:body :body
:data :data

View File

@@ -43,6 +43,11 @@
(fn [db [_ multi]] (fn [db [_ multi]]
(get-in db [::status multi]))) (get-in db [::status multi])))
(re-frame/reg-sub
::single
(fn [db [_ single]]
(get-in db [::status single])))
(re-frame/reg-event-db (re-frame/reg-event-db
::loading-multi ::loading-multi
[(re-frame/path [::status]) ] [(re-frame/path [::status]) ]
@@ -66,3 +71,24 @@
(defn reset-multi [db multi] (defn reset-multi [db multi]
(assoc-in db [::status multi] {})) (assoc-in db [::status multi] {}))
(re-frame/reg-event-db
::loading
[(re-frame/path [::status]) ]
(fn [db [_ single]]
(assoc db single {:state :loading
:error nil})))
(re-frame/reg-event-db
::completed
[(re-frame/path [::status]) ]
(fn [db [_ single which]]
(assoc db single {:state nil
:error nil})))
(re-frame/reg-event-db
::error
[(re-frame/path [::status]) ]
(fn [db [_ single error]]
(assoc db single {:state :error
:error error})))

View File

@@ -146,7 +146,7 @@
:enter-class "appear" :enter-class "appear"
:exit-class "disappear"} :exit-class "disappear"}
(into [:div.test ] (into [:span.test ]
(r/children (r/current-component)))]]) (r/children (r/current-component)))]])
(defn body [] (defn body []
@@ -155,26 +155,32 @@
(fn [consume] (fn [consume]
(let [{:strs [column-count status]} (js->clj consume)] (let [{:strs [column-count status]} (js->clj consume)]
(r/as-element (r/as-element
(if (:loading status) (cond (= :loading (:state status))
[:tbody [:tbody
(for [i (range 40)] (for [i (range 40)]
^{:key i} ^{:key i}
[:tr [:tr
(for [x (range column-count)] (for [x (range column-count)]
^{:key x} ^{:key x}
[:td #_{:col-span column-count} [:td #_{:col-span column-count}
[appearing {:visible? true [appearing {:visible? true
:timeout 1000 :timeout 1000
:enter-class "appear" :enter-class "appear"
:exit-class "disappear"} :exit-class "disappear"}
[:div.test [:div.test
[:div.ph-item [:div.ph-item
[:div.ph-row [:div.ph-row
[:div.ph-col-12.big]]]]]]) [:div.ph-col-12.big]]]]]])
])] ])]
(into [:tbody] (= :error (:state status))
[:tbody [:tr [:td.has-text-centered {:col-span column-count}
"An unexpected error has occured. "
(-> status :error first :message)
" Please try refreshing the page."]]]
:else
(into [:tbody]
children)))))])) children)))))]))
(defn sortable-header-cell [{:keys [style class sort-key sort-name asc]}] (defn sortable-header-cell [{:keys [style class sort-key sort-name asc]}]
(let [children (r/children (r/current-component))] (let [children (r/children (r/current-component))]

View File

@@ -12,7 +12,8 @@
[vimsical.re-frame.cofx.inject :as inject] [vimsical.re-frame.cofx.inject :as inject]
[auto-ap.events :as events] [auto-ap.events :as events]
[auto-ap.utils :refer [replace-by]] [auto-ap.utils :refer [replace-by]]
[re-frame.core :as re-frame])) [re-frame.core :as re-frame]
[auto-ap.status :as status]))
;; SUBS ;; SUBS
@@ -54,8 +55,7 @@
(-> data (-> data
:transaction-rule-page :transaction-rule-page
(update :transaction-rules (fn [rules] (update :transaction-rules (fn [rules]
(mapv ungraphql-transaction-rule rules))))) (mapv ungraphql-transaction-rule rules))))))))
(assoc-in [:status :loading] false))))
(re-frame/reg-sub (re-frame/reg-sub
::last-params ::last-params
@@ -82,9 +82,10 @@
[with-user (re-frame/inject-cofx ::inject/sub [::params])] [with-user (re-frame/inject-cofx ::inject/sub [::params])]
(fn [{:keys [db user] ::keys [params] :as cofx} _] (fn [{:keys [db user] ::keys [params] :as cofx} _]
{:db (-> db {:db (-> db
(assoc-in [:status :loading] true)
(assoc-in [::last-params] params)) (assoc-in [::last-params] params))
:graphql {:token user :graphql {:token user
:owns-state {:single ::page}
:query-obj {:venia/queries [[:transaction_rule_page :query-obj {:venia/queries [[:transaction_rule_page
(or params {}) (or params {})
[[:transaction-rules default-read] [[:transaction-rules default-read]
@@ -117,7 +118,7 @@
[table/table {:id :transactions [table/table {:id :transactions
:params (re-frame/subscribe [::params]) :params (re-frame/subscribe [::params])
:rule-page (re-frame/subscribe [::page]) :rule-page (re-frame/subscribe [::page])
:status (re-frame/subscribe [::subs/status]) :status @(re-frame/subscribe [::status/single ::page])
:on-params-change (fn [params] :on-params-change (fn [params]
(println "CHANGING PARAMS TO" params) (println "CHANGING PARAMS TO" params)
(re-frame/dispatch [::params-change params]))}] (re-frame/dispatch [::params-change params]))}]

View File

@@ -72,7 +72,7 @@
states @(re-frame/subscribe [::status/multi ::run])] states @(re-frame/subscribe [::status/multi ::run])]
[grid/grid {:on-params-change opc [grid/grid {:on-params-change opc
:params @(re-frame/subscribe [::table-params]) :params @(re-frame/subscribe [::table-params])
:status @status :status status
:column-count 6} :column-count 6}
[grid/controls {:start start :end end :count count :total total}] [grid/controls {:start start :end end :count count :total total}]
[grid/table {:fullwidth true } [grid/table {:fullwidth true }
@@ -124,5 +124,5 @@
(defn table [params] (defn table [params]
(r/create-class {:component-will-unmount (dispatch-event [::unmounted]) (r/create-class {:component-will-unmount (dispatch-event [::unmounted])
:reagent-render (fn [] :reagent-render (fn [params]
[table* params])})) [table* params])}))