add utils
This commit is contained in:
parent
eba0dbe7c2
commit
09af95c459
@ -19,7 +19,7 @@
|
||||
|
||||
(remove-tap printer))
|
||||
|
||||
(defn page-template [view]
|
||||
(defn render-page [content]
|
||||
(str
|
||||
(h/html
|
||||
(h/raw "<!DOCTYPE html>")
|
||||
@ -31,9 +31,10 @@
|
||||
[:link {:rel "icon"
|
||||
:type "imaage/x-icon"
|
||||
: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 []
|
||||
;; todo: make functional. sloppy sloppy
|
||||
(let [todos (db/get-all-todos)]
|
||||
(h/html
|
||||
[:div {:id "todos"}
|
||||
@ -89,35 +90,48 @@
|
||||
#:d*.elements{:selector "#todos"
|
||||
: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
|
||||
(GET "/" []
|
||||
(fn [_ respond _]
|
||||
(-> (app-fragment)
|
||||
page-template
|
||||
render-page
|
||||
respond)))
|
||||
|
||||
(wrap-json-body
|
||||
(context "/sse/todo" []
|
||||
(GET "/" []
|
||||
(fn [req respond _]
|
||||
(respond
|
||||
(->sse-response req
|
||||
{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 ""})))})))))
|
||||
(fn-sse [_req sse]
|
||||
(refresh-todos! sse)))
|
||||
(POST "/create-todo" [] create-todo)
|
||||
|
||||
(context ["/:id", :id #"[0-9]+"] [id]
|
||||
(let-routes [id (read-string id)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user