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