aboutsummaryrefslogtreecommitdiff
path: root/src/dionysus/web/admin/home/blacklist.clj
blob: 81ac39de23594b88aad3a206bcfb020dc0f52523 (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
104
105
106
(ns dionysus.web.admin.home.blacklist
  (:require [clojure.string :as cstr]
            [dionysus.blacklist :as dblacklist]
            [dionysus.spotify :as dspotify]
            [dionysus.web.utils :as dwutils]
            [ring.util.response :as ruresp]))

(defn- blacklist-table-item [index edited-item]
  (let [item (nth @dblacklist/blacklist index)
        editor? (= (str index) edited-item)]
    [:tr
     [:td (when-not editor? (cstr/upper-case (:type item)))]
     [:td (if editor?
            [:input {:type :text :name "link" :value (:url item)}]
            [:a {:href (:url item) :target "_blank"}
             (condp = (:type item)
               "track"
               (-> (:id item)
                   dspotify/get-track!
                   :name)

               "artist"
               (-> (:id item)
                   dspotify/get-artist!
                   :name)

               nil)])]
     (if editor?
       [:td
        [:img {:src        "/assets/icons/check.svg"
               :class      "icon"
               :hx-trigger "click"
               :hx-post    (str "/blacklist/change/" index)
               :hx-target  "#blacklist"
               :hx-swap    "outerHTML"
               :hx-include "previous input"}]
        [:img {:src        "/assets/icons/trash.svg"
               :class      "icon"
               :hx-trigger "click"
               :hx-delete  (str "/blacklist/delete/" index)
               :hx-target  "#blacklist"
               :hx-swap    "outerHTML"}]]
       [:td
        [:img {:src        "/assets/icons/edit.svg"
               :class      "icon"
               :hx-trigger "click"
               :hx-get     (str "/blacklist?editing=" index)
               :hx-target  "#blacklist"
               :hx-swap    "outerHTML"}]])]))

(defn render-blacklist-table [edited-item]
  [:table {:id "blacklist"}
   [:thead
    [:tr
     [:th "Typ"]
     [:th "Link"]
     [:th "Aktionen"]]]
   [:tbody
    (for [index (-> @dblacklist/blacklist
                    .length
                    range)]
      (blacklist-table-item index edited-item))
    [:tr
     [:td]
     [:td [:input {:type        :text
                   :placeholder "Link to track or artist"
                   :name        "link"}]]
     [:td [:img {:src        "/assets/icons/plus.svg"
                 :class      "icon"
                 :hx-trigger "click"
                 :hx-post    "/blacklist/add"
                 :hx-target  "#blacklist"
                 :hx-swap    "outerHTML"
                 :hx-include "previous input"}]]]]])

(defn handle [req]
  (-> (get-in req [:query-params "editing"])
      render-blacklist-table
      dwutils/render-html
      ruresp/response))

(defn handle-add [req]
  (-> req
      (get-in [:form-params "link"])
      dblacklist/add)
  (handle {}))

(defn- valid-index? [s]
  (and (string? s) (string? (re-matches #"[0-9][0-9]*" s))))

(defn handle-change [req]
  (let [index (get-in req [:path-params :index])
        link (get-in req [:form-params "link"])]
    (if (and (valid-index? index) (string? link))
      (do (-> index
              Integer/parseInt
              (dblacklist/change link))
          (handle {}))
      (ruresp/bad-request "Bad request."))))

(defn handle-delete [req]
  (let [index (get-in req [:path-params :index])]
    (if (valid-index? index)
      (do (dblacklist/delete (Integer/parseInt index))
          (handle {}))
      (ruresp/bad-request "Bad request."))))