From 598da0f3229c80f9d84d39a0ceb00019ac885523 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 26 Apr 2019 13:20:40 -0700 Subject: [PATCH] refactored transactions. --- src/cljs/auto_ap/forms.cljs | 37 ++++- src/cljs/auto_ap/subs.cljs | 6 +- .../components/expense_accounts_field.cljs | 7 +- .../auto_ap/views/components/layouts.cljs | 4 + .../auto_ap/views/components/typeahead.cljs | 2 +- .../auto_ap/views/pages/invoices/form.cljs | 16 ++- .../auto_ap/views/pages/transactions.cljs | 2 +- .../views/pages/transactions/common.cljs | 2 +- .../views/pages/transactions/form.cljs | 126 ++++++------------ 9 files changed, 104 insertions(+), 98 deletions(-) diff --git a/src/cljs/auto_ap/forms.cljs b/src/cljs/auto_ap/forms.cljs index 55351511..4d1d76a1 100644 --- a/src/cljs/auto_ap/forms.cljs +++ b/src/cljs/auto_ap/forms.cljs @@ -1,6 +1,6 @@ (ns auto-ap.forms (:require [re-frame.core :as re-frame] - [auto-ap.views.utils :refer [dispatch-event]])) + [auto-ap.views.utils :refer [dispatch-event bind-field]])) (re-frame/reg-sub @@ -65,7 +65,8 @@ (assoc-in [::forms form :error] (or (:message (first result)) result))))) -(defn side-bar-form [{:keys [form]} children] + +(defn ^:deprecated side-bar-form [{:keys [form]} children] [:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::form-closing form])}] [:div children]]) (defn loading [db id] @@ -77,3 +78,35 @@ (-> db (assoc-in [::forms id :status] nil) (assoc-in [::forms id :error] nil))) + + +(defn vertical-form [{:keys [can-submit id change-event submit-event ]}] + {:form (fn [{:keys [title] :as params} & children] + (let [{:keys [data active? error]} @(re-frame/subscribe [::form id])] + + (into ^{:key id} [:form { :on-submit (fn [e] + (when (.-stopPropagation e) + (.stopPropagation e) + (.preventDefault e)) + (re-frame/dispatch-sync (conj submit-event params)))} + [:h1.title.is-2 title] + + ] + children))) + :field (fn [label control] + (let [{:keys [data]} @(re-frame/subscribe [::form id])] + [:div.field + (when label [:p.help label]) + [:div.control [bind-field (assoc-in control [1 :subscription] data)]]])) + + :error-notification (fn [] + (when-let [error (:error @(re-frame/subscribe [::form id]))] + ^{:key error} + [:div.notification.is-warning.animated.fadeInUp error])) + :submit-button (fn [child] + (let [error (:error @(re-frame/subscribe [::form id]))] + [:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe can-submit) + "" + "disabled") + :class (str @(re-frame/subscribe [::loading-class id]) + (when error " animated shake"))} child]))}) diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index fe4e5b79..5827f19a 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -107,8 +107,10 @@ (re-frame/reg-sub ::vendor-default-account - (fn [db [_ z]] - (let [i (-> (:vendors db) (get z) :default-account :id)] + (fn [db [_ v]] + (let [i (if (:default-account v) + (-> v :default-account :id) + (-> (:vendors db) (get v) :default-account :id))] (first (filter #(= (:id %) i) (:accounts db)))))) diff --git a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs index 9b54eec0..26146354 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs @@ -12,7 +12,7 @@ (<= 1 (count accounts))) (not (get-in accounts [0 :account :id])))) -(defn default-account [accounts default-account amount] +(defn default-account [accounts default-account amount locations] [{:id (doto (get-in accounts [0 :id] (str "new-" (random-uuid))) println) @@ -21,7 +21,10 @@ :amount-mode "%" :location (or (:location default-account) - (get-in accounts [0 :account :location])) + (get-in accounts [0 :account :location]) + (if (= 1 (count locations)) + (first locations) + nil)) :account default-account}]) diff --git a/src/cljs/auto_ap/views/components/layouts.cljs b/src/cljs/auto_ap/views/components/layouts.cljs index 4801f7f4..4012d3ba 100644 --- a/src/cljs/auto_ap/views/components/layouts.cljs +++ b/src/cljs/auto_ap/views/components/layouts.cljs @@ -136,3 +136,7 @@ bottom [:div#dz-hidden]])) + +(defn side-bar [{:keys [on-close]} children] + [:div [:a.delete.is-pulled-right {:on-click on-close}] [:div children]]) + diff --git a/src/cljs/auto_ap/views/components/typeahead.cljs b/src/cljs/auto_ap/views/components/typeahead.cljs index d5bb29c4..9616c8b5 100644 --- a/src/cljs/auto_ap/views/components/typeahead.cljs +++ b/src/cljs/auto_ap/views/components/typeahead.cljs @@ -153,7 +153,7 @@ [:div.control [:div.tags.has-addons [:span.tag text] - [:a.tag.is-delete {:on-click (fn [] (select nil))}]]]] + [:a.tag.is-delete {:on-click (fn [] (select nil) (reset! text-atom nil))}]]]] ^{:key "typeahead"} [:input.input {:type "text" :class class diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index c0a02e58..dd9fa561 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -147,13 +147,15 @@ ::change-vendor [(forms/in-form ::form)] (fn [{{:keys [data]} :db} [_ field value]] - (if (and value (expense-accounts-field/can-replace-with-default? (:expense-accounts data))) - {:dispatch [::forms/change ::form - field value - [:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data) - @(re-frame/subscribe [::subs/vendor-default-account value]) - (:amount data))]} - {:dispatch [::forms/change ::form field value]}))) + (let [locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])] + (if (and value (expense-accounts-field/can-replace-with-default? (:expense-accounts data))) + {:dispatch [::forms/change ::form + field value + [:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data) + @(re-frame/subscribe [::subs/vendor-default-account value]) + (:amount data) + locations)]} + {:dispatch [::forms/change ::form field value]})))) (re-frame/reg-event-fx diff --git a/src/cljs/auto_ap/views/pages/transactions.cljs b/src/cljs/auto_ap/views/pages/transactions.cljs index b3ef94d7..d72eb19d 100644 --- a/src/cljs/auto_ap/views/pages/transactions.cljs +++ b/src/cljs/auto_ap/views/pages/transactions.cljs @@ -122,7 +122,7 @@ {:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) })) (defn transactions-page [] - (let [{transaction-bar-active? :active?} @(re-frame/subscribe [::forms/form ::edit/edit-transaction])] + (let [{transaction-bar-active? :active?} @(re-frame/subscribe [::forms/form ::edit/form])] [side-bar-layout {:side-bar [:div [:p.menu-label "Bank Account"] diff --git a/src/cljs/auto_ap/views/pages/transactions/common.cljs b/src/cljs/auto_ap/views/pages/transactions/common.cljs index 8f1b0344..c59428c1 100644 --- a/src/cljs/auto_ap/views/pages/transactions/common.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/common.cljs @@ -6,7 +6,7 @@ :location :exclude-from-ledger [:vendor [:name :id]] - [:accounts [:id :amount :location [:account [:name :id :location]]]] + [:accounts [:id :amount :location [:account [:name :id :location :numeric-code]]]] :date [:yodlee_merchant [:name :yodlee-id]] :post_date diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index ee603666..cd11629e 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -1,7 +1,8 @@ (ns auto-ap.views.pages.transactions.form (:require [auto-ap.forms :as forms] [auto-ap.subs :as subs] - [auto-ap.views.components.typeahead :refer [typeahead]] + [auto-ap.views.components.layouts :as layouts] + [auto-ap.views.components.typeahead :refer [typeahead typeahead-entity]] [auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field] :as expense-accounts-field] [auto-ap.views.pages.transactions.common :refer [transaction-read]] [auto-ap.views.utils :refer [bind-field dispatch-event]] @@ -11,10 +12,10 @@ ;; SUBS (re-frame/reg-sub ::request - :<- [::forms/form ::edit-transaction] - (fn [{{:keys [id vendor-id accounts exclude-from-ledger]} :data}] + :<- [::forms/form ::form] + (fn [{{:keys [id vendor accounts exclude-from-ledger]} :data}] {:transaction {:id id - :vendor-id vendor-id + :vendor-id (:id vendor) :exclude-from-ledger exclude-from-ledger :accounts (map (fn [{:keys [id account amount location]}] @@ -27,7 +28,7 @@ (re-frame/reg-sub ::can-submit - :<- [::forms/form ::edit-transaction] + :<- [::forms/form ::form] (fn [{:keys [data status]} _] (not= :loading status))) @@ -37,7 +38,7 @@ ::edited (fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction match-transaction]}]] {:db (-> db - (forms/stop-form ::edit-transaction)) + (forms/stop-form ::form)) :dispatch (conj edit-completed (or edit-transaction match-transaction))})) @@ -46,7 +47,7 @@ (fn [db [_ which potential-payment-matches]] (let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])] (-> db - (forms/start-form ::edit-transaction {:id (:id which) + (forms/start-form ::form {:id (:id which) :yodlee-merchant (:yodlee-merchant which) :amount (:amount which) :potential-payment-matches potential-payment-matches @@ -55,8 +56,7 @@ :exclude-from-ledger (:exclude-from-ledger which) :payment (:payment which) :client-id (:id (:client which)) - :vendor-id (:id (:vendor which)) - :vendor-name (:name (:vendor which)) + :vendor (:vendor which) :accounts (expense-accounts-field/from-graphql (:accounts which) (:amount which) locations)}))))) @@ -64,11 +64,10 @@ (re-frame/reg-event-fx ::saving - (fn [{:keys [db]} [_ edit-completed]] - (println "saving..." edit-completed) + (fn [{:keys [db]} [_ {:keys [edit-completed]}]] (when @(re-frame/subscribe [::can-submit]) - (let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])] - {:db (forms/loading db ::edit-transaction ) + (let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::form])] + {:db (forms/loading db ::form ) :graphql {:token (-> db :user) :query-obj {:venia/operation {:operation/type :mutation @@ -77,13 +76,13 @@ @(re-frame/subscribe [::request]) transaction-read]}]} :on-success [::edited edit-completed] - :on-error [::forms/save-error ::edit-transaction]}})))) + :on-error [::forms/save-error ::form]}})))) (re-frame/reg-event-fx ::matching (fn [{:keys [db]} [_ edit-completed payment-id]] - (let [{{:keys [id ]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])] - {:db (forms/loading db ::edit-transaction ) + (let [{{:keys [id ]} :data :as data} @(re-frame/subscribe [::forms/form ::form])] + {:db (forms/loading db ::form ) :graphql {:token (-> db :user) :query-obj {:venia/operation {:operation/type :mutation @@ -93,81 +92,43 @@ :payment-id payment-id} transaction-read]}]} :on-success [::edited edit-completed] - :on-error [::forms/save-error ::edit-transaction]}}))) + :on-error [::forms/save-error ::form]}}))) (re-frame/reg-event-fx ::change-vendor - [(forms/in-form ::edit-transaction)] + [(forms/in-form ::form)] (fn [{{:keys [data]} :db} [_ field value]] - (println "HIIII") - (if (and value (expense-accounts-field/can-replace-with-default? (:accounts data))) - {:dispatch [::forms/change ::edit-transaction - field value - [:accounts] (expense-accounts-field/default-account (:accounts data) - @(re-frame/subscribe [::subs/vendor-default-account value]) - (:amount data))]} - {:dispatch [::forms/change ::edit-transaction field value]}))) + (let [locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])] + (if (and value (expense-accounts-field/can-replace-with-default? (:accounts data))) + {:dispatch [::forms/change ::form + field value + [:accounts] (expense-accounts-field/default-account (:accounts data) + @(re-frame/subscribe [::subs/vendor-default-account value]) + (:amount data) + locations)]} + {:dispatch [::forms/change ::form field value]})))) (re-frame/reg-event-db ::manual-match - [(forms/in-form ::edit-transaction)] + [(forms/in-form ::form)] (fn [edit-transaction] (update-in edit-transaction [:data] dissoc :potential-payment-matches))) ;; VIEWS - -(defn vertical-form [{:keys [can-submit id change-event ]}] - {:form (fn [{:keys [title] :as params} & children] - (let [{:keys [data active? error]} @(re-frame/subscribe [::forms/form id])] - - (into ^{:key id} [:form { :on-submit (fn [e] - (when (.-stopPropagation e) - (.stopPropagation e) - (.preventDefault e)) - (re-frame/dispatch-sync [::saving (:edit-completed params)]))} - [:h1.title.is-2 title] - - ] - children))) - :field (fn [label control] - (let [{:keys [data]} @(re-frame/subscribe [::forms/form id])] - [:div.field - (when label [:p.help label]) - [:div.control [bind-field (assoc-in control [1 :subscription] data)]]])) - - :error-notification (fn [] - (when-let [error (:error @(re-frame/subscribe [::forms/form id]))] - ^{:key error} - [:div.notification.is-warning.animated.fadeInUp error])) - :submit-button (fn [child] - (let [error (:error @(re-frame/subscribe [::forms/form id]))] - [:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit]) - "" - "disabled") - :class (str @(re-frame/subscribe [::forms/loading-class id]) - (when error " animated shake"))} child]))}) - -(re-frame/reg-sub - ::error - :<- [::forms/form ::edit-transaction] - (fn [{:keys [error]}] - error)) - - -(def my-form (vertical-form {:can-submit [::can-submit] - :change-event [::forms/change ::edit-transaction] - :id ::edit-transaction})) +(def my-form (forms/vertical-form {:can-submit [::can-submit] + :change-event [::forms/change ::form] + :submit-event [::saving ] + :id ::form})) (defn form [{:keys [edit-completed]}] - [forms/side-bar-form {:form ::edit-transaction } - (let [locations #{"BR" "DT"} #_@(re-frame/subscribe [::subs/locations-for-client (:client-id data)]) - change-event [::forms/change ::edit-transaction] - {:keys [data] } @(re-frame/subscribe [::forms/form ::edit-transaction]) + [layouts/side-bar {:on-close (dispatch-event [::forms/form-closing ::form])} + (let [change-event [::forms/change ::form] + {:keys [data] } @(re-frame/subscribe [::forms/form ::form]) + locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)]) {:keys [form field error-notification submit-button ]} my-form] - (println "DATA" data) [form {:title "Hello" :edit-completed edit-completed} [field "Merchant" [:input.input {:type "text" @@ -198,16 +159,17 @@ [:td.no-border (when check-number (str "Check " check-number " ")) memo] [:td.no-border [:a.button.is-primary.is-small {:on-click (dispatch-event [::matching edit-completed id])} - "Match"]]]))] - ] + "Match"]]]))]] + [:div [field "Vendor" - [typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors])) - :type "typeahead" - :auto-focus true - :field [:vendor-id] - :disabled (boolean (:payment data)) - :event [::change-vendor]}]] + [typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors]) + :match->text :name + :type "typeahead-entity" + :auto-focus true + :field [:vendor] + :disabled (boolean (:payment data)) + :event [::change-vendor]}]] [field nil [expense-accounts-field {:type "expense-accounts"