continuing to refactor form.

This commit is contained in:
Bryce Covert
2019-04-26 14:14:13 -07:00
parent 058d8b95bd
commit 5d5db63ed6
4 changed files with 84 additions and 66 deletions

View File

@@ -47,6 +47,8 @@
[form-name]
(re-frame/path [::forms form-name]))
(re-frame/reg-event-db
::change
(fn [db [_ form & path-pairs]]
@@ -74,6 +76,16 @@
(assoc-in [::forms id :status] :loading)
(assoc-in [::forms id :error] nil)))
(defn triggers-loading [form]
(re-frame/enrich
(fn [db event]
(loading db form))))
(defn triggers-stop [form]
(re-frame/enrich
(fn [db event]
(stop-form db form))))
(defn save-succeeded [db id]
(-> db
(assoc-in [::forms id :status] nil)
@@ -82,13 +94,15 @@
(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])]
(let [{:keys [data active? error]} @(re-frame/subscribe [::form id])
can-submit @(re-frame/subscribe can-submit)]
(into ^{:key id} [:form { :on-submit (fn [e]
(when (.-stopPropagation e)
(.stopPropagation e)
(.preventDefault e))
(re-frame/dispatch-sync (conj submit-event params)))}
(when (.-stopPropagation e)
(.stopPropagation e)
(.preventDefault e))
(when can-submit
(re-frame/dispatch-sync (conj submit-event params))))}
[:h1.title.is-2 title]
]

View File

@@ -13,9 +13,8 @@
(not (get-in accounts [0 :account :id]))))
(defn default-account [accounts default-account amount locations]
[{:id (doto (get-in accounts [0 :id]
(str "new-" (random-uuid)))
println)
[{:id (get-in accounts [0 :id]
(str "new-" (random-uuid)))
:amount (Math/abs amount)
:amount-percentage 100
:amount-mode "%"
@@ -137,7 +136,7 @@
[:div.column.is-narrow
(when-not disabled
[:a.delete {:on-click (dispatch-event [::remove-expense-account event expense-accounts id])}])]]
(println "TYPAHEAD" expense-accounts)
[:div.field
[:div.columns
[:div.column

View File

@@ -5,27 +5,32 @@
[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]]
[auto-ap.views.utils :refer [bind-field dispatch-event with-user]]
[re-frame.core :as re-frame]
[clojure.string :as str]
[clojure.set :as set]))
;; SUBS
(re-frame/reg-sub
::request
::submit-query
:<- [::forms/form ::form]
(fn [{{:keys [id vendor accounts exclude-from-ledger]} :data}]
{:transaction {:id id
:vendor-id (:id vendor)
: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)}}))
{:venia/operation {:operation/type :mutation
:operation/name "EditTransaction"}
:venia/queries [{:query/data
[:edit-transaction
{:transaction {:id id
:vendor-id (:id vendor)
: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)}}
transaction-read]}]}))
(re-frame/reg-sub
::can-submit
@@ -64,42 +69,35 @@
(re-frame/reg-event-fx
::saving
(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 )
: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 params]
:on-error [::forms/save-error ::form]}}))))
[with-user (forms/triggers-loading ::form) (forms/in-form ::form)]
(fn [{:keys [user]} [_ params]]
{:graphql
{:token user
:query-obj @(re-frame/subscribe [::submit-query])
:on-success [::edited params]
:on-error [::forms/save-error ::form]}}))
(re-frame/reg-event-fx
::matching
(fn [{:keys [db]} [_ params payment-id]]
(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
:operation/name "MatchTransaction"}
:venia/queries [{:query/data [:match-transaction
{:transaction_id id
:payment-id payment-id}
transaction-read]}]}
:on-success [::edited params]
:on-error [::forms/save-error ::form]}})))
[with-user (forms/triggers-loading ::form) (forms/in-form ::form)]
(fn [{{{:keys [id]} :data} :db user :user} [_ params payment-id]]
{:graphql
{:token user
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "MatchTransaction"}
:venia/queries [{:query/data [:match-transaction
{:transaction_id id
:payment-id payment-id}
transaction-read]}]}
:on-success [::edited params]
:on-error [::forms/save-error ::form]}}))
(re-frame/reg-event-fx
::edited
[(forms/triggers-stop ::form)]
(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))}))
{:dispatch (conj edit-completed (or edit-transaction match-transaction))}))
(re-frame/reg-event-db
::manual-match
@@ -143,16 +141,16 @@
[form {:title "Hello" :edit-completed edit-completed}
[field "Merchant"
[:input.input {:type "text"
:field [:yodlee-merchant :name]
:disabled "disabled"}]]
:field [:yodlee-merchant :name]
:disabled "disabled"}]]
[field "Amount"
[:input.input {:type "text"
:field [:amount]
:disabled "disabled"}]]
[:input.input {:type "text"
:field [:amount]
:disabled "disabled"}]]
[field "Description"
[:input.input {:type "text"
:field [:description-original]
:disabled "disabled"}]]
[:input.input {:type "text"
:field [:description-original]
:disabled "disabled"}]]
(if (and (seq (:potential-payment-matches data))
(not (:payment data)))
@@ -169,13 +167,13 @@
:disabled (boolean (:payment data))
:event [::change-vendor]}]]
[field nil
[expense-accounts-field
{:type "expense-accounts"
:field [:accounts]
:max (Math/abs (js/parseFloat (:amount data)))
:descriptor "credit account"
:disabled (boolean (:payment data))
:locations locations
:event change-event}]]
[expense-accounts-field
{:type "expense-accounts"
:field [:accounts]
:max (Math/abs (js/parseFloat (:amount data)))
:descriptor "credit account"
:disabled (boolean (:payment data))
:locations locations
:event change-event}]]
[error-notification]
[submit-button "Save"]])])])

View File

@@ -270,3 +270,10 @@
(defn local-now []
(t/to-default-time-zone (t/now)))
(def with-user
(re-frame/->interceptor
:id :with-user
:before (fn [context]
(-> context
(assoc-in [:coeffects :user] (get-in context [:coeffects :db :user]))))))