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

View File

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

View File

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

View File

@@ -270,3 +270,10 @@
(defn local-now [] (defn local-now []
(t/to-default-time-zone (t/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]))))))