From 88a9eb7749ca2e46edd6d04689d22fbda11c8952 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 19 Apr 2024 09:41:31 +0300 Subject: [PATCH] Add a validation for the edit article form. --- src/main/blog/handlers.cljs | 50 ++++++++++++++++++++++++++++++++++++- src/main/blog/server.cljs | 14 +++++++++++ views/admin_panel.hbs | 11 +++++--- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/main/blog/handlers.cljs b/src/main/blog/handlers.cljs index fbe0b4f..329abb9 100644 --- a/src/main/blog/handlers.cljs +++ b/src/main/blog/handlers.cljs @@ -211,6 +211,54 @@ (res/status 500) (res/send (ex-message err)))))))) +(defn htmx-update-title-validation-handler-factory + [] + (fn [req res] + (let [channel (chan) + title (.-title (.-body req)) + content (.-content (.-body req))] + (go + (try + (let [error (cond + (= (count title) 0) + "Tittle must be nonempty!!!" + + :else + nil)] + (res/status 200) + (.render res "update_title_validation" + (-> {:error error + :title title + :layout false} + (clj->js)))) + (catch js/Error err + (res/status 500) + (res/send (ex-message err)))))))) + +(defn htmx-update-content-validation-handler-factory + [] + (fn [req res] + (let [channel (chan) + title (.-title (.-body req)) + content (.-content (.-body req))] + (go + (try + (let [error (cond + (= (count content) 0) + "Content must be nonempty!!!" + + :else + nil)] + (res/status 200) + (.render res "update_content_validation" + (-> {:error error + :content content + :layout false} + (clj->js)))) + (catch js/Error err + (res/status 500) + (res/send (ex-message err)))))))) + (defn htmx-update-article-handler-factory [client] (fn [req res] @@ -313,7 +361,7 @@ (res/send (ex-message err)))))))) (defn htmx-create-title-validation-handler-factory - [client] + [] (fn [req res] (let [channel (chan) title (.-title (.-body req)) diff --git a/src/main/blog/server.cljs b/src/main/blog/server.cljs index 30508ad..5f3b5ef 100644 --- a/src/main/blog/server.cljs +++ b/src/main/blog/server.cljs @@ -149,6 +149,20 @@ (. app use "/htmx/admin/rows/article/:id/" (handlers/user-not-authorized-factory)) + (. app post "/htmx/validation/update-title/" + handlers/is-user-authorized + (handlers/htmx-update-title-validation-handler-factory)) + + (. app use "/htmx/validation/update-title/" + (handlers/user-not-authorized-factory)) + + (. app post "/htmx/validation/update-content/" + handlers/is-user-authorized + (handlers/htmx-update-content-validation-handler-factory)) + + (. app use "/htmx/validation/update-content/" + (handlers/user-not-authorized-factory)) + (. app patch "/htmx/admin/rows/article/:id/" handlers/is-user-authorized (handlers/htmx-update-article-handler-factory client)) diff --git a/views/admin_panel.hbs b/views/admin_panel.hbs index 0fc6f09..6c98f9b 100644 --- a/views/admin_panel.hbs +++ b/views/admin_panel.hbs @@ -163,7 +163,7 @@
-
+
-
+
- +