Should improve perf somewhat

This commit is contained in:
Bryce Covert
2020-05-02 08:09:26 -07:00
parent 532e35bb50
commit 5643b98b13
6 changed files with 116 additions and 91 deletions

View File

@@ -2,6 +2,7 @@
(:require [auto-ap.events :as events]
[auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[vimsical.re-frame.cofx.inject :as inject]
[auto-ap.views.components.bank-account-filter :refer [bank-account-filter]]
[auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]]
[auto-ap.routes :as routes]
@@ -15,7 +16,7 @@
[auto-ap.utils :refer [replace-by]]
[auto-ap.views.pages.transactions.manual :as manual]
[auto-ap.views.utils :refer [bind-field date->str dispatch-event nf active-when]]
[auto-ap.views.utils :refer [bind-field date->str dispatch-event nf active-when with-user]]
[goog.string :as gstring]
[re-frame.core :as re-frame]
[reagent.core :as reagent]))
@@ -26,52 +27,62 @@
(fn [db]
(-> db ::ledger-page)))
(re-frame/reg-sub
::last-params
(fn [db]
(-> db ::last-params)))
(re-frame/reg-sub
::params
:<- [::last-params]
:<- [::subs/client]
:<- [::side-bar/filter-params]
:<- [::table/table-params]
(fn [[client filter-params table-params]]
(cond-> {}
client (assoc :client-id (:id client))
(seq filter-params) (merge filter-params)
(seq table-params) (merge @(re-frame/subscribe [::table/table-params])))))
(fn [[last-params client filter-params table-params]]
(println "HERE?")
(let [params (cond-> {}
client (assoc :client-id (:id client))
(seq filter-params) (merge filter-params)
(seq table-params) (merge table-params))]
(when (not= params last-params)
(re-frame/dispatch [::params-change]))
params)))
(re-frame/reg-event-fx
::params-change
(fn [cofx [_]]
(let [new-params @(re-frame/subscribe [::params])]
(when (not= (::last-params (:db cofx)) new-params)
{:db (-> (:db cofx)
(assoc-in [::last-params] new-params)
(assoc-in [:status :loading] true))
:graphql {:token (-> cofx :db :user)
:query-obj {:venia/queries [[:ledger-page
new-params
[[:journal-entries [:id
:source
:amount
:note
:cleared-against
[:vendor
[:name :id]]
[:client
[:name :id]]
[:line-items
[:id :debit :credit :location
[:account [:id]]]]
:date]]
:total
:start
:end]]]}
:on-success [::received]}}))))
[with-user (re-frame/inject-cofx ::inject/sub [::params])]
(fn [{:keys [user ::params db]} [_]]
(println "execute")
{:db (-> db
(assoc-in [::last-params] params)
(assoc-in [:status :loading] true))
:graphql {:token user
:query-obj {:venia/queries [[:ledger-page
params
[[:journal-entries [:id
:source
:amount
:note
:cleared-against
[:vendor
[:name :id]]
[:client
[:name :id]]
[:line-items
[:id :debit :credit :location
[:account [:id]]]]
:date]]
:total
:start
:end]]]}
:on-success [::received]}}))
(re-frame/reg-event-fx
::unmounted
(fn [{:keys [db]} _]
{:db (dissoc db ::last-params ::table/table-params ::side-bar/filters ::ledger-page)}))
(re-frame/reg-event-db
::received
(fn [db [_ data]]
@@ -85,21 +96,19 @@
[:div
[:h1.title "Ledger"]
[table/table {:id :ledger
:params (re-frame/subscribe [::params])
:ledger-page (re-frame/subscribe [::ledger-page])
:status (re-frame/subscribe [::subs/status])
:on-params-change (fn [params]
(re-frame/dispatch [::params-change params]))}]
:status (re-frame/subscribe [::subs/status])}]
[manual/modal {:import-completed [::manual-import-completed ]}]]))
(defn ledger-page []
(reagent/create-class
{:display-name "ledger-page"
:component-did-mount #(re-frame/dispatch [::params-change {}])
:component-will-unmount #(re-frame/dispatch [::unmounted])
:reagent-render
(fn []
[side-bar-layout
{:side-bar [ledger-side-bar]
:main [ledger-content]}])}))
(let [params @(re-frame/subscribe [::params])]
[side-bar-layout
{:side-bar [ledger-side-bar]
:main [ledger-content]}]))}))