From 634f7e41e2fbd1e5446ee52914d4fc2262ad7af7 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 19 Apr 2024 08:09:27 +0300 Subject: [PATCH] Add a validation for the about form in the admin panel. --- src/main/blog/handlers.cljs | 23 +++++++++++++++++++++++ src/main/blog/server.cljs | 7 +++++++ views/about_content_validation.hbs | 8 ++++++++ views/admin_panel.hbs | 4 ++-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 views/about_content_validation.hbs diff --git a/src/main/blog/handlers.cljs b/src/main/blog/handlers.cljs index 9c72a2e..10b7490 100644 --- a/src/main/blog/handlers.cljs +++ b/src/main/blog/handlers.cljs @@ -258,6 +258,29 @@ (res/status 500) (res/send (ex-message err)))))))) +(defn htmx-about-validation-handler-factory + [] + (fn [req res] + (let [channel (chan) + about-content (.-content (.-body req))] + (go + (try + (let [error (cond + (= (count about-content) 0) + "Content must nonempty!!!" + + :else + nil)] + (res/status 200) + (.render res "about_content_validation" + (-> {:error error + :about-content about-content + :layout false} + (clj->js)))) + (catch js/Error err + (res/status 500) + (res/send (ex-message err)))))))) + (defn htmx-update-about-handler-factory [client] (fn [req res] diff --git a/src/main/blog/server.cljs b/src/main/blog/server.cljs index 0abca7a..f4a880e 100644 --- a/src/main/blog/server.cljs +++ b/src/main/blog/server.cljs @@ -94,6 +94,13 @@ (. app use "/admin" (handlers/user-not-authorized-factory)) + (. app post "/htmx/validation/about/" + handlers/is-user-authorized + (handlers/htmx-about-validation-handler-factory)) + + (. app use "/htmx/validation/about/" + (handlers/user-not-authorized-factory)) + (. app patch "/htmx/about/" handlers/is-user-authorized (handlers/htmx-update-about-handler-factory client)) diff --git a/views/about_content_validation.hbs b/views/about_content_validation.hbs new file mode 100644 index 0000000..0bfdb42 --- /dev/null +++ b/views/about_content_validation.hbs @@ -0,0 +1,8 @@ +
+ + + {{#if error}} +
{{ error }}
+ {{/if}} +
diff --git a/views/admin_panel.hbs b/views/admin_panel.hbs index 9558baf..8e39d4b 100644 --- a/views/admin_panel.hbs +++ b/views/admin_panel.hbs @@ -16,9 +16,9 @@
-
+
- +