feat(grfn/bbbg): Move nav to page-global

Change-Id: I0a313d9df818fcbfeea5484123c1e7bcec439d66
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4509
Reviewed-by: grfn <grfn@gws.fyi>
Autosubmit: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
This commit is contained in:
Griffin Smith 2021-12-19 20:26:13 -05:00 committed by clbot
parent 4643585e01
commit 9ea538beea
3 changed files with 39 additions and 27 deletions

View file

@ -5,6 +5,37 @@
[hiccup.core :refer [html]] [hiccup.core :refer [html]]
[ring.util.response :refer [content-type response]])) [ring.util.response :refer [content-type response]]))
(def ^:dynamic *authenticated?* false)
(defn authenticated? [request]
(some? (get-in request [:session ::user/id])))
(defn wrap-auth-required [handler]
(fn [req]
(when (authenticated? req)
(handler req))))
(defn wrap-dynamic-auth [handler]
(fn [req]
(binding [*authenticated?* (authenticated? req)]
(handler req))))
(defn global-nav []
[:nav.global-nav
[:ul
(when *authenticated?*
[:li [:a {:href "/attendees"}
"Attendees"]])
[:li [:a {:href "/events"}
"Events"]]
(if authenticated?
[:li [:form {:method :post
:action "/auth/sign-out"}
[:input {:type "submit"
:value "Sign Out"}]]]
[:li [:a {:href "/auth/discord"}
"Sign In"]])]])
(defn render-page [opts & body] (defn render-page [opts & body]
(let [[{:keys [title]} body] (let [[{:keys [title]} body]
(if (map? opts) (if (map? opts)
@ -22,6 +53,7 @@
:href "/main.css"}]] :href "/main.css"}]]
[:body [:body
[:div.content [:div.content
(global-nav)
(flash/render-flash) (flash/render-flash)
body] body]
[:script {:src "https://cdnjs.cloudflare.com/ajax/libs/tarekraafat-autocomplete.js/10.2.6/autoComplete.js"}] [:script {:src "https://cdnjs.cloudflare.com/ajax/libs/tarekraafat-autocomplete.js/10.2.6/autoComplete.js"}]
@ -32,14 +64,6 @@
response response
(content-type "text/html"))) (content-type "text/html")))
(defn authenticated? [request]
(some? (get-in request [:session ::user/id])))
(defn wrap-auth-required [handler]
(fn [req]
(when (authenticated? req)
(handler req))))
(comment (comment
(render-page (render-page
[:h1 "hi"]) [:h1 "hi"])

View file

@ -9,23 +9,10 @@
[ring.util.response :refer [redirect]] [ring.util.response :refer [redirect]]
[bbbg.discord :as discord])) [bbbg.discord :as discord]))
(defn- home-page [{:keys [authenticated?]}] (defn- home-page []
[:nav.home-nav [:div.home-page
[:ul [:a {:href "/signup-forms"}
(when authenticated? "Event Signup Form"]])
[:li [:a {:href "/attendees"}
"Attendees"]])
[:li [:a {:href "/events"}
"Events"]]
[:li [:a {:href "/signup-forms"}
"Event Signup Form"]]
(if authenticated?
[:li [:form {:method :post
:action "/auth/sign-out"}
[:input {:type "submit"
:value "Sign Out"}]]]
[:li [:a {:href "/auth/discord"}
"Sign In"]])]])
(defn auth-failure [] (defn auth-failure []
[:div.auth-failure [:div.auth-failure
@ -36,8 +23,7 @@
(defn home-routes [{:keys [db] :as env}] (defn home-routes [{:keys [db] :as env}]
(routes (routes
(GET "/" request (GET "/" [] (page-response (home-page)))
(page-response (home-page {:authenticated? (authenticated? request)})))
(POST "/auth/sign-out" request (POST "/auth/sign-out" request
(if (authenticated? request) (if (authenticated? request)

View file

@ -3,6 +3,7 @@
[bbbg.discord.auth :as discord.auth :refer [wrap-discord-auth]] [bbbg.discord.auth :as discord.auth :refer [wrap-discord-auth]]
[bbbg.handlers.attendee-checks :as attendee-checks] [bbbg.handlers.attendee-checks :as attendee-checks]
[bbbg.handlers.attendees :as attendees] [bbbg.handlers.attendees :as attendees]
[bbbg.handlers.core :refer [wrap-dynamic-auth]]
[bbbg.handlers.events :as events] [bbbg.handlers.events :as events]
[bbbg.handlers.home :as home] [bbbg.handlers.home :as home]
[bbbg.handlers.signup-form :as signup-form] [bbbg.handlers.signup-form :as signup-form]
@ -82,6 +83,7 @@
(defn middleware [app env] (defn middleware [app env]
(-> app (-> app
wrap-dynamic-auth
(wrap-discord-auth env) (wrap-discord-auth env)
wrap-keyword-params wrap-keyword-params
wrap-params wrap-params