refactored transactions.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
(ns auto-ap.forms
|
(ns auto-ap.forms
|
||||||
(:require [re-frame.core :as re-frame]
|
(: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
|
(re-frame/reg-sub
|
||||||
@@ -65,7 +65,8 @@
|
|||||||
(assoc-in [::forms form :error] (or (:message (first result))
|
(assoc-in [::forms form :error] (or (:message (first result))
|
||||||
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]])
|
[:div [:a.delete.is-pulled-right {:on-click (dispatch-event [::form-closing form])}] [:div children]])
|
||||||
|
|
||||||
(defn loading [db id]
|
(defn loading [db id]
|
||||||
@@ -77,3 +78,35 @@
|
|||||||
(-> db
|
(-> db
|
||||||
(assoc-in [::forms id :status] nil)
|
(assoc-in [::forms id :status] nil)
|
||||||
(assoc-in [::forms id :error] 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]))})
|
||||||
|
|||||||
@@ -107,8 +107,10 @@
|
|||||||
|
|
||||||
(re-frame/reg-sub
|
(re-frame/reg-sub
|
||||||
::vendor-default-account
|
::vendor-default-account
|
||||||
(fn [db [_ z]]
|
(fn [db [_ v]]
|
||||||
(let [i (-> (:vendors db) (get z) :default-account :id)]
|
(let [i (if (:default-account v)
|
||||||
|
(-> v :default-account :id)
|
||||||
|
(-> (:vendors db) (get v) :default-account :id))]
|
||||||
(first (filter
|
(first (filter
|
||||||
#(= (:id %) i)
|
#(= (:id %) i)
|
||||||
(:accounts db))))))
|
(:accounts db))))))
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
(<= 1 (count accounts)))
|
(<= 1 (count accounts)))
|
||||||
(not (get-in accounts [0 :account :id]))))
|
(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]
|
[{:id (doto (get-in accounts [0 :id]
|
||||||
(str "new-" (random-uuid)))
|
(str "new-" (random-uuid)))
|
||||||
println)
|
println)
|
||||||
@@ -21,7 +21,10 @@
|
|||||||
:amount-mode "%"
|
:amount-mode "%"
|
||||||
:location (or
|
:location (or
|
||||||
(:location default-account)
|
(: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}])
|
:account default-account}])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -136,3 +136,7 @@
|
|||||||
bottom
|
bottom
|
||||||
[:div#dz-hidden]]))
|
[:div#dz-hidden]]))
|
||||||
|
|
||||||
|
|
||||||
|
(defn side-bar [{:keys [on-close]} children]
|
||||||
|
[:div [:a.delete.is-pulled-right {:on-click on-close}] [:div children]])
|
||||||
|
|
||||||
|
|||||||
@@ -153,7 +153,7 @@
|
|||||||
[:div.control
|
[:div.control
|
||||||
[:div.tags.has-addons
|
[:div.tags.has-addons
|
||||||
[:span.tag text]
|
[: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"
|
^{:key "typeahead"} [:input.input {:type "text"
|
||||||
:class class
|
:class class
|
||||||
|
|
||||||
|
|||||||
@@ -147,13 +147,15 @@
|
|||||||
::change-vendor
|
::change-vendor
|
||||||
[(forms/in-form ::form)]
|
[(forms/in-form ::form)]
|
||||||
(fn [{{:keys [data]} :db} [_ field value]]
|
(fn [{{:keys [data]} :db} [_ field value]]
|
||||||
(if (and value (expense-accounts-field/can-replace-with-default? (:expense-accounts data)))
|
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])]
|
||||||
{:dispatch [::forms/change ::form
|
(if (and value (expense-accounts-field/can-replace-with-default? (:expense-accounts data)))
|
||||||
field value
|
{:dispatch [::forms/change ::form
|
||||||
[:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data)
|
field value
|
||||||
@(re-frame/subscribe [::subs/vendor-default-account value])
|
[:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data)
|
||||||
(:amount data))]}
|
@(re-frame/subscribe [::subs/vendor-default-account value])
|
||||||
{:dispatch [::forms/change ::form field value]})))
|
(:amount data)
|
||||||
|
locations)]}
|
||||||
|
{:dispatch [::forms/change ::form field value]}))))
|
||||||
|
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
|
|||||||
@@ -122,7 +122,7 @@
|
|||||||
{:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) }))
|
{:component-will-mount #(re-frame/dispatch-sync [::params-change {}]) }))
|
||||||
|
|
||||||
(defn transactions-page []
|
(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-layout
|
||||||
{:side-bar [:div
|
{:side-bar [:div
|
||||||
[:p.menu-label "Bank Account"]
|
[:p.menu-label "Bank Account"]
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
:location
|
:location
|
||||||
:exclude-from-ledger
|
:exclude-from-ledger
|
||||||
[:vendor [:name :id]]
|
[:vendor [:name :id]]
|
||||||
[:accounts [:id :amount :location [:account [:name :id :location]]]]
|
[:accounts [:id :amount :location [:account [:name :id :location :numeric-code]]]]
|
||||||
:date
|
:date
|
||||||
[:yodlee_merchant [:name :yodlee-id]]
|
[:yodlee_merchant [:name :yodlee-id]]
|
||||||
:post_date
|
:post_date
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
(ns auto-ap.views.pages.transactions.form
|
(ns auto-ap.views.pages.transactions.form
|
||||||
(:require [auto-ap.forms :as forms]
|
(:require [auto-ap.forms :as forms]
|
||||||
[auto-ap.subs :as subs]
|
[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.components.expense-accounts-field :refer [expense-accounts-field] :as expense-accounts-field]
|
||||||
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
|
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
|
||||||
[auto-ap.views.utils :refer [bind-field dispatch-event]]
|
[auto-ap.views.utils :refer [bind-field dispatch-event]]
|
||||||
@@ -11,10 +12,10 @@
|
|||||||
;; SUBS
|
;; SUBS
|
||||||
(re-frame/reg-sub
|
(re-frame/reg-sub
|
||||||
::request
|
::request
|
||||||
:<- [::forms/form ::edit-transaction]
|
:<- [::forms/form ::form]
|
||||||
(fn [{{:keys [id vendor-id accounts exclude-from-ledger]} :data}]
|
(fn [{{:keys [id vendor accounts exclude-from-ledger]} :data}]
|
||||||
{:transaction {:id id
|
{:transaction {:id id
|
||||||
:vendor-id vendor-id
|
:vendor-id (:id vendor)
|
||||||
:exclude-from-ledger exclude-from-ledger
|
:exclude-from-ledger exclude-from-ledger
|
||||||
:accounts (map
|
:accounts (map
|
||||||
(fn [{:keys [id account amount location]}]
|
(fn [{:keys [id account amount location]}]
|
||||||
@@ -27,7 +28,7 @@
|
|||||||
|
|
||||||
(re-frame/reg-sub
|
(re-frame/reg-sub
|
||||||
::can-submit
|
::can-submit
|
||||||
:<- [::forms/form ::edit-transaction]
|
:<- [::forms/form ::form]
|
||||||
(fn [{:keys [data status]} _]
|
(fn [{:keys [data status]} _]
|
||||||
(not= :loading status)))
|
(not= :loading status)))
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@
|
|||||||
::edited
|
::edited
|
||||||
(fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction match-transaction]}]]
|
(fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction match-transaction]}]]
|
||||||
{:db (-> db
|
{:db (-> db
|
||||||
(forms/stop-form ::edit-transaction))
|
(forms/stop-form ::form))
|
||||||
|
|
||||||
:dispatch (conj edit-completed (or edit-transaction match-transaction))}))
|
:dispatch (conj edit-completed (or edit-transaction match-transaction))}))
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@
|
|||||||
(fn [db [_ which potential-payment-matches]]
|
(fn [db [_ which potential-payment-matches]]
|
||||||
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])]
|
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])]
|
||||||
(-> db
|
(-> db
|
||||||
(forms/start-form ::edit-transaction {:id (:id which)
|
(forms/start-form ::form {:id (:id which)
|
||||||
:yodlee-merchant (:yodlee-merchant which)
|
:yodlee-merchant (:yodlee-merchant which)
|
||||||
:amount (:amount which)
|
:amount (:amount which)
|
||||||
:potential-payment-matches potential-payment-matches
|
:potential-payment-matches potential-payment-matches
|
||||||
@@ -55,8 +56,7 @@
|
|||||||
:exclude-from-ledger (:exclude-from-ledger which)
|
:exclude-from-ledger (:exclude-from-ledger which)
|
||||||
:payment (:payment which)
|
:payment (:payment which)
|
||||||
:client-id (:id (:client which))
|
:client-id (:id (:client which))
|
||||||
:vendor-id (:id (:vendor which))
|
:vendor (:vendor which)
|
||||||
:vendor-name (:name (:vendor which))
|
|
||||||
:accounts (expense-accounts-field/from-graphql (:accounts which)
|
:accounts (expense-accounts-field/from-graphql (:accounts which)
|
||||||
(:amount which)
|
(:amount which)
|
||||||
locations)})))))
|
locations)})))))
|
||||||
@@ -64,11 +64,10 @@
|
|||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::saving
|
::saving
|
||||||
(fn [{:keys [db]} [_ edit-completed]]
|
(fn [{:keys [db]} [_ {:keys [edit-completed]}]]
|
||||||
(println "saving..." edit-completed)
|
|
||||||
(when @(re-frame/subscribe [::can-submit])
|
(when @(re-frame/subscribe [::can-submit])
|
||||||
(let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])]
|
(let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::form])]
|
||||||
{:db (forms/loading db ::edit-transaction )
|
{:db (forms/loading db ::form )
|
||||||
:graphql
|
:graphql
|
||||||
{:token (-> db :user)
|
{:token (-> db :user)
|
||||||
:query-obj {:venia/operation {:operation/type :mutation
|
:query-obj {:venia/operation {:operation/type :mutation
|
||||||
@@ -77,13 +76,13 @@
|
|||||||
@(re-frame/subscribe [::request])
|
@(re-frame/subscribe [::request])
|
||||||
transaction-read]}]}
|
transaction-read]}]}
|
||||||
:on-success [::edited edit-completed]
|
:on-success [::edited edit-completed]
|
||||||
:on-error [::forms/save-error ::edit-transaction]}}))))
|
:on-error [::forms/save-error ::form]}}))))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::matching
|
::matching
|
||||||
(fn [{:keys [db]} [_ edit-completed payment-id]]
|
(fn [{:keys [db]} [_ edit-completed payment-id]]
|
||||||
(let [{{:keys [id ]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])]
|
(let [{{:keys [id ]} :data :as data} @(re-frame/subscribe [::forms/form ::form])]
|
||||||
{:db (forms/loading db ::edit-transaction )
|
{:db (forms/loading db ::form )
|
||||||
:graphql
|
:graphql
|
||||||
{:token (-> db :user)
|
{:token (-> db :user)
|
||||||
:query-obj {:venia/operation {:operation/type :mutation
|
:query-obj {:venia/operation {:operation/type :mutation
|
||||||
@@ -93,81 +92,43 @@
|
|||||||
:payment-id payment-id}
|
:payment-id payment-id}
|
||||||
transaction-read]}]}
|
transaction-read]}]}
|
||||||
:on-success [::edited edit-completed]
|
:on-success [::edited edit-completed]
|
||||||
:on-error [::forms/save-error ::edit-transaction]}})))
|
:on-error [::forms/save-error ::form]}})))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
::change-vendor
|
::change-vendor
|
||||||
[(forms/in-form ::edit-transaction)]
|
[(forms/in-form ::form)]
|
||||||
(fn [{{:keys [data]} :db} [_ field value]]
|
(fn [{{:keys [data]} :db} [_ field value]]
|
||||||
(println "HIIII")
|
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])]
|
||||||
(if (and value (expense-accounts-field/can-replace-with-default? (:accounts data)))
|
(if (and value (expense-accounts-field/can-replace-with-default? (:accounts data)))
|
||||||
{:dispatch [::forms/change ::edit-transaction
|
{:dispatch [::forms/change ::form
|
||||||
field value
|
field value
|
||||||
[:accounts] (expense-accounts-field/default-account (:accounts data)
|
[:accounts] (expense-accounts-field/default-account (:accounts data)
|
||||||
@(re-frame/subscribe [::subs/vendor-default-account value])
|
@(re-frame/subscribe [::subs/vendor-default-account value])
|
||||||
(:amount data))]}
|
(:amount data)
|
||||||
{:dispatch [::forms/change ::edit-transaction field value]})))
|
locations)]}
|
||||||
|
{:dispatch [::forms/change ::form field value]}))))
|
||||||
|
|
||||||
|
|
||||||
(re-frame/reg-event-db
|
(re-frame/reg-event-db
|
||||||
::manual-match
|
::manual-match
|
||||||
[(forms/in-form ::edit-transaction)]
|
[(forms/in-form ::form)]
|
||||||
(fn [edit-transaction]
|
(fn [edit-transaction]
|
||||||
(update-in edit-transaction [:data] dissoc :potential-payment-matches)))
|
(update-in edit-transaction [:data] dissoc :potential-payment-matches)))
|
||||||
|
|
||||||
;; VIEWS
|
;; VIEWS
|
||||||
|
|
||||||
|
(def my-form (forms/vertical-form {:can-submit [::can-submit]
|
||||||
(defn vertical-form [{:keys [can-submit id change-event ]}]
|
:change-event [::forms/change ::form]
|
||||||
{:form (fn [{:keys [title] :as params} & children]
|
:submit-event [::saving ]
|
||||||
(let [{:keys [data active? error]} @(re-frame/subscribe [::forms/form id])]
|
:id ::form}))
|
||||||
|
|
||||||
(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}))
|
|
||||||
|
|
||||||
|
|
||||||
(defn form [{:keys [edit-completed]}]
|
(defn form [{:keys [edit-completed]}]
|
||||||
[forms/side-bar-form {:form ::edit-transaction }
|
[layouts/side-bar {:on-close (dispatch-event [::forms/form-closing ::form])}
|
||||||
(let [locations #{"BR" "DT"} #_@(re-frame/subscribe [::subs/locations-for-client (:client-id data)])
|
(let [change-event [::forms/change ::form]
|
||||||
change-event [::forms/change ::edit-transaction]
|
{:keys [data] } @(re-frame/subscribe [::forms/form ::form])
|
||||||
{:keys [data] } @(re-frame/subscribe [::forms/form ::edit-transaction])
|
locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])
|
||||||
{:keys [form field error-notification submit-button ]} my-form]
|
{:keys [form field error-notification submit-button ]} my-form]
|
||||||
(println "DATA" data)
|
|
||||||
[form {:title "Hello" :edit-completed edit-completed}
|
[form {:title "Hello" :edit-completed edit-completed}
|
||||||
[field "Merchant"
|
[field "Merchant"
|
||||||
[:input.input {:type "text"
|
[:input.input {:type "text"
|
||||||
@@ -198,16 +159,17 @@
|
|||||||
[:td.no-border (when check-number (str "Check " check-number " ")) memo]
|
[:td.no-border (when check-number (str "Check " check-number " ")) memo]
|
||||||
[:td.no-border
|
[:td.no-border
|
||||||
[:a.button.is-primary.is-small {:on-click (dispatch-event [::matching edit-completed id])}
|
[:a.button.is-primary.is-small {:on-click (dispatch-event [::matching edit-completed id])}
|
||||||
"Match"]]]))]
|
"Match"]]]))]]
|
||||||
]
|
|
||||||
[:div
|
[:div
|
||||||
[field "Vendor"
|
[field "Vendor"
|
||||||
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors]))
|
[typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors])
|
||||||
:type "typeahead"
|
:match->text :name
|
||||||
:auto-focus true
|
:type "typeahead-entity"
|
||||||
:field [:vendor-id]
|
:auto-focus true
|
||||||
:disabled (boolean (:payment data))
|
:field [:vendor]
|
||||||
:event [::change-vendor]}]]
|
:disabled (boolean (:payment data))
|
||||||
|
:event [::change-vendor]}]]
|
||||||
[field nil
|
[field nil
|
||||||
[expense-accounts-field
|
[expense-accounts-field
|
||||||
{:type "expense-accounts"
|
{:type "expense-accounts"
|
||||||
|
|||||||
Reference in New Issue
Block a user