1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-06 18:33:38 +02:00

Make code a bit nicer to read / more idiomatic in some places

This commit is contained in:
Arne Schlüter 2018-04-22 01:00:40 +02:00
commit 491c172ae7
3 changed files with 26 additions and 32 deletions

View file

@ -91,9 +91,9 @@
(fn [{:keys [db]} [_ route params query]] (fn [{:keys [db]} [_ route params query]]
;; all the naviagation logic is in routes.cljs; all we need to do here ;; all the naviagation logic is in routes.cljs; all we need to do here
;; is say what actually happens once we've navigated succesfully ;; 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]) {: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 (re-frame/reg-event-fx
::routes/unauthorized ::routes/unauthorized

View file

@ -18,21 +18,20 @@
(def protected-routes #{::main ::album-view}) (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 ; 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 (defmethod route-data :default [route-id params query] []) ; no data
"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." (defmethod route-data ::main
[route params query] [route-id params query]
(if-let [route-data' (route-data route)] [:api-request "getAlbumList2" :albumList2 {:type "recent"}])
(if (vector? route-data')
route-data' (defmethod route-data ::album-view
(route-data' route params query)) [route-id params query]
[])) [:api-request "getAlbum" :album {:id (:id params)}])
;; shouldn't need to change anything below ;; shouldn't need to change anything below
@ -40,17 +39,17 @@
;; holding credentials, which is necessary to restrict certain routes, and the ;; holding credentials, which is necessary to restrict certain routes, and the
;; last one is used for actual navigation ;; last one is used for actual navigation
(def login (atom nil)) (def credentials (atom nil))
(re-frame/reg-fx (re-frame/reg-fx
::set-credentials ::set-credentials
(fn [credentials] (fn [credentials']
(reset! login credentials))) (reset! credentials credentials')))
(re-frame/reg-fx (re-frame/reg-fx
::unset-credentials ::unset-credentials
(fn [credentials] (fn []
(reset! login nil))) (reset! credentials nil)))
(re-frame/reg-fx (re-frame/reg-fx
::navigate ::navigate
@ -59,7 +58,7 @@
(r/navigate! router route-id params query))) (r/navigate! router route-id params query)))
(defn can-access? [route] (defn can-access? [route]
(or (not (protected-routes route)) @login)) (or (not (protected-routes route)) @credentials))
(defn on-navigate (defn on-navigate
[route-id params query] [route-id params query]

View file

@ -38,11 +38,8 @@
(defn album-list [content] (defn album-list [content]
[:div [:div
[:h2 (str "Recently played")] [:h2 (str "Recently played")]
[:ul [:ul (for [[idx album] (map-indexed vector (:album content))]
(map-indexed [:li {:key idx} [album-item album]])]])
(fn [idx album]
[:li {:key idx} [album-item album]])
(:album content))]])
;; single album ;; single album
@ -53,10 +50,8 @@
(:title song)]]) (:title song)]])
(defn song-list [songs] (defn song-list [songs]
[:ul [:ul (for [[idx song] (map-indexed vector songs)]
(map-indexed [:li {:key idx} [song-item song]])])
(fn [idx song] [:li {:key idx} [song-item song]])
songs)])
(defn album-detail [content] (defn album-detail [content]
[:div [:div