add utils

This commit is contained in:
Adam Jeniski 2025-09-17 12:09:30 -09:00
parent eba0dbe7c2
commit 09af95c459

View File

@ -19,7 +19,7 @@
(remove-tap printer)) (remove-tap printer))
(defn page-template [view] (defn render-page [content]
(str (str
(h/html (h/html
(h/raw "<!DOCTYPE html>") (h/raw "<!DOCTYPE html>")
@ -31,9 +31,10 @@
[:link {:rel "icon" [:link {:rel "icon"
:type "imaage/x-icon" :type "imaage/x-icon"
:href "https://data-star.dev/cdn-cgi/image/format=auto,width=24/static/images/rocket-48x48-4c739bfaffe86a6ffcc3a6d77e3c5547730f03d74c11aa460209596d1811f7a3.png"}]] :href "https://data-star.dev/cdn-cgi/image/format=auto,width=24/static/images/rocket-48x48-4c739bfaffe86a6ffcc3a6d77e3c5547730f03d74c11aa460209596d1811f7a3.png"}]]
[:body view]]))) [:body content]])))
(defn todos-fragment [] (defn todos-fragment []
;; todo: make functional. sloppy sloppy
(let [todos (db/get-all-todos)] (let [todos (db/get-all-todos)]
(h/html (h/html
[:div {:id "todos"} [:div {:id "todos"}
@ -89,35 +90,48 @@
#:d*.elements{:selector "#todos" #:d*.elements{:selector "#todos"
:patch-mode "replace"})) :patch-mode "replace"}))
(defmacro fn-sse [[req sse :as _bindings]
& body]
`(fn [~req respond# _#]
(respond#
(->sse-response
~req
{on-open (fn [~sse]
(d*/with-open-sse ~sse
~@body))}))))
(defmacro defn-sse [var-name
[req sse :as _bindings]
& body]
`(defn ~var-name [~req respond# _#]
(respond#
(->sse-response
~req
{on-open (fn [~sse]
(d*/with-open-sse ~sse
~@body))}))))
(defn-sse create-todo [req sse]
(let [title (-> req :body :title)]
(when title
(db/add-todo! title))
(d*/with-open-sse sse
(refresh-todos! sse)
(d*/patch-signals! sse #json{:title ""}))))
(defroutes approutes (defroutes approutes
(GET "/" [] (GET "/" []
(fn [_ respond _] (fn [_ respond _]
(-> (app-fragment) (-> (app-fragment)
page-template render-page
respond))) respond)))
(wrap-json-body (wrap-json-body
(context "/sse/todo" [] (context "/sse/todo" []
(GET "/" [] (GET "/" []
(fn [req respond _] (fn-sse [_req sse]
(respond (refresh-todos! sse)))
(->sse-response req (POST "/create-todo" [] create-todo)
{on-open (fn [sse]
(d*/with-open-sse sse
(refresh-todos! sse)))}))))
(POST "/create-todo" []
(fn [req respond _]
(let [title (-> req :body :title)]
(tap> title)
(when title
(db/add-todo! title))
(respond
(->sse-response req
{on-open (fn [sse]
(d*/with-open-sse sse
(refresh-todos! sse)
(d*/patch-signals! sse
#json{:title ""})))})))))
(context ["/:id", :id #"[0-9]+"] [id] (context ["/:id", :id #"[0-9]+"] [id]
(let-routes [id (read-string id)] (let-routes [id (read-string id)]