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

Fix all the "unregistered effects handler for :event" errors, closes #25

This commit is contained in:
Arne Schlüter 2018-10-10 15:39:28 +02:00
commit 563cab9099
3 changed files with 31 additions and 36 deletions

View file

@ -24,13 +24,13 @@
(defn good-api-response (defn good-api-response
"Handles when the server responded. There could still be an error while "Handles when the server responded. There could still be an error while
processing the request on the server side which we have to account for." processing the request on the server side which we have to account for."
[fx [_ endpoint params response]] [{:keys [db]} [_ endpoint params response]]
(let [response-cache (cons :db (cache-path endpoint params))] (let [response-cache (cache-path endpoint params)]
(try (try
(assoc-in fx response-cache (api/unwrap-response response)) {:db (assoc-in db response-cache (api/unwrap-response response))}
(catch ExceptionInfo e (catch ExceptionInfo e
{:dispatch [:notification/show :error (api/error-msg e)] {:dispatch [:notification/show :error (api/error-msg e)]
:db (update-in fx response-cache dissoc :api/is-loading?)})))) :db (update-in db response-cache dissoc :api/is-loading?)}))))
(reg-event-fx :api/good-response good-api-response) (reg-event-fx :api/good-response good-api-response)

View file

@ -52,10 +52,10 @@
(defn user-login (defn user-login
"Gets called after the user clicked on the login button" "Gets called after the user clicked on the login button"
[cofx [_ user pass server]] [{:keys [db]} [_ user pass server]]
(let [credentials {:u user, :p pass, :server server, :verified? false}] (let [credentials {:u user, :p pass, :server server, :verified? false}]
(-> (assoc-in cofx [:db :credentials] credentials) {:db (assoc db :credentials credentials)
(assoc :dispatch [:credentials/send-authentication-request credentials])))) :dispatch [:credentials/send-authentication-request credentials]}))
(re-frame/reg-event-fx :credentials/user-login user-login) (re-frame/reg-event-fx :credentials/user-login user-login)
@ -63,30 +63,30 @@
"Tries to authenticate a user by requesting info about the given user, saving "Tries to authenticate a user by requesting info about the given user, saving
the credentials when the request was successful." the credentials when the request was successful."
[cofx [_ credentials]] [cofx [_ credentials]]
(assoc cofx :http-xhrio {:method :get {:http-xhrio {:method :get
:uri (api/url credentials "getUser" {:username (:u credentials)}) :uri (api/url credentials "getUser" {:username (:u credentials)})
:response-format (ajax/json-response-format {:keywords? true}) :response-format (ajax/json-response-format {:keywords? true})
:on-success [:credentials/authentication-response credentials] :on-success [:credentials/authentication-response credentials]
:on-failure [:api/failed-response]})) ; <- we don't need endpoint and params here because the response is not cached :on-failure [:api/failed-response]}}) ; <- we don't need endpoint and params here because the response is not cached
(re-frame/reg-event-fx :credentials/send-authentication-request authentication-request) (re-frame/reg-event-fx :credentials/send-authentication-request authentication-request)
(defn authentication-response (defn authentication-response
"Since we don't get real status codes, we have to look into the server's "Since we don't get real status codes, we have to look into the server's
response and see whether we actually sent the correct credentials" response and see whether we actually sent the correct credentials"
[fx [_ credentials response]] [_ [_ credentials response]]
(assoc fx :dispatch (if (api/is-error? response) {:dispatch (if (api/is-error? response)
[:credentials/authentication-failure response] [:credentials/authentication-failure response]
[:credentials/authentication-success credentials response]))) [:credentials/authentication-success credentials response])})
(re-frame/reg-event-fx :credentials/authentication-response authentication-response) (re-frame/reg-event-fx :credentials/authentication-response authentication-response)
(defn authentication-failure (defn authentication-failure
"Removes all stored credentials and displays potential api errors to the user" "Removes all stored credentials and displays potential api errors to the user"
[fx [_ response]] [{:keys [db store]} [_ response]]
(-> (assoc fx :dispatch [:notification/show :error (api/error-msg (api/->exception response))]) {:dispatch [:notification/show :error (api/error-msg (api/->exception response))]
(update :store dissoc :credentials) :store (dissoc store :credentials)
(update :db dissoc :credentials))) :db (dissoc db :credentials)})
(re-frame/reg-event-fx :credentials/authentication-failure authentication-failure) (re-frame/reg-event-fx :credentials/authentication-failure authentication-failure)
@ -151,20 +151,15 @@
(defn show-notification (defn show-notification
"Displays an informative message to the user" "Displays an informative message to the user"
[fx [_ level message]] [{:keys [db]} [_ level message]]
(let [id (.now js/performance) (let [id (.now js/performance)
hide-later (fn [level] ;; the level argument is optional; if it's not given, it defaults to :info
[{:ms (get notification-duration level) level' (if (nil? message) :info level)
:dispatch [:notification/hide id]}])] message' (if (nil? message) level message)]
(if (nil? message) {:db (assoc-in db [:notifications id] {:level level'
(let [message level :message message'})
level :info] :dispatch-later [{:ms (get notification-duration level)
(-> (assoc-in fx [:db :notifications id] {:level level :dispatch [:notification/hide id]}]}))
:message message})
(assoc :dispatch-later (hide-later level))))
(-> (assoc-in fx [:db :notifications id] {:level level
:message message})
(assoc :dispatch-later (hide-later level))))))
(re-frame/reg-event-fx :notification/show show-notification) (re-frame/reg-event-fx :notification/show show-notification)

View file

@ -117,7 +117,7 @@
;; an interceptor, we know that when handling the event (see ;; an interceptor, we know that when handling the event (see
;; below) the credentials aren't altered anymore ;; below) the credentials aren't altered anymore
credentials' (get-in context [:coeffects :db :credentials])] credentials' (get-in context [:coeffects :db :credentials])]
(println "calling do-navigation with" route credentials') #_(println "calling do-navigation with" route credentials')
(reset! credentials credentials') (reset! credentials credentials')
(apply r/navigate! router route) (apply r/navigate! router route)
(dissoc context :event))))) (dissoc context :event)))))
@ -130,7 +130,7 @@
(defn on-navigate (defn on-navigate
[route-id params query] [route-id params query]
(println "on-navigate is called" route-id params query credentials) #_(println "calling on-navigate with" route credentials')
(if (can-access? route-id) (if (can-access? route-id)
(re-frame/dispatch [:routes/did-navigate route-id params query]) (re-frame/dispatch [:routes/did-navigate route-id params query])
(re-frame/dispatch [:routes/unauthorized route-id params query]))) (re-frame/dispatch [:routes/unauthorized route-id params query])))