about summary refs log tree commit diff
path: root/users/aspen/bbbg/src/bbbg/handlers/core.clj
blob: caa679ee873ff379af06fa95cdab23d89aa9303b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(ns bbbg.handlers.core
  (:require
   [bbbg.user :as user]
   [bbbg.views.flash :as flash]
   [hiccup.core :refer [html]]
   [ring.util.response :refer [content-type response]]
   [clojure.string :as str]))

(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))))

(def ^:dynamic *current-uri*)

(defn wrap-current-uri [handler]
  (fn [req]
    (binding [*current-uri* (:uri req)]
      (handler req))))

(defn nav-item [href label]
  (let [active?
        (when *current-uri*
          (str/starts-with?
           *current-uri*
           href))]
    [:li {:class (when active? "active")}
     [:a {:href href}
      label]]))

(defn global-nav []
  [:nav.global-nav
   [:ul
    (nav-item "/events" "Events")
    (when *authenticated?*
      (nav-item "/attendees" "Attendees"))
    [:li.spacer]
    [:li
     (if *authenticated?*
       [:form.link-form
        {:method :post
         :action "/auth/sign-out"}
        [:input {:type "submit"
                 :value "Sign Out"}]]
       [:a {:href "/auth/discord"}
        "Sign In"])]]])

(defn render-page [opts & body]
  (let [[{:keys [title]} body]
        (if (map? opts)
          [opts body]
          [{} (concat [opts] body)])]
    (html
     [:html {:lang "en"}
      [:head
       [:meta {:charset "UTF-8"}]
       [:meta {:name "viewport"
               :content "width=device-width,initial-scale=1"}]
       [:title (if title
                 (str title " - BBBG")
                 "BBBG")]
       [:link {:rel "stylesheet"
               :type "text/css"
               :href "/main.css"}]]
      [:body
       [:div.content
        (global-nav)
        #_(flash/render-flash flash/test-flash)
        (flash/render-flash)
        body]
       [:script {:src "/main.js"}]]])))

(defn page-response [& render-page-args]
  (-> (apply render-page render-page-args)
      response
      (content-type "text/html")))

(comment
  (render-page
   [:h1 "hi"])
  )