simple change to get accounts auto-populating.

This commit is contained in:
Bryce Covert
2019-04-17 11:13:19 -07:00
parent c51d235a36
commit d30b0948d4
4 changed files with 39 additions and 17 deletions

View File

@@ -100,12 +100,24 @@
(fn [db]
(vals (:vendors db))))
(re-frame/reg-sub
::vendors-by-id
(fn [db]
(:vendors db)))
(re-frame/reg-sub
::vendor-default-account
(fn [db [_ z]]
(let [i (-> (:vendors db) (get z) :default-account :id)]
(first (filter
#(= (:id %) i)
(:accounts db))))))
(re-frame/reg-sub
::sorted-vendors
:<- [::vendors]
(fn [vendors]
(sort-by :name vendors)))
(re-frame/reg-sub
::admin
(fn [db]

View File

@@ -68,6 +68,7 @@
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) chooseable-expense-accounts)
:type "typeahead"
:field [index :account :id]
:text-field [index :account :name]
:event [::expense-account-changed event expense-accounts]
:subscription expense-accounts}]]]]
[:div.column.is-narrow

View File

@@ -135,6 +135,26 @@
[:client-id] value
[:location] first-location]})))
(re-frame/reg-event-fx
::change-vendor
[(forms/in-form ::form)]
(fn [{{:keys [data]} :db} [_ location field value]]
(println "data" data)
(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])))]
(println "has only one?" has-only-one-expense-account?)
(if has-only-one-expense-account?
{:dispatch [::forms/change ::form
field value
[:expense-accounts] [{:id (str "new-" (random-uuid))
:amount (:total data)
:account @(re-frame/subscribe [::subs/vendor-default-account value])}]]}
{:dispatch [::forms/change ::form
field value]}))))
(re-frame/reg-event-fx
@@ -214,19 +234,7 @@
:event [::change-new-invoice-client [::form]]
:spec ::invoice/client-id
:subscription data}]]]])
(when (and should-select-location? (not exists?))
[:div.field
[:p.help "Location"]
[:div.control
[:div.select
[bind-field
[:select {:type "select"
:field [:location]
:spec (set locations)
:event change-event
:subscription data}
(map (fn [l] [:option {:value l} l]) locations)]]]]])
[:div.field
[:p.help "Vendor"]
[:div.control
@@ -237,7 +245,8 @@
:auto-focus (if @(re-frame/subscribe [::subs/client]) true false)
:field [:vendor-id]
:text-field [:vendor-name]
:event change-event
:text-event change-event
:event [::change-vendor [::form]]
:spec (s/nilable ::invoice/vendor-id)
:subscription data}]]]]

View File

@@ -87,7 +87,7 @@
keys (assoc keys
:on-change (dispatch-value-change (conj event field))
:value (get-in subscription field)
:value (or (get-in subscription field) "")
:class (str class
(when (and spec (not (s/valid? spec (get-in subscription field))))
" is-danger")))
@@ -95,7 +95,7 @@
options (if allow-nil?
(with-keys (conj rest [:option {:value nil}]))
(with-keys rest))]
(into [dom keys] options)))
(into [dom (dissoc keys :allow-nil?)] options)))
(defmethod do-bind "radio" [dom {:keys [field subscription event class value spec] :as keys} & rest]