From 38eea1c8c994d0f2b1c4d123bb76a1b4c80bf55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Schl=C3=BCter?= Date: Tue, 18 Sep 2018 11:23:18 +0200 Subject: [PATCH] Hide loading spinner when an error occured --- src/cljs/airsonic_ui/subs.cljs | 25 +++++++++++++++++++++---- test/cljs/airsonic_ui/subs_test.cljs | 4 +++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/cljs/airsonic_ui/subs.cljs b/src/cljs/airsonic_ui/subs.cljs index f8fb2ee..f70069a 100644 --- a/src/cljs/airsonic_ui/subs.cljs +++ b/src/cljs/airsonic_ui/subs.cljs @@ -2,17 +2,34 @@ (:require [re-frame.core :refer [reg-sub subscribe]] [airsonic-ui.api.helpers :as api] [airsonic-ui.helpers :refer [kebabify]] - [debux.cs.core :refer-macros [dbg]] [clojure.string :as str])) +;; +;; app initialization +;; + +;; TODO: Computation and extaction is mixed; this could be simpler + +(defn- error-notifications [notifications] + (filter (fn [[_ n]] + (= :error (:level n))) notifications)) + +(defn- no-errors? [db] + (empty? (error-notifications (:notifications db)))) + +(defn- no-route? [db] + (empty? (:routes/current-route db))) + +(defn- no-credentials? [db] + (and (not (empty? (:credentials db))) + (not (get-in db [:credentials :verified?])))) + (defn is-booting? "The boot process starts with setting up routing and continues if we found previous credentials and ends when we receive a response from the server." [db _] ;; so either we don't have any credentials or they are not verified - (or (empty? (:routes/current-route db)) - (and (not (empty? (:credentials db))) - (not (get-in db [:credentials :verified?]))))) + (and (no-errors? db) (or (no-route? db) (no-credentials? db)))) (reg-sub ::is-booting? is-booting?) diff --git a/test/cljs/airsonic_ui/subs_test.cljs b/test/cljs/airsonic_ui/subs_test.cljs index 1fa04d1..af9f0e2 100644 --- a/test/cljs/airsonic_ui/subs_test.cljs +++ b/test/cljs/airsonic_ui/subs_test.cljs @@ -22,7 +22,9 @@ :credentials verified-credentials})))) (testing "Should be true when routing is not yet set up" (is (true? (is-booting? {:routes/current-route nil - :credentials verified-credentials})))))) + :credentials verified-credentials})))) + (testing "Should be false when an error occurred" + (is (false? (is-booting? (:db (events/show-notification {} [:_ :error "Something bad happened"])))))))) (deftest cover-images (let [credentials {:server "https://foo.bar"