cleaning up interface for account.

This commit is contained in:
Bryce Covert
2019-04-25 18:46:54 -07:00
parent 459e0b3e52
commit dd86b2be48
4 changed files with 64 additions and 80 deletions

View File

@@ -50,7 +50,7 @@
(re-frame/reg-event-db
::change
(fn [db [_ form & path-pairs]]
(println "CHANGING" form path-pairs)
(println "CHANGING" path-pairs)
(reduce
(fn [db [path value]]
(assoc-in db (into [::forms form :data] path) value))

View File

@@ -7,6 +7,41 @@
[re-frame.core :as re-frame]
[clojure.string :as str]))
(defn can-replace-with-default? [accounts]
(and (or (not (seq accounts))
(<= 1 (count accounts)))
(not (get-in accounts [0 :account :id]))))
(defn default-account [accounts default-account amount]
[{:id (str "new-" (random-uuid))
:amount (Math/abs amount)
:amount-percentage 100
:amount-mode "%"
:location (or
(:location default-account)
(get-in accounts [0 :account :location]))
:account default-account}])
(defn from-graphql [accounts total locations]
(println accounts total locations)
(if (seq accounts)
(vec (map
(fn [a]
(-> a
(update :amount js/parseFloat)
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
(Math/abs (js/parseFloat total)))))
(assoc :amount-mode "$")))
accounts))
[{:id (str "new-" (random-uuid))
:amount-mode "$"
:amount (Math/abs total)
:amount-percentage 100
:location (if (= 1 (count locations))
(first locations)
nil)}]))
;; EVENTS

View File

@@ -6,7 +6,7 @@
[auto-ap.subs :as subs]
[auto-ap.views.components.dropdown :refer [drop-down]]
[auto-ap.views.components.typeahead :refer [typeahead]]
[auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field recalculate-amounts]]
[auto-ap.views.components.expense-accounts-field :refer [expense-accounts-field recalculate-amounts] :as expense-accounts-field]
[auto-ap.views.pages.invoices.common :refer [invoice-read]]
[auto-ap.views.utils
:refer
@@ -101,19 +101,12 @@
submit-query)
;; EVENTS
(re-frame/reg-event-db
::adding
(fn [db [_ new]]
(-> db (forms/start-form ::form (assoc new :expense-accounts [{:amount 0
:id (str "new-" (random-uuid))
:amount-percentage 100
:amount-mode "%"}])))))
(re-frame/reg-event-db
::editing
(fn [db [_ which]]
(let [edit-invoice (update which :date #(date->str % standard))
edit-invoice (assoc edit-invoice :original edit-invoice)]
edit-invoice (assoc edit-invoice :original edit-invoice)
locations @(re-frame/subscribe [::subs/locations-for-client (:id (:client which))])]
(-> db
(forms/start-form ::form {:id (:id edit-invoice)
:status (:status edit-invoice)
@@ -124,19 +117,9 @@
:vendor-id (:id (:vendor edit-invoice))
:vendor-name (:name (:vendor edit-invoice))
:client-id (:id (:client edit-invoice))
:expense-accounts (if (seq (:expense-accounts which))
(vec (map
(fn [a]
(-> a
(update :amount #(js/parseFloat %))
(assoc :amount-percentage (* 100 (/ (js/parseFloat (:amount a))
(Math/abs (js/parseFloat (:total which))))))
(assoc :amount-mode "%")))
(:expense-accounts edit-invoice)))
[{:id (str "new-" (random-uuid))
:amount-mode "$"
:amount (Math/abs (:total edit-invoice))
:amount-percentage 100}])
:expense-accounts (expense-accounts-field/from-graphql (:expense-accounts which)
(:amount which)
locations)
:client-name (:name (:client edit-invoice))})))))
@@ -159,25 +142,18 @@
field value
[:expense-accounts] (recalculate-amounts (:expense-accounts data) value)]}))
(re-frame/reg-event-fx
::change-vendor
[(forms/in-form ::form)]
(fn [{{:keys [data]} :db} [_ location field value]]
(let [has-only-one-expense-account? (and value
(or (not (seq (:expense-accounts data)))
(<= 1 (count (:expense-accounts data))))
(not (get-in data [:expense-accounts 0 :account :id])))]
(if has-only-one-expense-account?
{:dispatch [::forms/change ::form
field value
[:expense-accounts] [{:id (str "new-" (random-uuid))
:amount (:total data)
:amount-percentage 100
:amount-mode "%"
:account @(re-frame/subscribe [::subs/vendor-default-account value])}]]}
{:dispatch [::forms/change ::form
field value]}))))
(fn [{{:keys [data]} :db} [_ field value]]
(if (and value (expense-accounts-field/can-replace-with-default? (:expense-accounts data)))
{:dispatch [::forms/change ::form
field value
[:expense-accounts] (expense-accounts-field/default-account (:expense-accounts data)
@(re-frame/subscribe [::subs/vendor-default-account value])
(:amount data))]}
{:dispatch [::forms/change ::form field value]})))
(re-frame/reg-event-fx
@@ -267,9 +243,8 @@
:disabled exists?
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false)
:field [:vendor-id]
:text-field [:vendor-name]
:text-event change-event
:event [::change-vendor [::form]]
:event [::change-vendor]
:spec (s/nilable ::invoice/vendor-id)
:subscription data}]]]]

View File

@@ -2,7 +2,7 @@
(:require [auto-ap.forms :as forms]
[auto-ap.subs :as subs]
[auto-ap.views.components.typeahead :refer [typeahead]]
[auto-ap.views.components.expense-accounts-field :refer [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.utils :refer [bind-field dispatch-event]]
[re-frame.core :as re-frame]
@@ -57,22 +57,9 @@
: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)}])})))))
:accounts (expense-accounts-field/from-graphql (:accounts which)
(:amount which)
locations)})))))
(re-frame/reg-event-fx
@@ -111,22 +98,14 @@
::change-vendor
[(forms/in-form ::edit-transaction)]
(fn [{{:keys [data]} :db} [_ field value]]
(let [has-only-one-expense-account? (and value
(or (not (seq (:accounts data)))
(<= 1 (count (:accounts data))))
(not (get-in data [:accounts 0 :account :id])))]
(if has-only-one-expense-account?
{:dispatch [::forms/change ::edit-transaction
field value
[:accounts] [{:id (str "new-" (random-uuid))
: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]}))))
(if (and value (expense-accounts-field/can-replace-with-default? (:accounts data)))
{:dispatch [::forms/change ::edit-transaction
field value
[:accounts] (expense-accounts-field/default-account (:accounts data)
@(re-frame/subscribe [::subs/vendor-default-account value])
(:amount data))]}
{:dispatch [::forms/change ::edit-transaction field value]})))
(re-frame/reg-event-db
::manual-match
@@ -149,11 +128,6 @@
(re-frame/dispatch-sync [::saving edit-completed]))}
[:h1.title.is-2 "Edit Transaction"]
(comment
[:div.notification
[:p "This transaction matches Invoice 'ABC' for 'DBI Beverages'. " [:a "Create payment and match"] "."]])
[:div.field
[:p.help "Merchant"]
[:div.control