From 491c172ae73f2b081a0973b311378a05aede9e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Schl=C3=BCter?= Date: Sun, 22 Apr 2018 01:00:40 +0200 Subject: [PATCH] Make code a bit nicer to read / more idiomatic in some places --- src/airsonic_ui/events.cljs | 4 ++-- src/airsonic_ui/routes.cljs | 37 ++++++++++++++++++------------------- src/airsonic_ui/views.cljs | 13 ++++--------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/airsonic_ui/events.cljs b/src/airsonic_ui/events.cljs index f6a3a65..25952d2 100644 --- a/src/airsonic_ui/events.cljs +++ b/src/airsonic_ui/events.cljs @@ -91,9 +91,9 @@ (fn [{:keys [db]} [_ route params query]] ;; all the naviagation logic is in routes.cljs; all we need to do here ;; is say what actually happens once we've navigated succesfully - (println "routes/route-data" (routes/data-for route params query)) + (println "routes/route-data" (routes/route-data route params query)) {:db (assoc db :current-route [route params query]) - :dispatch (routes/data-for route params query)})) + :dispatch (routes/route-data route params query)})) (re-frame/reg-event-fx ::routes/unauthorized diff --git a/src/airsonic_ui/routes.cljs b/src/airsonic_ui/routes.cljs index 4eac91d..97f5fa5 100644 --- a/src/airsonic_ui/routes.cljs +++ b/src/airsonic_ui/routes.cljs @@ -18,21 +18,20 @@ (def protected-routes #{::main ::album-view}) ; which data should be requested for which route? can either be a vector or a function returning a vector -(def route-data - {::main [:api-request "getAlbumList2" :albumList2 {:type "recent"}] - ::album-view (fn [route-id params _] - [:api-request "getAlbum" :album {:id (:id params)}])}) +(defmulti route-data + "Returns the events that take care of correct data being fetched." + (fn [route-id & _] route-id)) -(defn data-for - "Wrapper around route-data so we can call it like a function no matter whether - the value associated with the route key is a function or not." - [route params query] - (if-let [route-data' (route-data route)] - (if (vector? route-data') - route-data' - (route-data' route params query)) - [])) +(defmethod route-data :default [route-id params query] []) ; no data + +(defmethod route-data ::main + [route-id params query] + [:api-request "getAlbumList2" :albumList2 {:type "recent"}]) + +(defmethod route-data ::album-view + [route-id params query] + [:api-request "getAlbum" :album {:id (:id params)}]) ;; shouldn't need to change anything below @@ -40,17 +39,17 @@ ;; holding credentials, which is necessary to restrict certain routes, and the ;; last one is used for actual navigation -(def login (atom nil)) +(def credentials (atom nil)) (re-frame/reg-fx ::set-credentials - (fn [credentials] - (reset! login credentials))) + (fn [credentials'] + (reset! credentials credentials'))) (re-frame/reg-fx ::unset-credentials - (fn [credentials] - (reset! login nil))) + (fn [] + (reset! credentials nil))) (re-frame/reg-fx ::navigate @@ -59,7 +58,7 @@ (r/navigate! router route-id params query))) (defn can-access? [route] - (or (not (protected-routes route)) @login)) + (or (not (protected-routes route)) @credentials)) (defn on-navigate [route-id params query] diff --git a/src/airsonic_ui/views.cljs b/src/airsonic_ui/views.cljs index 6dd69c9..6d81a0f 100644 --- a/src/airsonic_ui/views.cljs +++ b/src/airsonic_ui/views.cljs @@ -38,11 +38,8 @@ (defn album-list [content] [:div [:h2 (str "Recently played")] - [:ul - (map-indexed - (fn [idx album] - [:li {:key idx} [album-item album]]) - (:album content))]]) + [:ul (for [[idx album] (map-indexed vector (:album content))] + [:li {:key idx} [album-item album]])]]) ;; single album @@ -53,10 +50,8 @@ (:title song)]]) (defn song-list [songs] - [:ul - (map-indexed - (fn [idx song] [:li {:key idx} [song-item song]]) - songs)]) + [:ul (for [[idx song] (map-indexed vector songs)] + [:li {:key idx} [song-item song]])]) (defn album-detail [content] [:div