diff --git a/src/cljs/auto_ap/forms.cljs b/src/cljs/auto_ap/forms.cljs index 08bc0c3d..58c4efc7 100644 --- a/src/cljs/auto_ap/forms.cljs +++ b/src/cljs/auto_ap/forms.cljs @@ -1,5 +1,6 @@ (ns auto-ap.forms (:require [re-frame.core :as re-frame] + [re-frame.interceptor :as i] [auto-ap.views.utils :refer [dispatch-event bind-field]])) @@ -89,6 +90,17 @@ (assoc-in [::forms id :status] :loading) (assoc-in [::forms id :error] nil))) +(defn settles [{:keys [event time key]}] + (i/->interceptor + :id :settles + :befor (fn [context] + (println "here2") + context) + :after (fn [context] + (i/assoc-effect context :dispatch-debounce {:event event + :time time + :key key})))) + (defn triggers-loading [form] (re-frame/enrich (fn [db event] diff --git a/src/cljs/auto_ap/views/components/modal.cljs b/src/cljs/auto_ap/views/components/modal.cljs index 24588387..f30ebb1e 100644 --- a/src/cljs/auto_ap/views/components/modal.cljs +++ b/src/cljs/auto_ap/views/components/modal.cljs @@ -3,7 +3,7 @@ [reagent.core :as r] [auto-ap.events :as events] [auto-ap.subs :as subs] - [auto-ap.views.utils :refer [with-keys]])) + [auto-ap.views.utils :refer [with-keys appearing]])) (defn modal [{:keys [title foot hide-event class]} & body] [:div.modal.is-active (cond-> {} @@ -43,9 +43,15 @@ ] :id id :hide-event [::events/modal-status id {:visible? false}]} - (when error-message - [:div.notification.is-warning error-message]) - (when warning - [:div.notification.is-info warning])] + [appearing {:visible? error-message + :timeout 200 + :enter-class "appear" + :exit-class "disappear"} + [:div.notification.is-warning error-message]] + [appearing {:visible? warning + :timeout 200 + :enter-class "appear" + :exit-class "disappear"} + [:div.notification.is-info warning]]] (into (r/children (r/current-component))) (into [(when saving? [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]))]))) diff --git a/src/cljs/auto_ap/views/components/vendor_dialog.cljs b/src/cljs/auto_ap/views/components/vendor_dialog.cljs index 2078f07e..e39de9d1 100644 --- a/src/cljs/auto_ap/views/components/vendor_dialog.cljs +++ b/src/cljs/auto_ap/views/components/vendor_dialog.cljs @@ -18,20 +18,27 @@ (mapv #(.toLowerCase (str/join "" %)) (partition len 1 text))) -(defn partial-matches-vendor? [vendor vendors] - (when (> (count (:name vendor)) 5) - (let [text (.toLowerCase (:name vendor))] - (->> vendors - (filter #(not= (:id %) (:id vendor))) - (map :name) - (mapcat - (fn [v] (mapv - (fn [n] [v (jaro-winkler text n ) n text]) - (ngrams v (count text))))) - (filter #(> (second %) 0.9)) - #_(map (fn [x] (doto x println))) - (map first) - first)))) +(re-frame/reg-event-db + ::settled + [(forms/in-form ::vendor-form)] + (fn [{:keys [data] :as form} _] + (let [vendors @(re-frame/subscribe [::subs/vendors]) + text (.toLowerCase (:name data)) + matching-vendor (when (> (count text) 5) + (->> vendors + (filter #(not= (:id %) (:id data))) + (map :name) + (mapcat + (fn [v] (mapv + (fn [n] [v (jaro-winkler text n ) n text]) + (ngrams v (count text))))) + (filter #(> (second %) 0.9)) + #_(map (fn [x] (doto x println))) + (map first) + first))] + (if matching-vendor + (assoc form :warning (str "Are you sure you don't mean " matching-vendor "?")) + (dissoc form :warning))))) (re-frame/reg-event-db ::removed-override @@ -76,6 +83,9 @@ (re-frame/reg-event-db ::changed + [(forms/settles {:key ::vendor-form + :time 500 + :event [::settled]})] (forms/change-handler ::vendor-form (fn [data field value] @@ -308,7 +318,7 @@ (defn vendor-dialog [{:keys [save-event] }] (let [clients @(re-frame/subscribe [::subs/clients]) all-vendors @(re-frame/subscribe [::subs/vendors]) - {:keys [data error ] :as f} @(re-frame/subscribe [::forms/form ::vendor-form]) + {:keys [data error warning] :as f} @(re-frame/subscribe [::forms/form ::vendor-form]) change-event [::changed]] [action-modal {:id ::dialog :class ["wide"] @@ -319,9 +329,7 @@ [:span.icon.has-text-danger [:i.fa.fa-exclamation-triangle]])] :status-from ::vendor-form - #_#_:warning - (when-let [vendor (partial-matches-vendor? data all-vendors)] - (str "Are you sure you don't mean " vendor "?")) + :warning warning :action-text "Save" :save-event [::save] :can-submit? (s/valid? ::entity/vendor data)}