From 7223ef8c0d99e1d16e567cfe7e9e102a0ef442b6 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sun, 1 Jan 2023 15:24:05 -0800 Subject: [PATCH] Minor tweaks for percentages --- .../views/components/percentage_field.cljs | 52 ++++++------------- .../auto_ap/views/pages/invoices/form.cljs | 16 ++++-- 2 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/cljs/auto_ap/views/components/percentage_field.cljs b/src/cljs/auto_ap/views/components/percentage_field.cljs index 6eeb9af3..5c20618f 100644 --- a/src/cljs/auto_ap/views/components/percentage_field.cljs +++ b/src/cljs/auto_ap/views/components/percentage_field.cljs @@ -15,50 +15,32 @@ :else (str (js/parseFloat value)))})] - (react/useEffect (fn [] - ;; allow the controlling field to change the raw representation - ;; when the raw amount is a valid representation, so that 33. - ;; doesn't get unset - (when (or - (and (:raw parsed-amount) - (re-find good-% (:raw parsed-amount))) - (str/blank? (:raw parsed-amount))) - (set-parsed-amount - (assoc parsed-amount - :parsed value - :raw (cond - (str/blank? value) - "" - - (js/Number.isNaN (js/parseFloat value)) - "" - - :else - (str (js/parseFloat value)))))) - nil)) [:div.control.has-icons-left [:input.input {:type "text" :disabled disabled :placeholder placeholder :class class :on-change (fn [e] - (let [raw (.. e -target -value) - new-value (when (and raw - (not (str/blank? raw)) - (re-find good-% raw)) - (js/parseFloat raw))] - (set-parsed-amount {:raw raw - :parsed new-value}) - (when (not= value new-value) - (on-change new-value)))) + (set-parsed-amount + (assoc parsed-amount :raw (.. e -target -value)))) :value (or (:raw parsed-amount) "") :on-blur (fn [] - (when-not (re-find good-% (:raw parsed-amount)) - - (set-parsed-amount {:raw "" - :parsed nil}) - (on-change nil)) + (let [raw (:raw parsed-amount)] + (if (re-find good-% raw) + (let [new-value (when (and raw + (not (str/blank? raw)) + (re-find good-% raw)) + (js/parseFloat raw))] + (set-parsed-amount {:raw raw + :parsed new-value}) + (when (not= value new-value) + (on-change new-value))) + + (do + (set-parsed-amount {:raw "" + :parsed nil}) + (on-change nil)))) (when on-blur (on-blur))) :min min diff --git a/src/cljs/auto_ap/views/pages/invoices/form.cljs b/src/cljs/auto_ap/views/pages/invoices/form.cljs index 2720dcb8..eb5f32f8 100644 --- a/src/cljs/auto_ap/views/pages/invoices/form.cljs +++ b/src/cljs/auto_ap/views/pages/invoices/form.cljs @@ -104,15 +104,22 @@ (re-frame/reg-event-fx ::updated - [(re-frame/inject-cofx ::inject/sub [::subs/client])] - (fn [{:keys [db] ::subs/keys [client]} [_ _ command]] + [ + (re-frame/inject-cofx ::inject/sub (fn [[_ _ _ client]] + [::subs/locations-for-client (:id client)]))] + (fn [{:keys [db] ::subs/keys [locations-for-client]} [_ _ command client]] + (println locations-for-client) (when (= :create command) {:db (-> db (forms/stop-form ::form ) (forms/start-form ::form {:client client :status :unpaid - :date (c/now)}))}))) + :date (c/now) + :expense-accounts + (eaf/from-graphql [] + 0.0 + locations-for-client)}))}))) (re-frame/reg-event-fx ::adding @@ -253,7 +260,8 @@ :class "live-added") (if (:id data) :edit - :create)]) + :create) + (:client data)]) :on-error [::forms/save-error ::form]}}))) (re-frame/reg-event-fx