More and more expressive.

This commit is contained in:
Bryce Covert
2019-04-26 13:40:36 -07:00
parent 3c25a85f52
commit 058d8b95bd

View File

@@ -7,7 +7,8 @@
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
[auto-ap.views.utils :refer [bind-field dispatch-event]]
[re-frame.core :as re-frame]
[clojure.string :as str]))
[clojure.string :as str]
[clojure.set :as set]))
;; SUBS
(re-frame/reg-sub
@@ -34,37 +35,36 @@
;; EVENTS
(re-frame/reg-event-fx
::edited
(fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction match-transaction]}]]
{:db (-> db
(forms/stop-form ::form))
:dispatch (conj edit-completed (or edit-transaction match-transaction))}))
(re-frame/reg-event-db
::editing
(fn [db [_ which potential-payment-matches]]
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])]
(-> db
(forms/start-form ::form {:id (:id which)
:yodlee-merchant (:yodlee-merchant which)
:amount (:amount which)
:potential-payment-matches potential-payment-matches
:description-original (:description-original which)
:location (:location which)
:exclude-from-ledger (:exclude-from-ledger which)
:payment (:payment which)
:client-id (:id (:client which))
:vendor (:vendor which)
:accounts (expense-accounts-field/from-graphql (:accounts which)
(:amount which)
locations)})))))
(forms/start-form db ::form
(-> which
(select-keys [:vendor :amount :payment :client :description-original
:yodlee-merchant :id :potential-payment-matches
:exclude-from-ledger :location :accounts])
(assoc :potential-payment-matches potential-payment-matches)
(update :accounts expense-accounts-field/from-graphql (:amount which) locations))))))
(re-frame/reg-event-fx
::change-vendor
[(forms/in-form ::form)]
(fn [{{:keys [data]} :db} [_ field value]]
(let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client 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-fx
::saving
(fn [{:keys [db]} [_ {:keys [edit-completed]}]]
(fn [{:keys [db]} [_ params]]
(when @(re-frame/subscribe [::can-submit])
(let [{{:keys [id vendor-id account-id location]} :data :as data} @(re-frame/subscribe [::forms/form ::form])]
{:db (forms/loading db ::form )
@@ -75,12 +75,13 @@
:venia/queries [{:query/data [:edit-transaction
@(re-frame/subscribe [::request])
transaction-read]}]}
:on-success [::edited edit-completed]
:on-success [::edited params]
:on-error [::forms/save-error ::form]}}))))
(re-frame/reg-event-fx
::matching
(fn [{:keys [db]} [_ edit-completed payment-id]]
(fn [{:keys [db]} [_ params payment-id]]
(let [{{:keys [id ]} :data :as data} @(re-frame/subscribe [::forms/form ::form])]
{:db (forms/loading db ::form )
:graphql
@@ -91,23 +92,14 @@
{:transaction_id id
:payment-id payment-id}
transaction-read]}]}
:on-success [::edited edit-completed]
:on-success [::edited params]
:on-error [::forms/save-error ::form]}})))
(re-frame/reg-event-fx
::change-vendor
[(forms/in-form ::form)]
(fn [{{:keys [data]} :db} [_ 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]}))))
::edited
(fn [{:keys [db]} [_ {:keys [edit-completed]} {:keys [edit-transaction match-transaction]}]]
{:db (-> db (forms/stop-form ::form))
:dispatch (conj edit-completed (or edit-transaction match-transaction))}))
(re-frame/reg-event-db
::manual-match
@@ -123,11 +115,30 @@
:id ::form}))
(defn potential-payment-matches-box [{:keys [potential-payment-matches] :as params}]
[:div.box
[:div.columns
[:div.column
[:h1.subtitle.is-5 "Potentially matching payments:"]]
[:div.column.is-narrow
[:a.delete {:on-click (dispatch-event [::manual-match])} ]]]
[:table.table.compact.is-borderless
(list
(for [{:keys [memo check-number vendor id]} potential-payment-matches]
[:tr
[:td.no-border (:name vendor)]
[: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 params id])}
"Match"]]]))]])
(defn form [{:keys [edit-completed]}]
[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)])
locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client data))])
{:keys [form field error-notification submit-button ]} my-form]
[form {:title "Hello" :edit-completed edit-completed}
[field "Merchant"
@@ -142,24 +153,11 @@
[:input.input {:type "text"
:field [:description-original]
:disabled "disabled"}]]
(if (and (seq (:potential-payment-matches data))
(not (:payment data)))
[:div.box
[:div.columns
[:div.column
[:h1.subtitle.is-5 "Potentially matching payments:"]]
[:div.column.is-narrow
[:a.delete {:on-click (dispatch-event [::manual-match])} ]]]
[:table.table.compact.is-borderless
(list
(for [{:keys [memo check-number vendor id]} (:potential-payment-matches data)]
[:tr
[:td.no-border (:name vendor)]
[: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"]]]))]]
[potential-payment-matches-box {:matches (:potential-payment-matches data)
:edit-completed edit-completed}]
[:div
[field "Vendor"