summaryrefslogtreecommitdiff
path: root/src/chef/pages/admin.clj
blob: 031da8c7ba0010496d6c1bb48103ac1b00afd1b7 (plain)
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
(ns chef.pages.admin
  (:require [chef.utils :as cutils]
            [hiccup2.core :as html]
            [ring.util.response :as ruresp]
            [chef.database :as cdb]
            [next.jdbc :as jdbc]
            [honey.sql :as sql]))

(defn- render-category [data children]
  [:li
   [:p {:style {:display :inline-block}}
    (if (some? data)
      [:input {:type       :text :placeholder "Name"
               :value      (:categories/name data)
               :name       "name"
               :hx-post    (str "/api/admin/edit-category/" (:categories/id data))
               :hx-trigger "change"}]
      "Startseite")]
   [:p {:style {:display      :inline-block
                :margin-left  "1em"
                :margin-right "1em"}} "->"]
   [:input {:type       :text :placeholder "Frage"
            :style      {:display :inline-block
                         :width   :auto}
            :value      (:categories/question data)
            :name       "question"
            :hx-post    (str "/api/admin/edit-category/" (:categories/id data))
            :hx-trigger "change"}]                          ; TODO: only show when category has children
   [:img {:src     "/static/icons/plus.svg" :height "30em"
          :style   {:vertical-align :middle
                    :margin-left    "1em"}
          :hx-post (str "/api/admin/create-category"
                        (when (some? data) (str "?parent=" (:categories/id data))))
          :hx-swap "none"}]
   [:img {:src       "/static/icons/trash.svg" :height "30em"
          :style     {:vertical-align :middle
                      :margin-left    "1em"}
          :hx-delete (str "/api/admin/delete-category/" (:categories/id data))
          :hx-swap   "none"}]
   [:ul
    (for [child children]
      (render-category child (->> (sql/format {:select [:*]
                                               :from   [:categories]
                                               :where  [:= :parent (:categories/id child)]})
                                  (jdbc/execute! @cdb/db))))]])

(defn- render []
  (cutils/gen-page "chef - Admin"
                   [:h1 "chef - Admin"]
                   [:h2 "Kategorien"]
                   ;;TODO: add delete icons to non-root categories
                   [:ul
                    (render-category nil
                                     (->> (sql/format {:select [:*]
                                                       :from   [:categories]
                                                       :where  [:is :parent :null]})
                                          (jdbc/execute! @cdb/db)))]
                   [:h2 "Rezepte"]
                   [:i "Coming soon..."]))

(defn handler [req]
  (let [access-token (get-in req [:oauth2/access-tokens :auth])
        resp (-> (render)
                 html/html
                 str
                 ruresp/response)]
    (if (some? access-token)
      (assoc resp :session (assoc (:session req) :oauth-token access-token))
      (cutils/auth-only req resp))))