Refactored editing.

This commit is contained in:
Bryce Covert
2019-04-11 07:33:15 -07:00
parent f752bfa632
commit 704481466b
2 changed files with 22 additions and 29 deletions

View File

@@ -1,22 +1,16 @@
(ns auto-ap.views.pages.transactions
(:require [re-frame.core :as re-frame]
[auto-ap.entities.clients :as client]
[auto-ap.entities.vendors :as vendor]
[reagent.core :as reagent]
[goog.string :as gstring]
(:require [auto-ap.events :as events]
[auto-ap.forms :as forms]
[auto-ap.views.components.sorter :refer [sorted-column]]
[auto-ap.views.components.typeahead :refer [typeahead]]
[auto-ap.subs :as subs]
[auto-ap.views.components.bank-account-filter :refer [bank-account-filter]]
[auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]]
[auto-ap.views.components.modal :refer [action-modal]]
[auto-ap.views.components.paginator :refer [paginator]]
[auto-ap.views.components.layouts :refer [side-bar-layout appearing-side-bar]]
[auto-ap.views.components.bank-account-filter :refer [bank-account-filter]]
[auto-ap.views.pages.transactions.edit :as edit]
[auto-ap.events :as events]
[auto-ap.views.utils :refer [dispatch-event date->str bind-field nf]]
[auto-ap.utils :refer [by]]
[auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table]
[auto-ap.subs :as subs]))
[auto-ap.views.components.sorter :refer [sorted-column]]
[auto-ap.views.pages.transactions.form :as edit]
[auto-ap.views.utils :refer [bind-field date->str dispatch-event nf]]
[goog.string :as gstring]
[re-frame.core :as re-frame]))
(def transaction-read
[:id
@@ -176,7 +170,7 @@
[:td status]
[:td (:name bank-account )]
[:td
[:a.button {:on-click (dispatch-event [::edit/transaction-editing i])} [:span [:span.icon [:i.fa.fa-pencil]]]]
[:a.button {:on-click (dispatch-event [::edit/editing i])} [:span [:span.icon [:i.fa.fa-pencil]]]]
(when check
[:a.tag {:href (:s3-url check) :target "_new"} [:i.fa.fa-money-check] [:span.icon [:i.fa.fa-money]] (str " " (:check-number check) " (" (gstring/format "$%.2f" amount ) ")")])]
]))]]]))))
@@ -284,5 +278,5 @@
:value (:bank-acount-filter @(re-frame/subscribe [::transaction-page]))
:bank-accounts @(re-frame/subscribe [::subs/bank-accounts])}]]]
:main [transactions-content]
:right-side-bar [appearing-side-bar {:visible? transaction-bar-active?} [edit/edit-transaction-form {:edit-completed [::edit-completed]}]]}]))
:right-side-bar [appearing-side-bar {:visible? transaction-bar-active?} [edit/form {:edit-completed [::edit-completed]}]]}]))

View File

@@ -1,4 +1,4 @@
(ns auto-ap.views.pages.transactions.edit
(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]]
@@ -6,7 +6,7 @@
[re-frame.core :as re-frame]))
(re-frame/reg-event-db
::transaction-editing
::editing
(fn [db [_ which]]
(-> db
(forms/start-form ::edit-transaction {:id (:id which)
@@ -14,23 +14,23 @@
:vendor-name (:name (:vendor which))}))))
(re-frame/reg-sub
::can-submit-edit-transaction
::can-submit
:<- [::forms/form ::edit-transaction]
(fn [{:keys [data status]} _]
(not= :loading status)))
(re-frame/reg-event-fx
::transaction-edited
::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-fx
::edit-transaction-saving
::saving
(fn [{:keys [db]} [_ edit-completed]]
(when @(re-frame/subscribe [::can-submit-edit-transaction])
(when @(re-frame/subscribe [::can-submit])
(let [{{:keys [id vendor-id]} :data :as data} @(re-frame/subscribe [::forms/form ::edit-transaction])]
{:db (forms/loading db ::edit-transaction )
@@ -52,11 +52,11 @@
[:payment [:check_number :s3_url]]
[:client [:name :id]]
[:bank-account [:name :yodlee-account-id]]]]}]}
:on-success [::transaction-edited edit-completed]
:on-success [::edited edit-completed]
:on-error [::forms/save-error ::edit-transaction]}}))))
(defn edit-transaction-form [{:keys [edit-completed]}]
(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])
data (assoc data :merchant-name "Hello") ;; TODO - just until merchant is added
@@ -67,7 +67,7 @@
(when (.-stopPropagation e)
(.stopPropagation e)
(.preventDefault e))
(re-frame/dispatch-sync [::edit-transaction-saving edit-completed]))}
(re-frame/dispatch-sync [::saving edit-completed]))}
[:h1.title.is-2 "Edit Transaction"]
[:div.notification
@@ -119,10 +119,9 @@
^{:key error} [:div.notification.is-warning.animated.fadeInUp
error])
[:button.button.is-medium.is-primary.is-fullwidth {:disabled (if @(re-frame/subscribe [::can-submit-edit-transaction])
[: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"]
])]
)
])])