blob: 89a24622908d115e48f9ed764e7f8d30ea800567 (
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
(ns chef.frontend.admin
(:require [chef.utils :as cutils]
[hiccup2.core :as html]
[ring.util.response :as ruresp]
[chef.logic.categories :as clcategories]
[chef.logic.recipes :as clrecipes]))
(defn- render-category [data children]
[:li
[:p {:style {:display :inline-block}}
(if (pos? (:categories/id data))
[:input {:type :text :placeholder "Name"
:value (:categories/name data)
:name "name"
:hx-post (str "/api/admin/category/" (:categories/id data))
:hx-trigger "change"}]
"Startseite")]
(when (or (neg? (:categories/id data))
(->> (:categories/id data)
clcategories/find-categories-with-parent
count
pos?))
(list [: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/category/" (:categories/id data))
:hx-trigger "change"}]))
[:img {:src "/static/icons/plus.svg" :height "30em"
:style {:vertical-align :middle
:margin-left "1em"}
:hx-post (str "/api/admin/category/create/"
(when (pos? (:categories/id data)) (str "?parent=" (:categories/id data))))
:hx-swap "none"}]
(when (pos? (:categories/id data))
[:img {:src "/static/icons/trash.svg" :height "30em"
:style {:vertical-align :middle
:margin-left "1em"}
:hx-delete (str "/api/admin/category/" (:categories/id data))
:hx-swap "none"}])
[:ul
(for [child children]
(->> (:categories/id child)
clcategories/find-categories-with-parent
(render-category child)))]])
(defn- render-recipe-table-row [recipe]
(let [tr-id (str "recipe-" (:recipes/id recipe))]
[:tr {:id tr-id}
[:td
[:p (:recipes/title recipe)]]
[:td
(let [category (clcategories/get-category (:recipes/category recipe))]
[:p (clcategories/generate-path category)])]
[:td
[:div
[:button {:class ["button" "primary"]
:onclick (str "window.open(\"/admin/recipe-editor/"
(:recipes/id recipe)
"\", \"\", \"width=900,height=900\")")}
"Bearbeiten"]
[:button {:class ["button error"]
:hx-trigger "click"
:hx-swap :none
:hx-delete (str "/api/admin/recipe/" (:recipes/id recipe))}
"Löschen"]]]]))
(defn- render []
(cutils/gen-page "chef - Admin"
[:div {:style {:margin-left "1em"}}
[:h1 "chef - Admin"]
[:h2 "Kategorien"]
[:ul
(render-category (clcategories/get-category -1)
(clcategories/find-categories-with-parent -1))]
[:h2 "Rezepte"]
[:table
[:tr
[:th "Titel"]
[:th "Kategorie"]
[:th "Aktionen"]]
(for [recipe (clrecipes/get-all-recipes)]
(render-recipe-table-row recipe))]
[:button {:class "button primary"
:hx-trigger "click"
:hx-swap :none
:hx-post "/api/admin/recipe/create/"}
"Rezept erstellen"]]))
(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))))
|