better warning behavior.A
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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)"}}])]))])))
|
||||
|
||||
@@ -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)}
|
||||
|
||||
Reference in New Issue
Block a user