supports validation and multiple account entering.
This commit is contained in:
@@ -29,10 +29,14 @@
|
||||
(re-frame/reg-event-fx
|
||||
::expense-account-changed
|
||||
(fn [_ [_ event expense-accounts field value]]
|
||||
{:dispatch (into event [(assoc-in expense-accounts field value)
|
||||
(if (= (list :account :id) (drop 1 field))
|
||||
(if-let [location (:location @(re-frame/subscribe [::subs/account value]))]
|
||||
[[(first field) :location] location]))])}))
|
||||
(let [updated-accounts (cond-> expense-accounts
|
||||
true (assoc-in field value)
|
||||
(= (list :account :id) (drop 1 field)) (assoc-in [(first field) :account] @(re-frame/subscribe [::subs/account value]))
|
||||
)
|
||||
updated-accounts (if-let [location (get-in updated-accounts [(first field) :account :location])]
|
||||
(assoc-in updated-accounts [(first field) :location] location)
|
||||
updated-accounts)]
|
||||
{:dispatch (into event [updated-accounts])})))
|
||||
|
||||
|
||||
;; VIEWS
|
||||
|
||||
@@ -17,17 +17,17 @@
|
||||
(let [text (r/atom (or (second (first (filter #(= (first %) value) matches))) ""))
|
||||
highlighted (r/atom nil)
|
||||
selected (r/atom (first (first (filter #(= (first %) value) matches))))
|
||||
select (fn [[id text-description text-value]]
|
||||
(reset! selected id)
|
||||
(reset! text text-description)
|
||||
(when on-change
|
||||
(if (= :not-found id)
|
||||
(on-change nil text-description text-value)
|
||||
(on-change id text-description (or text-value text-description)))))]
|
||||
]
|
||||
(r/create-class
|
||||
{:reagent-render (fn [{:keys [matches on-change disabled field text-field value class not-found-description]}]
|
||||
|
||||
(let [text @text
|
||||
(let [ select (fn [[id text-description text-value]]
|
||||
(reset! selected id)
|
||||
(reset! text text-description)
|
||||
(when on-change
|
||||
(if (= :not-found id)
|
||||
(on-change nil text-description text-value)
|
||||
(on-change id text-description (or text-value text-description)))))
|
||||
text @text
|
||||
valid-matches (get-valid-matches matches not-found-description not-found-value text)]
|
||||
[:div.typeahead
|
||||
(if disabled
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
:amount
|
||||
:location
|
||||
[:vendor [:name :id]]
|
||||
[:account [:id :name]]
|
||||
[:accounts [:id :amount :location [:account [:name :id :location]]]]
|
||||
:date
|
||||
[:yodlee_merchant [:name :yodlee-id]]
|
||||
:post_date
|
||||
|
||||
@@ -5,32 +5,24 @@
|
||||
[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]))
|
||||
[re-frame.core :as re-frame]
|
||||
[clojure.string :as str]))
|
||||
|
||||
(re-frame/reg-event-db
|
||||
::editing
|
||||
(fn [db [_ which]]
|
||||
(-> db
|
||||
(forms/start-form ::edit-transaction {:id (:id which)
|
||||
:yodlee-merchant (:yodlee-merchant which)
|
||||
:description-original (:description-original which)
|
||||
:location (:location which)
|
||||
:client-id (:id (:client which))
|
||||
:account-id (:id (:account which))
|
||||
:account-name (:name (:account which))
|
||||
:vendor-id (:id (:vendor which))
|
||||
:vendor-name (:name (:vendor which))
|
||||
:expense-accounts (or (:expense-accounts which)
|
||||
[{:id (str "new-" (random-uuid))
|
||||
:amount (Math/abs (:amount which))}])}))))
|
||||
;; SUBS
|
||||
(re-frame/reg-sub
|
||||
::request
|
||||
:<- [::forms/form ::edit-transaction]
|
||||
(fn [{{:keys [id vendor-id account-id location]} :data}]
|
||||
(fn [{{:keys [id vendor-id accounts]} :data}]
|
||||
{:transaction {:id id
|
||||
:location location
|
||||
:vendor-id vendor-id
|
||||
:account-id account-id}}))
|
||||
: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
|
||||
@@ -39,12 +31,30 @@
|
||||
(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)
|
||||
:description-original (:description-original which)
|
||||
:location (:location which)
|
||||
:client-id (:id (:client which))
|
||||
:vendor-id (:id (:vendor which))
|
||||
:vendor-name (:name (:vendor which))
|
||||
:accounts (or (vec (:accounts which))
|
||||
[{:id (str "new-" (random-uuid))
|
||||
:amount (Math/abs (:amount which))}])}))))
|
||||
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::saving
|
||||
@@ -72,6 +82,8 @@
|
||||
{: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])
|
||||
@@ -121,15 +133,15 @@
|
||||
:event change-event
|
||||
:subscription data}]]]]
|
||||
|
||||
[:div.field
|
||||
[bind-field
|
||||
[expense-accounts-field
|
||||
{:type "expense-accounts"
|
||||
:field [:expense-accounts]
|
||||
:descriptor "credit account"
|
||||
:locations locations
|
||||
:event change-event
|
||||
:subscription data}]]]
|
||||
[:div.field]
|
||||
[bind-field
|
||||
[expense-accounts-field
|
||||
{:type "expense-accounts"
|
||||
:field [:accounts]
|
||||
:descriptor "credit account"
|
||||
:locations locations
|
||||
:event change-event
|
||||
:subscription data}]]
|
||||
|
||||
(comment
|
||||
[:div.field
|
||||
|
||||
@@ -150,6 +150,7 @@
|
||||
(when (and spec (not (s/valid? spec (get-in subscription field))))
|
||||
" is-danger")))
|
||||
keys (dissoc keys :field :subscription :spec)]
|
||||
|
||||
(into [dom keys] (with-keys rest))))
|
||||
|
||||
(defmethod do-bind :default [dom {:keys [field event subscription class spec] :as keys} & rest]
|
||||
|
||||
Reference in New Issue
Block a user