1
0
Fork 0
mirror of https://github.com/heyarne/airsonic-ui.git synced 2026-05-06 18:33:38 +02:00
airsonic-ui/src/airsonic_ui/events.cljs
Arne Schlüter 8d24c1b42a Put on some lipgloss
commit 9fa1a611e2fa093819b332791c28783a4f92a6dc
Author: Arne Schlüter <arne@schlueter.is>
Date:   Fri May 18 00:00:31 2018 +0200

    Add album previews

commit 317a6632b898039e370e3f8d52627e08a8c8186c
Author: Arne Schlüter <arne@schlueter.is>
Date:   Fri May 18 00:00:06 2018 +0200

    Fix cover art url

commit 0ba09903b96f5241853f003f679c0f407243f12a
Author: Arne Schlüter <arne@schlueter.is>
Date:   Thu May 17 22:59:23 2018 +0200

    Add bulma breadcrumbs

commit d21c7c8acc802101ff8ec096d5c6fad90e4f8ea7
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed May 16 18:55:45 2018 +0200

    Add basic styling to bottom bar

commit 23b37984ca9e8af84767e073492a42bf6c5924ea
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed May 16 18:21:27 2018 +0200

    Add retina version of cover component

commit d86a44bc5f7b7472f0084c47b691b0f7d151f497
Author: Arne Schlüter <arne@schlueter.is>
Date:   Wed May 16 18:20:47 2018 +0200

    Add info about server address to README

commit 4114581c259e17e3d0342755124e4fa56cd5dd3a
Author: Arne Schlüter <arne@schlueter.is>
Date:   Tue May 8 11:51:07 2018 +0200

    Add cover image

commit e6cb0745b366cbce3c25f225d8e008f12fcaae8b
Author: Arne Schlüter <arne@schlueter.is>
Date:   Tue May 8 10:49:26 2018 +0200

    Move views to their own namespaces

commit 5c24a47cc07b347beedd972e32ec145348a82d65
Author: Arne Schlüter <arne@schlueter.is>
Date:   Mon May 7 18:10:00 2018 +0200

    Add login styling

commit e04e0505f684d99316bdb3e875c403af2c28c127
Author: Arne Schlüter <arne@schlueter.is>
Date:   Sun May 6 11:19:08 2018 +0200

    Add gh-pages deploy script

commit e1a4cb4bb646def28989100f2084990863160dd9
Author: Arne Schlüter <arne@schlueter.is>
Date:   Sun May 6 00:43:51 2018 +0200

    Add bulma
2018-05-28 12:29:42 +02:00

147 lines
4.8 KiB
Clojure

(ns airsonic-ui.events
(:require [re-frame.core :as re-frame]
[ajax.core :as ajax]
[airsonic-ui.routes :as routes]
[airsonic-ui.db :as db]
[airsonic-ui.api :as api]
[day8.re-frame.tracing :refer-macros [fn-traced]])) ; <- useful to debug handlers
;; this is where all of the event handling takes place; the names put the events into
;; the following categories:
;; ::events/something-happening -> relevant to only this app
;; :single-colon/something -> coming from external sources (e.g. :audio/... or :routes/...) that are potentially reusable
;; database reset / init
(re-frame/reg-event-db
::initialize-db
(fn [_]
db/default-db))
;; this is called with user and password to try and see if the credentials are
;; correct; if yes, ::auth-success will be fired
(re-frame/reg-event-fx
::authenticate
(fn [{:keys [db]} [_ user pass server]]
{:db (-> (update db :active-requests inc)
(assoc :server server))
:http-xhrio {:method :get
:uri (api/url server "ping" {:u user :p pass})
:response-format (ajax/json-response-format {:keywords? true})
:on-success [::auth-success user pass]
:on-failure [::api-failure]}}))
;; TODO: Test that credentials are associated
(re-frame/reg-event-fx
::auth-success
(fn [{:keys [db]} [_ user pass response]]
;; TODO: Handle failures differently
(let [login {:u user :p pass}]
{:routes/set-credentials login
:db (-> (update db :active-requests #(max (dec %) 0))
(assoc :login login))
:dispatch [::logged-in]})))
;; TODO: We have to find another solution for this once we have routes that
;; don't require a login but have the bottom controls
(re-frame/reg-fx
:show-nav-bar
(fn [_]
(.. js/document -documentElement -classList (add "has-navbar-fixed-bottom"))))
;; we do this in two steps to make sure the credentials are set once we navigate
(re-frame/reg-event-fx
::logged-in
(fn [_ _]
{:routes/navigate [::routes/main]
:show-nav-bar nil}))
;; TODO: Test that credentials are actually taken
;; TODO: Move these in the future? events.cljs should just do wiring. We could
;; implement api.cljs as a completely independent module.
(re-frame/reg-event-fx
:api-request
(fn [{:keys [db]} [_ endpoint k params]]
{:http-xhrio {:method :get
:uri (api/url (:server db) endpoint (merge params (:login db)))
:response-format (ajax/json-response-format {:keywords? true})
:on-success [::api-success k]
:on-failure [::api-failure]}}))
(re-frame/reg-event-db
::api-success
(fn [db [_ k response]]
; we "unwrap" the responses
(assoc db :response (-> response :subsonic-response k))))
(re-frame/reg-event-db
::api-failure
(fn [db event]
(println "api call gone bad; CORS headers missing? check for :status 0" event)
db))
;; musique
; TODO: Make play, next and previous a bit prettier and more DRY
(re-frame/reg-event-fx
; sets up the db, starts to play a song and adds the rest to a playlist
::play-songs
(fn [{:keys [db]} [_ songs song]]
{:play-song (api/song-url (:server db) (:login db) song)
:db (-> db
(assoc-in [:currently-playing :item] song)
(assoc-in [:currently-playing :playlist] songs))}))
(re-frame/reg-event-fx
::next-song
(fn [{:keys [db]} _]
(let [playlist (-> db :currently-playing :playlist)
current (-> db :currently-playing :item)
next (first (rest (drop-while #(not= % current) playlist)))]
(when next
{:play-song (api/song-url (:server db) (:login db) next)
:db (assoc-in db [:currently-playing :item] next)}))))
(re-frame/reg-event-fx
::previous-song
(fn [{:keys [db]} _]
(let [playlist (-> db :currently-playing :playlist)
current (-> db :currently-playing :item)
previous (last (take-while #(not= % current) playlist))]
(when previous
{:play-song (api/song-url (:server db) (:login db) previous)
:db (assoc-in db [:currently-playing :item] previous)}))))
(re-frame/reg-event-fx
::toggle-play-pause
(fn [_ _]
{:toggle-play-pause nil}))
(re-frame/reg-event-db
:audio/update
(fn [db [_ status]]
; we receive this from the player once it's playing
(assoc-in db [:currently-playing :status] status)))
;; routing
(re-frame/reg-event-fx
:routes/navigation
(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
{:db (assoc db :current-route [route params query])
:dispatch (routes/route-data route params query)}))
(re-frame/reg-event-fx
:routes/unauthorized
(fn [fx _]
;; log out on 403
{:routes/navigate [routes/default-route]
:routes/unset-credentials nil
:db db/default-db}))