about summary refs log tree commit diff
path: root/users/grfn/bbbg/src/bbbg/handlers/core.clj
blob: 37561c25e1a3d261c9228bbe9f034899adfb2212 (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
(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]]))

(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]
  (let [[{:keys [title]} body]
        (if (map? opts)
          [opts body]
          [{} (concat [opts] body)])]
    (html
     [:html {:lang "en"}
      [:head
       [:meta {:charset "UTF-8"}]
       [:title (if title
                 (str title " - BBBG")
                 "BBBG")]
       [:link {:rel "stylesheet"
               :type "text/css"
               :href "/main.css"}]]
      [:body
       [:div.content
        (global-nav)
        (flash/render-flash)
        body]
       [:script {:src "https://cdnjs.cloudflare.com/ajax/libs/tarekraafat-autocomplete.js/10.2.6/autoComplete.js"}]
       [: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"])
  )