From 111710365bf40ffc3f600c85168f03260221f6ae Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 8 Feb 2019 07:40:14 -0800 Subject: [PATCH] invoice number like incorporates a timeout. --- src/cljs/auto_ap/effects.cljs | 13 +++++++++++ .../auto_ap/views/pages/unpaid_invoices.cljs | 23 +++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/cljs/auto_ap/effects.cljs b/src/cljs/auto_ap/effects.cljs index 10f2b804..8c0c8217 100644 --- a/src/cljs/auto_ap/effects.cljs +++ b/src/cljs/auto_ap/effects.cljs @@ -119,6 +119,19 @@ node)) m)) +(defonce timeouts + (atom {})) + +(re-frame/reg-fx + :dispatch-debounce + (fn [{:keys [event time key]}] + (js/clearTimeout (@timeouts key)) + (swap! timeouts assoc key + (js/setTimeout (fn [] + (re-frame/dispatch event) + (swap! timeouts dissoc key)) + time)))) + (re-frame/reg-fx :graphql (fn [{:keys [query on-success on-error token variables query-obj]}] diff --git a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs index e4383d6a..88a15ce4 100644 --- a/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/unpaid_invoices.cljs @@ -72,6 +72,7 @@ (fn [db] (-> db (::params {})))) + (re-frame/reg-event-fx ::params-change (fn [cofx [_ params]] @@ -79,7 +80,7 @@ (assoc-in [:status :loading] true) (assoc-in [::params] params)) :graphql {:token (-> cofx :db :user) - :query-obj (invoice-table/query (doto (assoc params :imported true) println)) + :query-obj (invoice-table/query (-> params (assoc :imported true) (dissoc :invoice-number-like-current)) ) :on-success [::received]}})) (re-frame/reg-event-db ::unmount-invoices @@ -683,14 +684,26 @@ (assoc-in updated [::params :vendor-id] value)) updated)))) +(re-frame/reg-event-fx + ::invoice-number-like-current-changed + (fn [{:keys [db]} [_ params invoice-like]] + {:db (assoc-in db [::params :invoice-number-like-current] invoice-like ) + :dispatch-debounce {:event [::invoice-number-like-settled invoice-like] + :time 500 + :key ::invoice-nuber-like}})) + +(re-frame/reg-event-fx + ::invoice-number-like-settled + (fn [{:keys [db]} [_ invoice-like]] + {:dispatch [::params-change (assoc (::params db) :invoice-number-like invoice-like) ]})) + (defn invoice-number-filter [] - (let [{:keys [invoice-number-like] :as params} @(re-frame/subscribe [::params])] + (let [{:keys [invoice-number-like-current] :as params} @(re-frame/subscribe [::params])] [:div.field [:div.control [:input.input {:placeholder "AP-123" - :value invoice-number-like + :value invoice-number-like-current :on-change (fn [x] - (re-frame/dispatch [::params-change (assoc params :invoice-number-like (.. x -target -value) :start 0) ]) - )} ]]])) + (re-frame/dispatch [::invoice-number-like-current-changed params (.. x -target -value) ]))} ]]])) (defn pay-button [{:keys [print-checks-shown? checked-invoices print-checks-loading?]}] (let [current-client @(re-frame/subscribe [::subs/client])]