From 58176adffca82ac4f91002240964490ccbda0668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Schl=C3=BCter?= Date: Wed, 25 Apr 2018 14:14:17 +0200 Subject: [PATCH] Implement artist view --- shadow-cljs.edn | 2 +- src/airsonic_ui/routes.cljs | 12 ++++++--- src/airsonic_ui/views.cljs | 52 +++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/shadow-cljs.edn b/shadow-cljs.edn index 0ad2a88..c645180 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -11,7 +11,7 @@ [day8.re-frame/re-frame-10x "0.3.2-react16"] [day8.re-frame/tracing "0.5.1"] ;; for CIDER - [cider/cider-nrepl "0.16.0"] + [cider/cider-nrepl "0.16.0-snapshot"] [refactor-nrepl "2.3.1"]] :builds diff --git a/src/airsonic_ui/routes.cljs b/src/airsonic_ui/routes.cljs index e183ada..c766785 100644 --- a/src/airsonic_ui/routes.cljs +++ b/src/airsonic_ui/routes.cljs @@ -7,15 +7,15 @@ (def router (r/router [["/" ::login] ["/hello" ::main] - ["/album/:id" ::album-view] - ["/artist/:id" ::artist-view]])) + ["/artist/:id" ::artist-view] + ["/album/:id" ::album-view]])) ; use this in views to construct a url (defn url-for [k params] (str "#" (r/resolve router k params))) ; which routes need valid login credentials? -(def protected-routes #{::main ::album-view}) +(def protected-routes #{::main ::artist-view ::album-view}) ; which data should be requested for which route? can either be a vector or a function returning a vector @@ -29,9 +29,13 @@ [route-id params query] [:api-request "getAlbumList2" :albumList2 {:type "recent"}]) +(defmethod route-data ::artist-view + [route-id params query] + [:api-request "getArtist" :artist (select-keys params [:id])]) + (defmethod route-data ::album-view [route-id params query] - [:api-request "getAlbum" :album {:id (:id params)}]) + [:api-request "getAlbum" :album (select-keys params [:id])]) ;; shouldn't need to change anything below diff --git a/src/airsonic_ui/views.cljs b/src/airsonic_ui/views.cljs index 5438877..49ae0e8 100644 --- a/src/airsonic_ui/views.cljs +++ b/src/airsonic_ui/views.cljs @@ -38,7 +38,29 @@ [:div [:button {:on-click #(dispatch [::events/authenticate @user @pass @server])} "Submit"]]]))) -;; album list (start page) +;; single album + +(defn song-item [songs song] + (let [artist-id (:artistId song)] + [:div + [:a + (when artist-id {:href (routes/url-for ::routes/artist-view {:id artist-id})}) + (:artist song)] + " - " + [:a + {:href "#" :on-click (fn [e] + (.preventDefault e) + (dispatch [::events/play-songs songs song]))} + (:title song)]])) + +(defn album-detail [content] + [:div + [:h2 (str (:artist content) " - " (:name content))] + (let [songs (:song content)] + [:ul (for [[idx song] (map-indexed vector songs)] + [:li {:key idx} [song-item songs song]])])]) + +;; single artist (defn album-item [album] (let [{:keys [artist artistId name coverArt year id]} album] @@ -49,29 +71,20 @@ ;; link to album [:a {:href (routes/url-for ::routes/album-view {:id id})} name] (when year (str " (" year ")"))])) -;; TODO: album-list shouldn't know about the structure of content and should just get a list -(defn album-list [content] +(defn artist-detail [content] [:div - [:h2 (str "Recently played")] + [:h2 (:name content)] + [:p (:albumCount content) " items"] [:ul (for [[idx album] (map-indexed vector (:album content))] [:li {:key idx} [album-item album]])]]) -;; single album +;; TODO: album-list shouldn't know about the structure of content and should just get a list -(defn song-item [songs song] - [:div (str (:artist song) " - ") - [:a - {:on-click #(dispatch [::events/play-songs songs song])} - (:title song)]]) - -(defn song-list [songs] - [:ul (for [[idx song] (map-indexed vector songs)] - [:li {:key idx} [song-item songs song]])]) - -(defn album-detail [content] +(defn most-recent [content] [:div - [:h2 (str (:artist content) " - " (:name content))] - [song-list (:song content)]]) + [:h2 "Recently played"] + [:ul (for [[idx album] (map-indexed vector (:album content))] + [:li {:key idx} [album-item album]])]]) ;; currently playing / coming next / audio controls... @@ -104,7 +117,8 @@ [:div [:span (str "Currently logged in as " (:u login))] (case route - ::routes/main [album-list content] + ::routes/main [most-recent content] + ::routes/artist-view [artist-detail content] ::routes/album-view [album-detail content]) [:a {:on-click #(dispatch [::events/initialize-db]) :href "#"} "Logout"] [bottom-bar]]))