aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/tubo/notifications/events.cljs
blob: 2ecbf2e0bda738b245de29a0dbae9ac2438bad04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns tubo.notifications.events
  (:require
   [re-frame.core :as rf]
   [nano-id.core :refer [nano-id]]))

(rf/reg-event-fx
 :notifications/add
 (fn [{:keys [db]} [_ data time]]
   (let [id (nano-id)
         updated-db (update db :notifications #(into [] (conj %1 %2))
                            (assoc data :id id))]
     {:db updated-db
      :fx (if (false? time)
              []
              [[:timeout {:id    id
                          :event [:notifications/remove id]
                          :time  (or time 2000)}]])})))

(rf/reg-event-db
 :notifications/remove
 (fn [db [_ id]]
   (update db :notifications #(remove (fn [notification]
                                        (= (:id notification) id)) %))))

(rf/reg-event-db
 :notifications/clear
 (fn [db _]
   (dissoc db :notifications)))