Files
integreat/src/cljs/auto_ap/views/pages/transactions/form.cljs
Bryce Covert 793a5c41ae cleanup.
2019-04-23 23:40:32 -07:00

199 lines
8.3 KiB
Clojure

(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.expense-accounts-field :refer [expense-accounts-field]]
[auto-ap.views.pages.transactions.common :refer [transaction-read]]
[auto-ap.views.utils :refer [bind-field]]
[re-frame.core :as re-frame]
[clojure.string :as str]))
;; SUBS
(re-frame/reg-sub
::request
:<- [::forms/form ::edit-transaction]
(fn [{{:keys [id vendor-id accounts exclude-from-ledger]} :data}]
{:transaction {:id id
:vendor-id vendor-id
:exclude-from-ledger exclude-from-ledger
:accounts (map
(fn [{:keys [id account amount location]}]
{:id (when-not (str/starts-with? id "new-")
id)
:account-id (:id account)
:location location
:amount amount})
accounts)}}))
(re-frame/reg-sub
::can-submit
:<- [::forms/form ::edit-transaction]
(fn [{:keys [data status]} _]
(not= :loading status)))
;; EVENTS
(re-frame/reg-event-fx
::edited
(fn [{:keys [db]} [_ edit-completed {:keys [edit-transaction]}]]
{:db (-> db
(forms/stop-form ::edit-transaction))
:dispatch (conj edit-completed edit-transaction)}))
(re-frame/reg-event-db
::editing
(fn [db [_ which]]
(-> db
(forms/start-form ::edit-transaction {:id (:id which)
:yodlee-merchant (:yodlee-merchant which)
:amount (:amount which)
:description-original (:description-original which)
:location (:location which)
:exclude-from-ledger (:exclude-from-ledger which)
:client-id (:id (:client which))
:vendor-id (:id (:vendor which))
:vendor-name (:name (:vendor which))
:accounts (if (seq (:accounts which))
(vec (map
(fn [a]
(-> a
(update :amount js/parseFloat)
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
(Math/abs (js/parseFloat (:amount which))))))
(assoc :amount-mode "$")))
(:accounts which)))
[{:id (str "new-" (random-uuid))
:amount-mode "$"
:amount (Math/abs (:amount which))
:amount-percentage 100}])}))))
(re-frame/reg-event-fx
::saving
(fn [{:keys [db]} [_ 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 )
:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "EditTransaction"}
:venia/queries [{:query/data [:edit-transaction
@(re-frame/subscribe [::request])
transaction-read]}]}
:on-success [::edited edit-completed]
:on-error [::forms/save-error ::edit-transaction]}}))))
(re-frame/reg-event-fx
::change-account
(fn [{:keys [db]} [_ f a]]
(if-let [forced-location (and (= f [:account-id])
(:location @(re-frame/subscribe [::subs/account a])))]
{:dispatch-n [[::forms/change ::edit-transaction f a]
[::forms/change ::edit-transaction [:location] forced-location]]}
{:dispatch [::forms/change ::edit-transaction f a]})))
;; VIEWS
(defn form [{:keys [edit-completed]}]
[forms/side-bar-form {:form ::edit-transaction }
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::edit-transaction])
locations @(re-frame/subscribe [::subs/locations-for-client (:client-id data)])
change-event [::forms/change ::edit-transaction]]
^{:key id}
[:form { :on-submit (fn [e]
(when (.-stopPropagation e)
(.stopPropagation e)
(.preventDefault e))
(re-frame/dispatch-sync [::saving edit-completed]))}
[:h1.title.is-2 "Edit Transaction"]
(comment
[:div.notification
[:p "This transaction matches Invoice 'ABC' for 'DBI Beverages'. " [:a "Create payment and match"] "."]])
[:div.field
[:p.help "Merchant"]
[:div.control
[bind-field
[:input.input {:type "text"
:field [:yodlee-merchant :name]
:disabled "disabled"
:subscription data}]]]]
[:div.field
[:p.help "Amount"]
[:div.control
[bind-field
[:input.input {:type "text"
:field [:amount]
:disabled "disabled"
:subscription data}]]]]
[:div.field
[:p.help "Description"]
[:div.control
[bind-field
[:input.input {:type "text"
:field [:description-original]
:disabled "disabled"
:subscription data}]]]]
[:div.field
[:p.help "Vendor"]
[:div.control
[bind-field
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors]))
:type "typeahead"
:auto-focus true
:field [:vendor-id]
:text-field [:vendor-name]
:event change-event
:subscription data}]]]]
[:div.field
[bind-field
[expense-accounts-field
{:type "expense-accounts"
:field [:accounts]
:max (Math/abs (js/parseFloat (:amount data)))
:descriptor "credit account"
:locations locations
:event change-event
:subscription data}]]]
[:div.field
[:div.control
[:label.checkbox
[bind-field
[:input.checkbox {:type "checkbox"
:field [:exclude-from-ledger]
:subscription data
:event change-event}
]]
" Exclude from ledger?"
]]]
(comment
[:div.field
[:p.control
[:label.checkbox
[:input.checkbox {:type "checkbox"
:field [:always-map]
:subscription data}]
" Always match Merchant '" (:merchant-name data) "' to '" (:vendor-name data) "'" ]]])
(when error
^{:key error} [:div.notification.is-warning.animated.fadeInUp
error])
[:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit])
""
"disabled")
:class (str @(re-frame/subscribe [::forms/loading-class ::edit-transaction])
(when error " animated shake"))} "Save"]])])