mirror of
https://github.com/heyarne/airsonic-ui.git
synced 2026-05-07 02:33:39 +02:00
Sketch out method to associate routes with api data
This commit is contained in:
parent
2ccf6b3f4c
commit
eb59e97e96
2 changed files with 38 additions and 41 deletions
|
|
@ -2,44 +2,18 @@
|
|||
(:require [re-frame.core :as re-frame]
|
||||
[ajax.core :as ajax]
|
||||
[airsonic-ui.routes :as routes]
|
||||
[airsonic-ui.config :as config]
|
||||
[airsonic-ui.db :as db]
|
||||
[clojure.string :as string]))
|
||||
[airsonic-ui.api :as api]))
|
||||
|
||||
;; TODO:
|
||||
|
||||
;; api related functions
|
||||
|
||||
(defn ^:private uri-escape [s]
|
||||
(js/encodeURIComponent s))
|
||||
|
||||
(defn ^:private api-url [endpoint params]
|
||||
(let [query (->> (assoc params
|
||||
:f "json"
|
||||
:c "airsonic-ui-cljs"
|
||||
:v "1.15.0")
|
||||
(map (fn [[k v]]
|
||||
(str (uri-escape (name k)) "=" (uri-escape v))))
|
||||
(string/join "&"))]
|
||||
(str config/server "/rest/" endpoint "?" query)))
|
||||
|
||||
(defn ^:private api-error?
|
||||
"We need to look at the message body because the subsonic api always responds
|
||||
with status 200"
|
||||
[response]
|
||||
(= "failed" (-> response :subsonic-response :status)))
|
||||
|
||||
(defn ^:private error-message
|
||||
[response]
|
||||
(let [{:keys [code message]} (-> response :subsonic-response :error)]
|
||||
(str "Code " code ": " message)))
|
||||
;; 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]]
|
||||
{:db (update db :active-requests inc)
|
||||
:http-xhrio {:method :get
|
||||
:uri (api-url "ping" {:u user :p pass})
|
||||
:uri (api/url "ping" {:u user :p pass})
|
||||
:response-format (ajax/json-response-format {:keywords? true})
|
||||
:on-success [::auth-success user pass]
|
||||
:on-failure [::api-failure]}}))
|
||||
|
|
@ -54,22 +28,23 @@
|
|||
{::routes/set-credentials login
|
||||
:db (-> (update db :active-requests #(max (dec %) 0))
|
||||
(assoc :login login))
|
||||
:dispatch-n (list [::start-page]
|
||||
[::api-request "getAlbumList2" :albumList2 {:type "recent"}])})))
|
||||
:dispatch [::logged-in]})))
|
||||
|
||||
;; we do this in two steps to make sure the credentials are set once we navigate
|
||||
(re-frame/reg-event-fx
|
||||
::start-page
|
||||
::logged-in
|
||||
(fn [_ _]
|
||||
{::routes/navigate [::routes/main]}))
|
||||
|
||||
;; 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
|
||||
:api-request
|
||||
(fn [{:keys [db]} [_ endpoint k params]]
|
||||
{:http-xhrio {:method :get
|
||||
:uri (api-url endpoint (merge params (:login db)))
|
||||
:uri (api/url endpoint (merge params (:login db)))
|
||||
:response-format (ajax/json-response-format {:keywords? true})
|
||||
:on-success [::api-success k]
|
||||
:on-failure [::api-failure]}}))
|
||||
|
|
@ -89,18 +64,20 @@
|
|||
|
||||
;; routing
|
||||
|
||||
(re-frame/reg-event-db
|
||||
(re-frame/reg-event-fx
|
||||
::routes/navigation
|
||||
(fn [db [_ route params query]]
|
||||
(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
|
||||
(assoc db :current-route [route params query])))
|
||||
(println "routes/route-data" (routes/data-for route params query))
|
||||
{:db (assoc db :current-route [route params query])
|
||||
:dispatch (routes/data-for route params query)}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::routes/unauthorized
|
||||
(fn [fx _]
|
||||
;; log out on 403
|
||||
{:navigate [routes/default-route]
|
||||
{::routes/navigate [routes/default-route]
|
||||
::routes/unset-credentials nil
|
||||
:db db/default-db}))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue