diff --git a/src/cljs/auto_ap/subs.cljs b/src/cljs/auto_ap/subs.cljs index 81f42325..fe4e5b79 100644 --- a/src/cljs/auto_ap/subs.cljs +++ b/src/cljs/auto_ap/subs.cljs @@ -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] 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 d8652866..85928998 100644 --- a/src/cljs/auto_ap/views/components/expense_accounts_field.cljs +++ b/src/cljs/auto_ap/views/components/expense_accounts_field.cljs @@ -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 diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 3dab245c..48fb0fb2 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -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}]]]] diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index c17d94dd..76ca020d 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -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]