From 8036d34a4e03e41c399b8a462c87de15fb42a884 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Mon, 17 Aug 2020 10:58:05 -0700 Subject: [PATCH] more standardization --- project.clj | 1 - src/clj/auto_ap/routes/invoices.clj | 1 + src/cljc/auto_ap/utils.cljc | 9 + .../components/expense_accounts_dialog.cljs | 291 +++++++++--------- src/cljs/auto_ap/views/components/grid.cljs | 4 +- .../views/components/invoice_table.cljs | 33 +- src/cljs/auto_ap/views/pages/admin/rules.cljs | 15 +- src/cljs/auto_ap/views/pages/admin/users.cljs | 14 +- src/cljs/auto_ap/views/pages/data_page.cljs | 20 +- .../auto_ap/views/pages/invoices/form.cljs | 45 +-- src/cljs/auto_ap/views/pages/payments.cljs | 14 +- .../auto_ap/views/pages/unpaid_invoices.cljs | 60 ++-- 12 files changed, 252 insertions(+), 255 deletions(-) diff --git a/project.clj b/project.clj index 3c14ee92..0b5d0d39 100644 --- a/project.clj +++ b/project.clj @@ -15,7 +15,6 @@ [ring/ring-defaults "0.2.1"] [mount "0.1.16"] [tolitius/yang "0.1.10"] - [day8.re-frame/forward-events-fx "0.0.6"] [ring "1.6.3" :exclusions [commons-codec commons-io clj-time diff --git a/src/clj/auto_ap/routes/invoices.clj b/src/clj/auto_ap/routes/invoices.clj index 328e4b7b..acf2f02d 100644 --- a/src/clj/auto_ap/routes/invoices.clj +++ b/src/clj/auto_ap/routes/invoices.clj @@ -291,6 +291,7 @@ (when-not (seq transactions) (throw (ex-info "No invoices found." {:imports (str imports)}))) + (log/info "creating invoice" transactions) @(d/transact (d/connect uri) (vec (set transactions)))))) diff --git a/src/cljc/auto_ap/utils.cljc b/src/cljc/auto_ap/utils.cljc index 1a66c13b..73d00856 100644 --- a/src/cljc/auto_ap/utils.cljc +++ b/src/cljc/auto_ap/utils.cljc @@ -31,6 +31,15 @@ replaced (into [x] replaced)))) + +(defn remove-by [xs f x] + (into [] + (filter + (fn [t] + (if (= (f t) (f x)) + false + true)) + xs))) (defn merge-by [xs f x] (let [found? (atom false) replaced (mapv diff --git a/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs b/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs index cc959c23..47215ed7 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_dialog.cljs @@ -1,51 +1,28 @@ (ns auto-ap.views.components.expense-accounts-dialog - (:require [auto-ap.entities.invoices-expense-accounts - :as - invoices-expense-accounts] - [auto-ap.events :as events] + (:require [auto-ap.forms :as forms] + [auto-ap.status :as status] [auto-ap.subs :as subs] - [auto-ap.views.components.modal :refer [action-modal]] - [auto-ap.views.components.typeahead :refer [typeahead]] - [auto-ap.views.utils :refer [bind-field dispatch-event]] - [clojure.spec.alpha :as s] + [auto-ap.utils :refer [by]] + [auto-ap.views.components.modal :as modal] + [auto-ap.views.components.typeahead :refer [typeahead-entity]] + [auto-ap.views.pages.invoices.common :refer [invoice-read]] + [auto-ap.views.utils :refer [dispatch-event with-user]] [clojure.string :as str] [goog.string :as gstring] [re-frame.core :as re-frame])) (re-frame/reg-sub - ::change-expense-accounts + ::can-submit (fn [db] - (-> db ::change-expense-accounts))) - -(re-frame/reg-sub - ::expense-account - :<- [::change-expense-accounts] - (fn [{{:keys [expense-accounts] :or {expense-accounts [] }} :invoice} [_ invoice-expense-account-id]] - (first (filter #(= invoice-expense-account-id (:id %)) expense-accounts)))) + true)) (re-frame/reg-event-fx - ::change-expense-accounts - (fn [{:keys [db]} [_ i]] - {:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}] - :db (assoc-in db [::change-expense-accounts :invoice] i)})) - - -(re-frame/reg-event-db - ::change - (fn [db [_ id f v]] - (update-in db [::change-expense-accounts :invoice :expense-accounts] - (fn [expense-accounts] - (mapv - (fn [expense-account] - (if (= id (:id expense-account)) - (assoc-in expense-account f v) - expense-account)) - expense-accounts))))) - -(re-frame/reg-event-fx - ::change-expense-accounts-saving - (fn [{:keys [db]} [_ on-success id ]] - (let [{{:keys [expense-accounts total]} :invoice} @(re-frame/subscribe [::change-expense-accounts]) + ::try-save + [(forms/in-form ::form)] + (fn [{:keys [db]} [_ id ]] + (let [{{:keys [ total]} :invoice + :keys [expense-accounts]} (:data db) + expense-accounts (vals expense-accounts) expense-accounts-total (->> expense-accounts (map :new-amount) (map js/parseFloat) @@ -56,65 +33,71 @@ (reduce + 0)) does-add-up? (< (Math/abs (- expense-accounts-total (js/parseFloat total))) 0.001)] (if (and does-add-up? - (every? :new-amount expense-accounts) - (s/valid? (s/* ::invoices-expense-accounts/invoices-expense-account) expense-accounts)) + (every? :new-amount expense-accounts)) - {:dispatch [::change-expense-accounts-submit on-success id]} - {:dispatch [::events/modal-status ::change-expense-accounts {:saving? false :error-message "Expense accounts do not add up."}]})))) + {:dispatch [::save]} + {:dispatch [::status/error ::form [{:message "Expense accounts do not add up."}]]})))) + -(re-frame/reg-event-db - ::saving-error - (fn [db [_ message]] - (assoc-in db [::change-expense-accounts :error] message))) (re-frame/reg-event-fx - ::change-expense-accounts-submit - (fn [{:keys [db] } [_ on-success id]] - (let [{:keys [id expense-accounts]} (get-in db [::change-expense-accounts :invoice])] + ::save + [with-user (forms/in-form ::form)] + (fn [{:keys [db user] } [_ id]] + (let [{{:keys [id]} :invoice + :keys [expense-accounts]} (:data db) + expense-accounts (vals expense-accounts)] {:graphql - {:token (-> db :user) + {:token user + :owns-state {:single ::form} :query-obj {:venia/operation {:operation/type :mutation :operation/name "EditExpenseAccounts"} :venia/queries [{:query/data [:edit-expense-accounts {:invoice-id id - :expense-accounts (map (fn [ea] {:id (if (clojure.string/includes? (:id ea) "new-") + :expense-accounts (map (fn [ea] {:id (if (clojure.string/includes? (str (:id ea)) "new-") nil - (:id ea) - ) + (:id ea)) :amount (:new-amount ea) :location (:location ea) :account-id (:id (:account ea))}) expense-accounts)} - [:id :total :outstanding-balance :invoice-number :date :status - [:vendor [:name :id]] - [:expense_accounts [:amount :id :location - [:account [:id :name :numeric-code :location]]]] - [:client [:name :id :locations]] - [:payments [:amount :id [:payment [:amount :s3_url :check_number ]]]]]]}]} - :on-success on-success}}))) + invoice-read]}]} + :on-success (fn [result] + [::updated (:edit-expense-accounts result)])}}))) + +(re-frame/reg-event-fx + ::updated + (fn [_ _] + {:dispatch [::modal/modal-closed]})) (re-frame/reg-event-db - ::add-expense-account-split + ::add-split + [(forms/in-form ::form)] (fn [db _] - (let [{{{:keys [locations]} :client} :invoice} @(re-frame/subscribe [::change-expense-accounts])] - (update-in db [::change-expense-accounts :invoice :expense-accounts] - conj {:amount "0.0" :id (str "new-" (random-uuid)) :account {} :location (first locations)})))) + (update-in db [:data :expense-accounts] + assoc (str "new-" (random-uuid)) {:amount "0.0" :account nil :location (-> db :data :invoice :client :locations first) }))) (re-frame/reg-event-db ::remove-expense-account-split + [(forms/in-form ::form)] (fn [db [_ x]] - (update-in db [::change-expense-accounts :invoice :expense-accounts] - (fn [expense-accounts] - (vec (filter #(not= x (:id %)) expense-accounts) ))))) + (update-in db [:data :expense-accounts] dissoc x))) +(def change-expense-accounts-form (forms/vertical-form {:submit-event [::try-save] + :change-event [::forms/change ::form] + :can-submit [::can-submit] + :id ::form})) -(defn change-expense-accounts-modal [{:keys [updated-event]}] - (let [{{:keys [expense-accounts total] :or {expense-accounts [] total 0} {:keys [locations]} :client} :invoice error :error :as data} @(re-frame/subscribe [::change-expense-accounts]) +(defn form [] + (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form]) + expense-accounts (:expense-accounts data) + {:keys [total] :or {total 0} {:keys [locations] :as client} :client} (:invoice data) + {:keys [form-inline horizontal-field field raw-field error-notification submit-button]} change-expense-accounts-form multi-location? (> (count locations) 1) - change-event [::change] - chooseable-expense-accounts @(re-frame/subscribe [::subs/accounts (:client (:invoice data))]) + chooseable-expense-accounts @(re-frame/subscribe [::subs/accounts client]) expense-accounts-total (->> expense-accounts + vals (map :new-amount) (map js/parseFloat) (map #(or % 0.0)) @@ -122,85 +105,91 @@ 0.0 %)) (reduce + 0))] - [action-modal {:id ::change-expense-accounts - :title "Change expense accounts" - :action-text "Save" - :save-event [::change-expense-accounts-saving updated-event] - :can-submit? true} - (when error - [:div error] ) - + [:div [:div - [:a.button.is-primary {:on-click (dispatch-event [::add-expense-account-split])} "Add split"]] - [:table.table - [:thead - [:tr - [:th {:style {:width "500px"}} "Expense Account"] - (when multi-location? - [:th {:style {:width "200px"}} "Location"]) - [:th {:style {:width "200px"}} "Original Amount"] - [:th {:style {:width "300px"}} "Amount"] - [:th {:style {:width "5em"}}]]] - [:tbody - (doall (for [{:keys [id] :as expense-account} expense-accounts - :let [sub @(re-frame/subscribe [::expense-account (:id expense-account)])]] - ^{:key id} - [:tr - [:td.expandable [:div.control - [bind-field - [typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) chooseable-expense-accounts) - :type "typeahead" - :field [:account :id] - :event [::change id] - :spec ::invoices-expense-accounts/account-id - :subscription sub}]]]] + [:a.button.is-primary {:on-click (dispatch-event [::add-split])} "Add split"]] + (form-inline {} + [:table.table + [:thead + [:tr + [:th {:style {:width "500px"}} "Expense Account"] + (when multi-location? + [:th {:style {:width "200px"}} "Location"]) + [:th {:style {:width "200px"}} "Original Amount"] + [:th {:style {:width "300px"}} "Amount"] + [:th {:style {:width "5em"}}]]] + [:tbody + (doall (for [[id expense-account] expense-accounts] + ^{:key id} + [:tr + [:td.expandable [:div.control + (raw-field + [typeahead-entity {:matches chooseable-expense-accounts + :type "typeahead-entity" + :match->text (fn [x] (str (:numeric-code x) " - " (:name x))) + :field [:expense-accounts id :account]}])]] - (when multi-location? - [:td - (if-let [forced-location (:location @(re-frame/subscribe [::subs/account (:client (:invoice data)) (-> sub :account :id )]))] - [:div.select - [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] - [:div.select - [bind-field - [:select {:type "select" - :field [:location] - :spec (set locations) - :event [::change id] - :subscription sub} - (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]) - - [:td - (str "$" (get-in sub [:amount]))] - [:td - [:div.control - [:div.field.has-addons.is-extended - [:p.control [:a.button.is-static "$"]] - [:p.control - [bind-field - [:input.input {:type "number" - :field [:new-amount-temp] - :style {:text-align "right"} - :on-blur (dispatch-event [::change id [:new-amount] (:new-amount-temp sub)]) - :on-key-down (fn [e ] - (if (= 13 (.-keyCode e)) - (do - - (re-frame/dispatch [::change id [:new-amount] (:new-amount-temp sub)]) - true) - false)) - :event [::change id] - :subscription sub - :value (get-in data [:new-amount-temp]) - - :max (:total data) - :step "0.01"}]]]]]] - [:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split (:id expense-account)])} [:i.fa.fa-times]]]])) - [:tr - [:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "] - [:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]] - [:tr - [:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "] - [:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]] - [:tr - [:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "] - [:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]]])) + (when multi-location? + [:td + (if-let [forced-location (:location @(re-frame/subscribe [::subs/account client (get-in expense-accounts [id :account :id])]))] + [:div.select + [:select {:disabled "disabled" :value forced-location} [:option {:value forced-location} forced-location]]] + [:div.select + (raw-field + [:select {:type "select" + :field [:expense-accounts id :location] + :spec (set locations)} + (map (fn [l] ^{:key l} [:option {:value l} l]) locations)])])]) + + [:td + (str "$" (get-in expense-accounts [id :amount]))] + [:td + [:div.control + [:div.field.has-addons.is-extended + [:p.control [:a.button.is-static "$"]] + [:p.control + (raw-field + [:input.input {:type "number" + :field [:expense-accounts id :new-amount-temp] + :style {:text-align "right"} + :on-blur (dispatch-event [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])]) + :on-key-down (fn [e ] + (if (= 13 (.-keyCode e)) + (do + (re-frame/dispatch [::forms/change ::form [:expense-accounts id :new-amount] (get-in expense-accounts [id :new-amount-temp])]) + true) + false)) + :max (:total data) + :step "0.01"}])]]]] + [:td [:a.button {:on-click (dispatch-event [::remove-expense-account-split id])} [:i.fa.fa-times]]]])) + [:tr + [:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Invoice total: "] + [:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" total ) )]] + [:tr + [:td { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Account total: "] + [:td { :style { :text-align "right"} } (str (gstring/format "$%.2f" expense-accounts-total ) )]] + [:tr + [:td.no-border { :col-span (if multi-location? "3" "2") :style { :text-align "right"} } "Difference: "] + [:td.no-border { :style { :text-align "right"} } (str (gstring/format "$%.2f" (- total expense-accounts-total) ) )]]]])])) + +(re-frame/reg-event-fx + ::show + (fn [{:keys [db]} [_ i]] + (let [accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id])] + {:dispatch [::modal/modal-requested {:title "Change expense accounts" + :body [form] + :confirm {:value "Save" + :status-from [::status/single ::form] + :class "is-primary" + :on-click (dispatch-event [::try-save]) + :can-submit [::can-submit] + :close-event [::status/completed ::form]}}] + :db (-> db + (forms/start-form ::form + + {:expense-accounts (by :id + (mapv + (fn [ea] + (assoc ea :account (accounts-by-id (:id (:account ea))))) + (:expense-accounts i))) + :invoice i}))}))) diff --git a/src/cljs/auto_ap/views/components/grid.cljs b/src/cljs/auto_ap/views/components/grid.cljs index bfde5ad7..399864b5 100644 --- a/src/cljs/auto_ap/views/components/grid.cljs +++ b/src/cljs/auto_ap/views/components/grid.cljs @@ -170,7 +170,9 @@ (let [children (r/children (r/current-component))] [:> Consumer {} (fn [consume] - (let [{:strs [column-count status]} (js->clj consume)] + (let [{:strs [column-count status check-boxes?]} (js->clj consume) + column-count (cond-> column-count + check-boxes? inc)] (r/as-element (cond (= :loading (:state status)) ^{:key "loading-body"} diff --git a/src/cljs/auto_ap/views/components/invoice_table.cljs b/src/cljs/auto_ap/views/components/invoice_table.cljs index 5d420d42..989d8ce6 100644 --- a/src/cljs/auto_ap/views/components/invoice_table.cljs +++ b/src/cljs/auto_ap/views/components/invoice_table.cljs @@ -15,7 +15,8 @@ [cljs-time.core :as t] [clojure.string :as str] [goog.string :as gstring] - [re-frame.core :as re-frame])) + [re-frame.core :as re-frame] + [auto-ap.views.components.expense-accounts-dialog :as expense-accounts-dialog])) (defn query [params] {:venia/queries [[:invoice_page @@ -84,15 +85,14 @@ {:invoice-id id} invoice-read]}]} :on-success (fn [result] - [::invoice-updated (assoc (:unvoid-invoice result) - :class "live-added")])}})) + [::invoice-updated (:unvoid-invoice result)])}})) (re-frame/reg-event-fx ::invoice-updated (fn [{:keys [db]} [_ invoice]] {:db db})) -(defn row [{:keys [invoice check-boxes checked selected-client overrides expense-event ]}] +(defn row [{:keys [invoice check-boxes checked selected-client overrides expense-event actions]}] (let [{:keys [client payments expense-accounts invoice-number date due total outstanding-balance id vendor checkable?] :as i} invoice accounts-by-id @(re-frame/subscribe [::subs/accounts-by-id client]) account->name #(:name (accounts-by-id (:id %)))] @@ -134,10 +134,12 @@ ^{:key (:id e)} [:span.dropdown-item (account->name (:account e)) " " (gstring/format "$%.2f" (:amount e) ) ]) - [:hr.dropdown-divider] + (when (get actions :expense-accounts) + [:<> - (when expense-event - [:a.dropdown-item.is-primary {:on-click (dispatch-event (conj expense-event i))} "Change"])]]]) + [:hr.dropdown-divider] + + [:a.dropdown-item.is-primary {:on-click (dispatch-event [::expense-accounts-dialog/show i])} "Change"]])]]]) [:span {:style {:margin-left "1em"}}] (when (seq payments) [:<> @@ -165,17 +167,20 @@ (when (= :cleared (:status (:payment payment))) (str " - " (:post-date (:transaction (:payment payment))))))]))]] [:span {:style {:margin-right "1em"}}]]) - (when (not= ":voided" (:status i)) + (when (and (get actions :edit) + (not= ":voided" (:status i))) [buttons/fa-icon {:icon "fa-pencil" :event [::form/editing i]}]) - (when (and (= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i))) + (when (and (get actions :void) + (= (:outstanding-balance i) (:total i)) (not= ":voided" (:status i))) [buttons/sl-icon {:icon "icon-bin-2" :event [::void-invoice i]}]) - (when (= ":voided" (:status i)) + (when (and (get actions :void) + (= ":voided" (:status i))) [buttons/fa-icon {:icon "fa-undo" :event [::unvoid-invoice i]}])]]])) -(defn invoice-table [{:keys [id data checked status vendors check-boxes on-check-changed expense-event overrides]}] +(defn invoice-table [{:keys [id data checked status check-boxes on-check-changed overrides actions]}] (let [selected-client @(re-frame/subscribe [::subs/client]) {:keys [sort]} @(re-frame/subscribe [::table-params]) selected-client @(re-frame/subscribe [::subs/client]) @@ -204,7 +209,7 @@ :checked checked :status status :check-boxes? check-boxes - :column-count (if selected-client 7 8)} + :column-count (if selected-client 8 9)} [grid/controls data [:div.level-item "Outstanding " (nf (:outstanding data))]] @@ -235,5 +240,5 @@ ^{:key id} [row {:invoice i :selected-client selected-client - :overrides overrides - :expense-event expense-event}])]])])) + :actions actions + :overrides overrides}])]])])) diff --git a/src/cljs/auto_ap/views/pages/admin/rules.cljs b/src/cljs/auto_ap/views/pages/admin/rules.cljs index ad79bd6d..832b301e 100644 --- a/src/cljs/auto_ap/views/pages/admin/rules.cljs +++ b/src/cljs/auto_ap/views/pages/admin/rules.cljs @@ -11,11 +11,11 @@ [auto-ap.views.utils :refer [dispatch-event with-user]] [vimsical.re-frame.cofx.inject :as inject] [vimsical.re-frame.fx.track :as track] - [day8.re-frame.forward-events-fx] [auto-ap.events :as events] [auto-ap.utils :refer [replace-by merge-by]] [re-frame.core :as re-frame] - [auto-ap.status :as status])) + [auto-ap.status :as status] + [auto-ap.effects.forward :as forward])) ;; SUBS @@ -91,16 +91,17 @@ (re-frame/reg-event-db ::deleted-transaction-rule [(re-frame/path [::page :transaction-rules])] - (fn [transaction-rules [_ [_ {id :delete-transaction-rule}]]] + (fn [transaction-rules [_ {id :delete-transaction-rule}]] (merge-by transaction-rules :id {:id id :class "live-removed"}))) (re-frame/reg-event-fx ::mounted (fn [{:keys [db]}] {:dispatch-n [[::events/yodlee-merchants-needed]] - :forward-events {:register ::page - :events #{::table/deleted-transaction-rule} - :dispatch-to [::deleted-transaction-rule]} + ::forward/register {:id ::page + :events #{::table/deleted-transaction-rule} + :event-fn (fn [[_ result]] + [::deleted-transaction-rule result])} ::track/register {:id ::params :subscription [::params] :event-fn (fn [params] [::params-change params])}})) @@ -108,7 +109,7 @@ (re-frame/reg-event-fx ::unmounted (fn [{:keys [db]}] - {:forward-events {:unregister ::page} + {::forward/dispose {:id ::page} ::track/dispose {:id ::params}})) ;; VIEWS diff --git a/src/cljs/auto_ap/views/pages/admin/users.cljs b/src/cljs/auto_ap/views/pages/admin/users.cljs index afc92419..182bff58 100644 --- a/src/cljs/auto_ap/views/pages/admin/users.cljs +++ b/src/cljs/auto_ap/views/pages/admin/users.cljs @@ -17,7 +17,8 @@ [auto-ap.routes :as routes] [bidi.bidi :as bidi] [auto-ap.status :as status] - [auto-ap.views.pages.admin.users.form :as form])) + [auto-ap.views.pages.admin.users.form :as form] + [auto-ap.effects.forward :as forward])) @@ -56,19 +57,20 @@ :role [:clients [:id :name]]]]]} :on-success [::received]} - :forward-events {:register ::edited-user - :events #{::form/saved} - :dispatch-to [::saved]}})) + ::forward/register {:id ::edited-user + :events #{::form/saved} + :event-fn (fn [[_ query-result]] + [::saved query-result])}})) (re-frame/reg-event-fx ::unmounted (fn [{:keys [db]} _] - {:forward-events {:unregister ::edited-user}})) + {::forward/dispose {:id ::edited-user}})) (re-frame/reg-event-db ::saved - (fn [db [_ [_ query-result]]] + (fn [db [_ query-result]] (-> db (update ::users replace-by diff --git a/src/cljs/auto_ap/views/pages/data_page.cljs b/src/cljs/auto_ap/views/pages/data_page.cljs index 0f198cb4..25fbc6ad 100644 --- a/src/cljs/auto_ap/views/pages/data_page.cljs +++ b/src/cljs/auto_ap/views/pages/data_page.cljs @@ -22,12 +22,18 @@ (let [checked (or checked #{})] (disj checked to-remove)))))) +(re-frame/reg-event-db + ::reset-checked + (fn [db [_ id]] + + (update db ::checked dissoc id))) + (re-frame/reg-event-db ::updated-entity (fn [db [_ id entity]] (update-in db - [::data id] - replace-by :id (assoc entity :class "live-added")))) + [::data id :data] + replace-by :id (update entity :class #(or % "live-added"))))) (re-frame/reg-event-db ::received @@ -62,13 +68,3 @@ (defn in-page-entities [which] (re-frame/path [::data which :data ] )) -(re-frame/reg-event-db - ::entity-updated - (fn [db [_ which [_ entity] :as g]] - (println g) - (update-in db [::data which :data] - (fn [entities] - (let [by-id (by :id entities )] - (if (by-id (:id entity)) - (merge-by entities :id entity) - (into [entity] entities))))))) diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 08578d5b..e9ca4342 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -108,11 +108,13 @@ (re-frame/reg-event-db ::updated (fn [db [_ invoice command]] - (-> db - (forms/stop-form ::form ) - (forms/start-form ::form {:client @(re-frame/subscribe [::subs/client]) - :status :unpaid - :date (date->str (c/now) standard)})))) + (if (= :create command) + (-> db + (forms/stop-form ::form ) + (forms/start-form ::form {:client @(re-frame/subscribe [::subs/client]) + :status :unpaid + :date (date->str (c/now) standard)})) + db))) (re-frame/reg-event-db ::adding @@ -188,27 +190,28 @@ ::saving [with-user (forms/in-form ::form)] (fn [{:keys [user] {:keys [data]} :db} _] - (let [command (if (:id data) - :edit - :create)] - {:graphql - {:token user - :owns-state {:single ::form} - :query-obj (if (:id data) - @(re-frame/subscribe [::edit-query]) - @(re-frame/subscribe [::create-query])) - :on-success (fn [result] - [::updated (assoc (if (:id data) - (:edit-invoice result) - (:add-invoice result)) - :class "live-added")]) - :on-error [::forms/save-error ::form]}}))) + {:graphql + {:token user + :owns-state {:single ::form} + :query-obj (if (:id data) + @(re-frame/subscribe [::edit-query]) + @(re-frame/subscribe [::create-query])) + :on-success (fn [result] + [::updated + (assoc (if (:id data) + (:edit-invoice result) + (:add-invoice result)) + :class "live-added") + (if (:id data) + :edit + :create)]) + :on-error [::forms/save-error ::form]}})) (re-frame/reg-event-fx ::added-and-printed (fn [{:keys [db]} [_ result]] (let [invoice (first (:invoices (:add-and-print-invoice result)))] - {:dispatch-n [[::updated (assoc invoice :class "live-added")] + {:dispatch-n [[::updated (assoc invoice :class "live-added") :create] [::checks-printed [invoice] (:pdf-url (:add-and-print-invoice result))]]}))) (re-frame/reg-event-db diff --git a/src/cljs/auto_ap/views/pages/payments.cljs b/src/cljs/auto_ap/views/pages/payments.cljs index ef856ce1..b0eaf720 100644 --- a/src/cljs/auto_ap/views/pages/payments.cljs +++ b/src/cljs/auto_ap/views/pages/payments.cljs @@ -20,7 +20,8 @@ [auto-ap.views.pages.check :as check] [auto-ap.subs :as subs] [auto-ap.status :as status] - [vimsical.re-frame.fx.track :as track])) + [vimsical.re-frame.fx.track :as track] + [auto-ap.effects.forward :as forward])) @@ -72,7 +73,7 @@ (fn [{:keys [db]} _] {:db (dissoc db ::last-params ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::payment-page) ::track/dispose {:id ::params} - :forward-events {:unregister ::page}})) + ::forward/dispose {:id ::page}})) (re-frame/reg-event-fx ::mounted @@ -80,14 +81,15 @@ {::track/register {:id ::params :subscription [::params] :event-fn (fn [params] [::params-change params])} - :forward-events {:register ::page - :events #{::table/payment-voided} - :dispatch-to [::payment-voided]}})) + ::forward/register {:id ::page + :events #{::table/payment-voided} + :event-fn (fn [[_ result]] + [::payment-voided result])}})) (re-frame/reg-event-db ::payment-voided [(re-frame/path [::payment-page :payments])] - (fn [db [_ [_ {:keys [void-payment]}]]] + (fn [db [_ {:keys [void-payment]}]] (mapv (fn [c] (if (= (:id c) (:id void-payment)) (assoc void-payment :class "live-removed") diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index 9ef76603..3689a139 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -6,11 +6,10 @@ [auto-ap.subs :as subs] [auto-ap.utils :refer [by merge-by replace-if]] [auto-ap.views.components.dropdown :refer [drop-down]] + [auto-ap.effects.forward :as forward] [auto-ap.views.components.expense-accounts-dialog :as - expense-accounts-dialog - :refer - [change-expense-accounts-modal]] + expense-accounts-dialog] [auto-ap.views.components.invoice-table :as table] [auto-ap.views.components.invoices.side-bar :as @@ -36,10 +35,8 @@ [reagent.core :as r] [vimsical.re-frame.fx.track :as track])) -(re-frame/reg-sub - ::change-expense-accounts - (fn [db] - (-> db ::change-expense-accounts))) +;; TODO: Sort out approaches for which buttons to show +;; TODO: make it so filters are basically free (re-frame/reg-sub ::params @@ -77,8 +74,8 @@ (fn [{:keys [db]} _] {:db (dissoc db ::table/table-params ::side-bar/filters ::side-bar/settled-filters ::last-params) :dispatch [::data-page/dispose :invoices] - :forward-events [{:unregister ::updated} - {:unregister ::checks-printed}] + ::forward/dispose [{:id ::updated} + {:id ::checks-printed}] ::track/dispose [{:id ::params}]})) (re-frame/reg-event-fx @@ -88,12 +85,14 @@ :subscription [::params] :event-fn (fn [params] [::params-change params])}] - :forward-events [{:register ::updated - :events #{::table/invoice-updated ::form/updated} - :dispatch-to [::data-page/entity-updated :invoices]} - {:register ::checks-printed - :events #{::form/checks-printed ::advanced-print-checks/checks-printed ::handwritten-checks/succeeded} - :dispatch-to [::checks-printed]}]})) + ::forward/register [{:id ::updated + :events #{::table/invoice-updated ::form/updated ::expense-accounts-dialog/updated} + :event-fn (fn [[_ invoice]] + [::data-page/updated-entity :invoices invoice])} + {:id ::checks-printed + :events #{::form/checks-printed ::advanced-print-checks/checks-printed ::handwritten-checks/succeeded} + :event-fn (fn [[_ invoices pdf-url]] + [::checks-printed invoices pdf-url])}]})) (re-frame/reg-sub @@ -104,7 +103,6 @@ (filter (comp #(get checked %) :id) (:data data)))) - (defn print-checks-query [invoice-payments bank-account-id type client-id] {:venia/operation {:operation/type :mutation :operation/name "PrintChecks"} @@ -132,18 +130,19 @@ (:client db)) :on-success (fn [result] [::checks-printed - [nil - (:invoices (:print-checks result)) - (:pdf-url (:print-checks result))]])}})) + (:invoices (:print-checks result)) + (:pdf-url (:print-checks result))])}})) (re-frame/reg-event-fx ::checks-printed - (fn [{:keys [db]} [_ [_ invoices pdf-url]]] - {:dispatch-n (cond->> (mapv - (fn [i] - [::data-page/entity-updated :invoices [nil (assoc i :class "live-added")]]) - invoices) + (fn [{:keys [db]} [_ invoices pdf-url]] + {:dispatch-n (cond->> [[::data-page/reset-checked :invoices]] + true (into (mapv + (fn [i] + [::data-page/updated-entity :invoices i]) + invoices)) + pdf-url (into [[::modal/modal-requested {:title "Your checks are ready!" :body [:div [:div "Click " [:a {:href pdf-url :target "_new"} "here"] " to print them."] @@ -158,15 +157,6 @@ #_#_:date (date->str (c/now) standard) :location (first (:locations @(re-frame/subscribe [::subs/client])))}]})) -(re-frame/reg-event-fx - ::expense-accounts-updated - (fn [{:keys [db]} [_ data]] - (let [updated (:edit-expense-accounts data)] - {:dispatch-n [[::events/modal-completed ::expense-accounts-dialog/change-expense-accounts] - [::data-page/entity-updated :invoices [nil updated]]] - :db (-> db - (dissoc ::change-expense-accounts))}))) - (defn pay-button [] (let [current-client @(re-frame/subscribe [::subs/client]) @@ -232,7 +222,7 @@ :check-boxes (= status :unpaid) :on-check-changed (fn [new] (re-frame/dispatch [::data-page/toggle-check :invoices new ])) - :expense-event [::expense-accounts-dialog/change-expense-accounts]}]])) + :actions #{:edit :void :expense-accounts}}]])) (defn unpaid-invoices-page [params] (r/create-class @@ -245,6 +235,4 @@ params @(re-frame/subscribe [::params])] [side-bar-layout {:side-bar [invoices-side-bar {}] :main [unpaid-invoices-content params] - :bottom [:div - [change-expense-accounts-modal {:updated-event [::expense-accounts-updated]}]] :right-side-bar [appearing-side-bar {:visible? invoice-bar-active?} [form/form {}]]}]))}))