diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index c7498e58..91eef070 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -159,7 +159,6 @@ _ (assert-can-see-client (:id context) client-id) all-ledger-entries (full-ledger-for-client client-id) lookup-account (build-account-lookup client-id)] - (->graphql {:periods (reduce (fn [acc {:keys [start end]}] (conj acc diff --git a/src/cljs/auto_ap/status.cljs b/src/cljs/auto_ap/status.cljs index f12ffcdf..e10d0cb9 100644 --- a/src/cljs/auto_ap/status.cljs +++ b/src/cljs/auto_ap/status.cljs @@ -137,3 +137,9 @@ (do ^{:key (:info state)} [:p (:info state)]))])])) + +(defn big-loader [status] + (when (= :loading (:state status)) + [:div.container + [:div.column.is-4.is-offset-4.has-text-centered + [:div.loader.is-loading.is-active.big.is-centered]]])) diff --git a/src/cljs/auto_ap/views/pages/data_page.cljs b/src/cljs/auto_ap/views/pages/data_page.cljs index d756f692..c6fdbb0e 100644 --- a/src/cljs/auto_ap/views/pages/data_page.cljs +++ b/src/cljs/auto_ap/views/pages/data_page.cljs @@ -45,19 +45,26 @@ (fn [db [_ which]] (get-in db [::table-params which]))) +(re-frame/reg-sub + ::additional-params + (fn [db [_ which]] + (get-in db [::additional-params which]))) + (re-frame/reg-sub ::params (fn [[_ id]] [(re-frame/subscribe [::subs/client]) (re-frame/subscribe [::settled-filters id]) - (re-frame/subscribe [::table-params id])]) - (fn [[client filters table-params query-params]] + (re-frame/subscribe [::table-params id]) + (re-frame/subscribe [::additional-params id])]) + (fn [[client filters table-params additional-params]] (let [query-params (auto-ap.views.utils/query-params)] (cond-> {} client (assoc :client-id (:id client)) (seq query-params) (merge query-params) (seq filters) (merge filters) - (seq table-params) (merge table-params))))) + (seq table-params) (merge table-params) + (seq additional-params) (merge additional-params))))) (re-frame/reg-event-fx ::received @@ -109,6 +116,12 @@ (fn [{:keys [db]} [_ which params :as z]] {:db (update-in db [::table-params which] merge params)})) + +(re-frame/reg-event-fx + ::additional-params-changed + (fn [{:keys [db]} [_ which params :as z]] + {:db (update-in db [::additional-params which] merge params)})) + (re-frame/reg-sub ::specific-filters (fn [db [_ id]] diff --git a/src/cljs/auto_ap/views/pages/ledger.cljs b/src/cljs/auto_ap/views/pages/ledger.cljs index e072c328..7a4ec84e 100644 --- a/src/cljs/auto_ap/views/pages/ledger.cljs +++ b/src/cljs/auto_ap/views/pages/ledger.cljs @@ -23,7 +23,6 @@ [vimsical.re-frame.fx.track :as track] [auto-ap.views.pages.data-page :as data-page] [clojure.set :as set])) - (defn data-params->query-params [params] {:start (:start params 0) :sort (:sort params) @@ -35,6 +34,7 @@ :amount-gte (:amount-gte (:amount-range params)) :amount-lte (:amount-lte (:amount-range params))}) + (re-frame/reg-event-fx ::params-change [with-user] diff --git a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs index 1ba5b205..44336170 100644 --- a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs @@ -9,7 +9,11 @@ [auto-ap.views.utils :refer [date->str date-picker bind-field standard pretty dispatch-event local-today ->% ->$ str->date with-user]] [cljs-time.core :as t] [re-frame.core :as re-frame] - [auto-ap.status :as status])) + [auto-ap.status :as status] + [auto-ap.views.pages.data-page :as data-page] + [vimsical.re-frame.fx.track :as track] + [reagent.core :as reagent] + [clojure.set :as set])) (def ranges {:sales [40000 49999] :cogs [50000 59999] @@ -55,45 +59,6 @@ (fn [db] (-> db ::ledger-list-active?))) -(re-frame/reg-sub - ::loading - (fn [db] - (-> db ::loading))) - -(re-frame/reg-sub - ::ledger-list-loading - (fn [db] - (-> db ::ledger-list-loading))) - -(re-frame/reg-sub - ::ledger-list - (fn [db] - (-> db ::ledger-list :ledger-page))) - -(re-frame/reg-sub - ::last-ledger-params - (fn [db] - (-> db ::last-ledger-params ))) - -(re-frame/reg-sub - ::investigate-ledger-params - (fn [db] - (-> db ::investigate-ledger-params ))) - -(re-frame/reg-sub - ::ledger-params - :<- [::last-ledger-params] - :<- [::subs/client] - :<- [::ledger-table/table-params] - :<- [::investigate-ledger-params] - (fn [[last-params client table-params investigate-ledger-params]] - (let [params (cond-> {} - client (assoc :client-id (:id client)) - (seq table-params) (merge table-params) - (seq investigate-ledger-params) (merge investigate-ledger-params))] - (when (not= params last-params) - (re-frame/dispatch [::ledger-params-changed])) - params))) (re-frame/reg-sub ::period-accounts @@ -140,9 +105,7 @@ (re-frame/reg-event-db ::received (fn [db [_ data]] - (-> db - (assoc ::report (:profit-and-loss data)) - (assoc-in [::loading] false)))) + (-> db (assoc ::report (:profit-and-loss data))))) (re-frame/reg-sub ::params @@ -161,36 +124,23 @@ (fn [db] (assoc db ::ledger-list-active? false))) -(re-frame/reg-event-db - ::error - (fn [db [_ [error]]] - (assoc db ::error (:message error) - ::loading false))) - -(re-frame/reg-event-db - ::ledger-list-received - (fn [db [_ ledger-list]] - (assoc db - ::ledger-list ledger-list - ::ledger-list-loading false))) (re-frame/reg-event-fx ::params-change (fn [cofx [_ params]] (let [c @(re-frame/subscribe [::subs/client])] (cond-> {:db (-> (:db cofx) - (assoc-in [::error] nil) - (assoc-in [::loading] true) - (assoc-in [::params] params))} + (assoc-in [::params] params) + (dissoc ::report))} c (assoc :graphql (when @(re-frame/subscribe [::subs/client]) {:token (-> cofx :db :user) + :owns-state {:single ::page} :query-obj {:venia/queries [[:profit-and-loss {:client-id (:id c) :periods (mapv (fn [[start end] ] {:start (date->str start standard) :end (date->str end standard)} ) (:periods params))} [[:periods [[:accounts [:name :amount :account-type :id :numeric-code :location]]]]]]]} - :on-success [::received] - :on-error [::error]})))))) + :on-success [::received]})))))) @@ -211,19 +161,27 @@ :db (assoc db ::periods periods)})) +(defn data-params->query-params [params] + (when params + {:start (:start params 0) + :sort (:sort params) + :vendor-id (:id (:vendor params)) + :client-id (:client-id params ) + :from-numeric-code (:from-numeric-code params) + :to-numeric-code (:to-numeric-code params) + :location (:location params) + :date-range (:date-range params)})) + (re-frame/reg-event-fx - ::ledger-params-changed - [with-user (re-frame/inject-cofx ::inject/sub [::ledger-params])] - (fn [{:keys [user ::ledger-params db]} [_ ]] + ::ledger-params-change + [with-user] + (fn [{:keys [user db]} [_ ledger-params]] (if (seq ledger-params) - {:db (assoc db - ::ledger-list-loading true - ::last-ledger-params ledger-params) - :graphql {:token user - :owns-state {:single ::ledger-list} + {:graphql {:token user + :owns-state {:single [::data-page/page ::ledger]} :query-obj {:venia/queries [[:ledger-page - ledger-params + (data-params->query-params ledger-params) [[:journal-entries [:id :source :amount @@ -238,22 +196,22 @@ :total :start :end]]]} - :on-success [::ledger-list-received]}}))) + :on-success (fn [result] + + [::data-page/received ::ledger (set/rename-keys (:ledger-page result) + {:journal-entries :data})])}}))) -(re-frame/reg-event-db +(re-frame/reg-event-fx ::investigate-clicked - (fn [db [_ location from-numeric-code to-numeric-code which]] + (fn [{:keys [db]} [_ location from-numeric-code to-numeric-code which]] (let [[from to] (get @(re-frame/subscribe [::periods]) which)] - (-> db - (assoc - ::ledger-list-active? true - ::ledger-list-loading true - ::investigate-ledger-params {:client-id (:id @(re-frame/subscribe [::subs/client])) - :from-numeric-code from-numeric-code - :to-numeric-code to-numeric-code - :location location - :date-range {:start (date->str from standard) - :end (date->str to standard)}}))))) + {:db (-> db (assoc ::ledger-list-active? true)) + :dispatch [::data-page/additional-params-changed ::ledger {:client-id (:id @(re-frame/subscribe [::subs/client])) + :from-numeric-code from-numeric-code + :to-numeric-code to-numeric-code + :location location + :date-range {:start (date->str from standard) + :end (date->str to standard)}}]}))) (def groupings {:sales [["40000-43999 Food Sales " 40000 43999] @@ -474,7 +432,7 @@ (fn [] (let [current-client @(re-frame/subscribe [::subs/client]) user @(re-frame/subscribe [::subs/user]) - error @(re-frame/subscribe [::error]) + status @(re-frame/subscribe [::status/single ::page]) params @(re-frame/subscribe [::params]) periods @(re-frame/subscribe [::periods])] @@ -485,6 +443,7 @@ [:div [:h1.title "Profit and Loss"] + [status/status-notification {:statuses [[::status/single ::page]]}] [:div.report-controls [:h2.title.is-4 "Range"] [:div @@ -604,63 +563,66 @@ :event [::date-picked] :popper-props (clj->js {:placement "right"}) :subscription params}]]]])] - (cond - error - [:div.notification.is-warning error] - - @(re-frame/subscribe [::loading]) - [:div [:i.icon.fa.fa-spin.fa-spinner]] - - :else - [:div + [status/big-loader status] + [:div + [:<> + (for [location @(re-frame/subscribe [::locations])] + ^{:key (str location "-summary")} + [location-summary location params] + )] + [:h2.title.is-4 {:style {:margin-bottom "1rem"}} "Detail"] + [:table.table.compact.balance-sheet + [:tbody + [:tr + [:td.has-text-right "Period Ending"] + (map-periods + (fn [i] + [:<> + [:td.has-text-right (date->str (get-in periods [i 1]))] + [:td]]) + (fn [i] + [:td.has-text-right "𝝙"]) + periods)] [:<> (for [location @(re-frame/subscribe [::locations])] - ^{:key (str location "-summary")} - [location-summary location params] - )] - [:h2.title.is-4 {:style {:margin-bottom "1rem"}} "Detail"] - [:table.table.compact.balance-sheet - [:tbody - [:tr - [:td.has-text-right "Period Ending"] - (map-periods - (fn [i] - [:<> - [:td.has-text-right (date->str (get-in periods [i 1]))] - [:td]]) - (fn [i] - [:td.has-text-right "𝝙"]) - periods)] - [:<> - (for [location @(re-frame/subscribe [::locations])] - ^{:key location} - [location-rows location] - )]]]])]))) + ^{:key location} + [location-rows location] + )]]]]]))) {:component-will-mount #(do (re-frame/dispatch-sync [::range-selected (and-last-year [(t/minus (local-today) (t/period :years 1)) (local-today)]) nil])) })) +(re-frame/reg-event-fx + ::unmounted + (fn [{:keys [db]} _] + {::track/dispose {:id ::ledger-params}})) +(re-frame/reg-event-fx + ::mounted + (fn [{:keys [db]} _] + {::track/register {:id ::ledger-params + :subscription [::data-page/params ::ledger] + :event-fn (fn [params] [::ledger-params-change params])}})) (defn ledger-list [_ ] - (let [ledger-page (re-frame/subscribe [::ledger-list])] - [:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::ledger-list-closing])}] - [:div - [:h1.title "Ledger entries"] - [ledger-table/table {:id :ledger - :ledger-page @ledger-page - :status @(re-frame/subscribe [::status/single ::ledger-list]) - :params (re-frame/subscribe [::ledger-params]) - :on-params-change (fn [params] - (re-frame/dispatch [::ledger-params-changed params]))}]]])) + [:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::ledger-list-closing])}] + [:div + [:h1.title "Ledger entries"] + [ledger-table/table {:id :ledger + :data-page ::ledger}]]]) (defn profit-and-loss-page [] - (let [ledger-list-active? @(re-frame/subscribe [::ledger-list-active?]) - user (re-frame/subscribe [::subs/user]) - ledger-params @(re-frame/subscribe [::ledger-params])] - (if (not= "manager" (:user/role @user)) - [side-bar-layout - {:side-bar [ledger-side-bar] - :main [profit-and-loss-content] - :right-side-bar [appearing-side-bar - {:visible? ledger-list-active?} - [ledger-list]]}] - [:div "Not authorized"]))) + (reagent/create-class + {:display-name "profit-and-loss-page" + :component-did-mount #(re-frame/dispatch [::mounted]) + :component-will-unmount #(re-frame/dispatch [::unmounted]) + :reagent-render + (fn [] + (let [ledger-list-active? @(re-frame/subscribe [::ledger-list-active?]) + user (re-frame/subscribe [::subs/user])] + (if (not= "manager" (:user/role @user)) + [side-bar-layout + {:side-bar [ledger-side-bar] + :main [profit-and-loss-content] + :right-side-bar [appearing-side-bar + {:visible? ledger-list-active?} + [ledger-list]]}] + [:div "Not authorized"])))}))