From 82754fe39f84391ab648dd9167304a439d0a9d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Schl=C3=BCter?= Date: Mon, 11 Jun 2018 19:53:06 +0200 Subject: [PATCH] Automatically hide notifications after a while --- src/cljs/airsonic_ui/events.cljs | 23 ++++++++++++++++------- test/cljs/airsonic_ui/events_test.cljs | 9 ++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/cljs/airsonic_ui/events.cljs b/src/cljs/airsonic_ui/events.cljs index 37b089e..791439a 100644 --- a/src/cljs/airsonic_ui/events.cljs +++ b/src/cljs/airsonic_ui/events.cljs @@ -200,19 +200,28 @@ ;; user messages +(def notification-duration + {:info 2500 + :error 10000}) + (defn show-notification "Displays an informative message to the user" - [db [_ level message]] - (let [id (.now js/performance)] + [fx [_ level message]] + (let [id (.now js/performance) + hide-later (fn [level] + [{:ms (get notification-duration level) + :dispatch [:notification/hide id]}])] (if (nil? message) (let [message level level :info] - (assoc-in db [:notifications id] {:level level - :message message})) - (assoc-in db [:notifications id] {:level level - :message message})))) + (-> (assoc-in fx [:db :notifications id] {:level level + :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-db +(re-frame/reg-event-fx :notification/show show-notification) diff --git a/test/cljs/airsonic_ui/events_test.cljs b/test/cljs/airsonic_ui/events_test.cljs index 9b0bfab..adf1f4d 100644 --- a/test/cljs/airsonic_ui/events_test.cljs +++ b/test/cljs/airsonic_ui/events_test.cljs @@ -51,8 +51,8 @@ (testing "remembering has no effect" (is (nil? (events/try-remember-user {} [:_])))))) -(defn- first-notification [db] - (-> (:notifications db) vals first)) +(defn- first-notification [fx] + (-> (get-in fx [:db :notifications]) vals first)) (deftest api-interaction (testing "Should show an error notification when airsonic responds with an error" @@ -77,4 +77,7 @@ id (-> (:notifications state) keys first)] - (is (empty? (:notifications (events/hide-notification state [:_ id]))))))) + (is (empty? (:notifications (events/hide-notification state [:_ id])))))) + (testing "Should automatically remove a message after a while" + (let [fx (events/show-notification {} [:_ :info "This is a notification"])] + (is (= :notification/hide (-> (:dispatch-later fx) first :dispatch first))))))