From 459e0b3e525b072f56a11ea77f25083fa4bc5b78 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Thu, 25 Apr 2019 16:20:45 -0700 Subject: [PATCH] updates the location of the expense account when one is selected. --- .../components/expense_accounts_field.cljs | 10 ++-- .../views/pages/transactions/form.cljs | 57 +++++++++++-------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs index 24ceefe6..21208309 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs @@ -12,11 +12,14 @@ (re-frame/reg-event-fx ::add-expense-account - (fn [_ [_ event expense-accounts]] + (fn [_ [_ event expense-accounts locations]] {:dispatch (conj event (conj expense-accounts {:amount 0 :id (str "new-" (random-uuid)) :amount-mode "%" - :amount-percentage 0}))})) + :amount-percentage 0 + :location (if (= 1 (count locations)) + (first locations) + nil)}))})) (re-frame/reg-event-fx ::remove-expense-account @@ -79,7 +82,7 @@ (when-not disabled [:p.buttons [:a.button {:on-click (dispatch-event [::spread-evenly event expense-accounts max-value])} "Spread evenly"] - [:a.button {:on-click (dispatch-event [::add-expense-account event expense-accounts])} "Add"]])]] + [:a.button {:on-click (dispatch-event [::add-expense-account event expense-accounts locations])} "Add"]])]] (for [[index {:keys [account id location amount amount-mode] :as expense-account}] (map vector (range) expense-accounts) :let [account (accounts-by-id (:id account))]] @@ -127,7 +130,6 @@ :event [::expense-account-changed event expense-accounts max-value] :subscription expense-accounts} (map (fn [l] ^{:key l} [:option {:value l} l]) locations)]]])]]]] - [:div.field [:p.help "Amount"] diff --git a/src/cljs/auto_ap/views/pages/transactions/form.cljs b/src/cljs/auto_ap/views/pages/transactions/form.cljs index d7b57c4c..673e6c08 100644 --- a/src/cljs/auto_ap/views/pages/transactions/form.cljs +++ b/src/cljs/auto_ap/views/pages/transactions/form.cljs @@ -44,31 +44,35 @@ (re-frame/reg-event-db ::editing (fn [db [_ which potential-payment-matches]] - (-> db - (forms/start-form ::edit-transaction {:id (:id which) - :yodlee-merchant (:yodlee-merchant which) - :amount (:amount which) - :potential-payment-matches potential-payment-matches - :description-original (:description-original which) - :location (:location which) - :exclude-from-ledger (:exclude-from-ledger which) - :payment (:payment 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}])})))) + (let [locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])] + (-> db + (forms/start-form ::edit-transaction {:id (:id which) + :yodlee-merchant (:yodlee-merchant which) + :amount (:amount which) + :potential-payment-matches potential-payment-matches + :description-original (:description-original which) + :location (:location which) + :exclude-from-ledger (:exclude-from-ledger which) + :payment (:payment 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 + :location (if (= 1 (count locations)) + (first locations) + nil)}])}))))) (re-frame/reg-event-fx @@ -118,6 +122,9 @@ :amount (Math/abs (:amount data)) :amount-percentage 100 :amount-mode "%" + :location (or + (:location @(re-frame/subscribe [::subs/vendor-default-account value])) + (get-in data [:accounts 0 :account :location])) :account @(re-frame/subscribe [::subs/vendor-default-account value])}]]} {:dispatch [::forms/change ::edit-transaction field value]}))))