From a694acb1faac10a3446d58dc7b573dc17f5ed873 Mon Sep 17 00:00:00 2001 From: jem Date: Tue, 15 Jun 2021 21:23:39 +0200 Subject: [PATCH] refactor useful functions out --- src/main/cljs/dda/c4k_keycloak/browser.cljs | 53 ++++++++++----------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/cljs/dda/c4k_keycloak/browser.cljs b/src/main/cljs/dda/c4k_keycloak/browser.cljs index 9c57796..7f961fc 100644 --- a/src/main/cljs/dda/c4k_keycloak/browser.cljs +++ b/src/main/cljs/dda/c4k_keycloak/browser.cljs @@ -11,9 +11,29 @@ (print "debug " sth) sth) -(defn fqdn [] - (-> js/document - (.getElementById "fqdn"))) +(defn get-element-by-id [name] + (-> js/document + (.getElementById name))) + +(defn get-content-from-element [name] + (-> (get-element-by-id name) + (.-value))) + +(defn set-validation-result! + [name validation-result] + (-> (get-element-by-id (str name "-validation")) + (.-innerHTML) + (set! validation-result)) + (-> (get-element-by-id name) + (.setCustomValidity validation-result)) + validation-result) + +(defn validate! [name spec] + (let [content (get-content-from-element name)] + (if (s/valid? spec content) + (set-validation-result! name "") + (set-validation-result! name + (expound/expound-str spec content {:print-specs? false}))))) (defn issuer [] (-> js/document @@ -25,10 +45,6 @@ (when-not (st/blank? issuer-str) (keyword issuer-str)))) -(defn fqdn-from-document [] - (-> (fqdn) - (.-value))) - (defn auth [] (-> js/document (.getElementById "auth"))) @@ -39,7 +55,7 @@ (defn config-from-document [] (merge - {:fqdn (fqdn-from-document)} + {:fqdn (get-content-from-element "fqdn")} (when-not (st/blank? (issuer-from-document)) {:issuer (issuer-from-document)}))) @@ -54,16 +70,6 @@ (.-value) (set! input))) -(defn set-fqdn-validation-result! - [validation-result] - (-> js/document - (.getElementById "fqdn-validation") - (.-innerHTML) - (set! validation-result)) - (-> (fqdn) - (.setCustomValidity validation-result)) - validation-result) - (defn set-issuer-validation-result! [validation-result] (-> js/document @@ -74,13 +80,6 @@ (.setCustomValidity validation-result)) validation-result) -(defn validate-fqdn! [] - (let [fqdn (fqdn-from-document)] - (if (s/valid? ::kc/fqdn fqdn) - (set-fqdn-validation-result! "") - (set-fqdn-validation-result! - (expound/expound-str ::kc/fqdn fqdn {:print-specs? false}))))) - (defn validate-issuer! [] (let [issuer (issuer-from-document)] (print-debug (js->clj issuer)) @@ -113,7 +112,7 @@ (expound/expound-str core/auth? auth-map {:print-specs? false}))))) (defn validate-all! [] - (validate-fqdn!) + (validate! "fqdn" ::kc/fqdn) (validate-issuer!) (validate-auth!) (set-validated!)) @@ -126,7 +125,7 @@ #(do (validate-all!) (-> (core/generate (config-from-document) (auth-from-document)) (set-output!))))) - (-> (fqdn) + (-> (get-element-by-id "fqdn") (.addEventListener "blur" #(do (validate-all!)))) (-> (issuer)