65 lines
2 KiB
Clojure
65 lines
2 KiB
Clojure
|
|
(ns com.biffweb.my-project.app
|
||
|
|
(:require [com.biffweb :as biff]
|
||
|
|
[com.biffweb.my-project.middleware :as mid]
|
||
|
|
[com.biffweb.my-project.ui :as ui]
|
||
|
|
[honey.sql :as sql]
|
||
|
|
[com.biffweb.my-project.settings :as settings]
|
||
|
|
[next.jdbc :as jdbc]))
|
||
|
|
|
||
|
|
(defn bar-form [{:keys [value]}]
|
||
|
|
(biff/form
|
||
|
|
{:hx-post "/app/set-bar"
|
||
|
|
:hx-swap "outerHTML"}
|
||
|
|
[:label {:for "bar"} "Bar: "
|
||
|
|
[:span (pr-str value)]]
|
||
|
|
[:div
|
||
|
|
[:input#bar {:type "text" :name "bar" :value value}]
|
||
|
|
[:button {:type "submit"} "Update"]]
|
||
|
|
"This demonstrates updating a value with HTMX."))
|
||
|
|
|
||
|
|
(defn set-bar [{:keys [example/ds session params] :as _ctx}]
|
||
|
|
(jdbc/execute! ds (sql/format {:update :users
|
||
|
|
:set {:bar (:bar params)}
|
||
|
|
:where [:= :id (:uid session)]}))
|
||
|
|
(biff/render (bar-form {:value (:bar params)})))
|
||
|
|
|
||
|
|
(defn app [{:keys [session example/ds] :as _ctx}]
|
||
|
|
(let [query (sql/format {:select [:*]
|
||
|
|
:from [:users] :where [:= :id (:uid session)]})
|
||
|
|
{:users/keys [email bar]} (jdbc/execute-one! ds query)]
|
||
|
|
(ui/page
|
||
|
|
{}
|
||
|
|
[:header.container
|
||
|
|
[:hgroup
|
||
|
|
{:style
|
||
|
|
{:display "flex"
|
||
|
|
:align-items "center"
|
||
|
|
:justify-content "space-between"}}
|
||
|
|
"some text for " email
|
||
|
|
(biff/form
|
||
|
|
{:action "/auth/signout"
|
||
|
|
:class "inline"}
|
||
|
|
[:button {:type "submit"}
|
||
|
|
"Sign out"])]]
|
||
|
|
|
||
|
|
(bar-form {:value bar}))))
|
||
|
|
|
||
|
|
(def about-page
|
||
|
|
(ui/page
|
||
|
|
{:base/title (str "About " settings/app-name)}
|
||
|
|
[:p "This app was made with "
|
||
|
|
[:a {:href "https://biffweb.com"} "Biff"] "."]))
|
||
|
|
|
||
|
|
(defn echo [{:keys [params]}]
|
||
|
|
{:status 200
|
||
|
|
:headers {"content-type" "application/json"}
|
||
|
|
:body params})
|
||
|
|
|
||
|
|
(def module
|
||
|
|
{:static {"/about/" about-page}
|
||
|
|
:routes ["/app" {:middleware [mid/wrap-signed-in]}
|
||
|
|
["" {:get app}]
|
||
|
|
|
||
|
|
["/set-bar" {:post set-bar}]]
|
||
|
|
:api-routes [["/api/echo" {:post echo}]]})
|