mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-07 10:43:39 +02:00
Merge feature/search
Squashed commit of the following:
commit 8a19df91f8daa1b791d40cc910947c94355a8d0d
Author: Arne Schlüter <arne@schlueter.is>
Date: Tue Aug 28 16:06:35 2018 +0200
Implement search UI (closes #19)
commit bf661dd25ec9f1d5569df88a8a87f94c1bc1b317
Author: Arne Schlüter <arne@schlueter.is>
Date: Tue Aug 28 11:09:46 2018 +0200
Re-add subscription for single endpoint and move helpers to a different location
This commit is contained in:
parent
d26decb2ff
commit
7653af5dd1
22 changed files with 236 additions and 49 deletions
15
src/cljs/airsonic_ui/components/search/events.cljs
Normal file
15
src/cljs/airsonic_ui/components/search/events.cljs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(ns airsonic-ui.components.search.events
|
||||
(:require [re-frame.core :refer [reg-event-fx reg-event-db]]
|
||||
[airsonic-ui.routes :as routes]))
|
||||
|
||||
(reg-event-db
|
||||
;; this is called on navigation and handled in routes.cljs; the reason is that
|
||||
;; when we're navigating to search?query=foo we don't have the term in our db.
|
||||
:search/restore-term-from-param
|
||||
(fn [db [_ term]]
|
||||
(assoc-in db [:search :term] term)))
|
||||
|
||||
(reg-event-fx
|
||||
:search/do-search
|
||||
(fn do-search [fx [_ term]]
|
||||
{:dispatch [:routes/do-navigation [::routes/search {} {:query term}]]}))
|
||||
5
src/cljs/airsonic_ui/components/search/subs.cljs
Normal file
5
src/cljs/airsonic_ui/components/search/subs.cljs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(ns airsonic-ui.components.search.subs
|
||||
(:require [re-frame.core :refer [reg-sub subscribe]]))
|
||||
|
||||
(reg-sub :search/current-term (fn current-term [db _]
|
||||
(get-in db [:search :term])))
|
||||
67
src/cljs/airsonic_ui/components/search/views.cljs
Normal file
67
src/cljs/airsonic_ui/components/search/views.cljs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
(ns airsonic-ui.components.search.views
|
||||
(:require [clojure.pprint :refer [pprint]]
|
||||
[re-frame.core :refer [dispatch subscribe]]
|
||||
[goog.functions :refer [debounce]]
|
||||
[airsonic-ui.routes :as routes :refer [url-for]]
|
||||
[airsonic-ui.views.song :as song]
|
||||
[airsonic-ui.views.cover :refer [card]]))
|
||||
|
||||
(defn form []
|
||||
(let [search-term @(subscribe [:search/current-term])
|
||||
throttled-search (debounce #(dispatch [:search/do-search (.. % -target -value)]) 100)]
|
||||
(fn []
|
||||
[:form {:on-submit #(.preventDefault %)}
|
||||
[:div.feld>p.control
|
||||
[:input.input {:on-change (fn [e]
|
||||
;; the event might be gone when we the dispatched
|
||||
;; function is fired, we need to persist it
|
||||
(.persist e)
|
||||
(throttled-search e))
|
||||
:default-value search-term
|
||||
:placeholder "Search"}]]])))
|
||||
|
||||
(defn artist-results [{:keys [artist]}]
|
||||
[:div.columns.is-multiline.is-mobile
|
||||
(for [[idx artist] (map-indexed vector artist)]
|
||||
(let [url #(url-for ::routes/artist-view (select-keys % [:id]))]
|
||||
^{:key idx} [:div.column.is-2
|
||||
[card artist
|
||||
:url-fn url
|
||||
:content [:div>a
|
||||
{:href (url artist), :title (:name artist)}
|
||||
(:name artist)]]]))])
|
||||
|
||||
(defn album-results [{:keys [album]}]
|
||||
[:div.columns.is-multiline.is-mobile
|
||||
(for [[idx album] (map-indexed vector album)]
|
||||
(let [url #(url-for ::routes/album-view (select-keys % [:id]))
|
||||
title (str (:name album) " (" (:artist album) ")")]
|
||||
^{:key idx} [:div.column.is-2 [card album
|
||||
:url-fn url
|
||||
:content [:div>a
|
||||
{:href (url album), :title title}
|
||||
title]]]))])
|
||||
|
||||
(defn song-results [{:keys [song]}]
|
||||
[song/listing song])
|
||||
|
||||
(defn results [{:keys [search]}]
|
||||
(let [term @(subscribe [:search/current-term])]
|
||||
[:div
|
||||
[:h2.title (str "Search results for \"" term "\"")]
|
||||
(if (empty? search)
|
||||
[:p "The server returned no results."]
|
||||
[:div.content
|
||||
(when-not (empty? (:artist search))
|
||||
[:section.section.is-small
|
||||
[:h3.subtitle.is-5 "Artists"]
|
||||
[artist-results search]])
|
||||
(when-not (empty? (:album search))
|
||||
[:section.section.is-small
|
||||
[:h3.subtitle.is-5 "Albums"]
|
||||
[album-results search]])
|
||||
(when-not (empty? (:song search))
|
||||
[:section.section.is-small
|
||||
[:h3.subtitle.is-5 "Songs"]
|
||||
[song-results search]])])
|
||||
[:pre (with-out-str (pprint search))]]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue