~sjm/redwood

ref: 8739a8f9f85d3ee990c60f9a5457c9ac534ada1e redwood/src/redwood/events.cljs -rw-r--r-- 4.6 KiB
8739a8f9Sam Marshall add timestamped journal 9 months ago
                                                                                
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(ns redwood.events
  (:require [re-frame.core :as rf]
            [redwood.data.app :as app]
            [redwood.data.time :as dt]
            [redwood.data.journal :as journal]
            [cljs-time.core :as t]))

(defn store-set-current-time
  [store time]
  (->> time
       dt/time->string
       (assoc store :current-time)))

(rf/reg-event-fx ::init
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} _]
                   (let [state (cond
                                 (not (nil? store)) (assoc 
                                                      (app/initial-state) 
                                                      ::app/current-time 
                                                      (dt/string->time 
                                                        (:current-time store)))
                                 :else (app/initial-state))]
                    {:db state
                      :store (assoc store :current-time (-> state
                                                            ::app/current-time
                                                            dt/time->string))})))
(rf/reg-event-fx ::add-time
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ time]]
                   (let [new-time (-> db
                                     ::app/current-time
                                     (t/plus time))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)}))) 
                            
                    
(rf/reg-event-fx ::set-year
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ year]]
                   (let [new-time (-> db
                                     ::app/current-time
                                     (dt/set-year year))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)})))

(rf/reg-event-fx ::set-month
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ month]]
                   (let [new-time (-> db
                                     ::app/current-time
                                     (dt/set-month month))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)})))

(rf/reg-event-fx ::set-day
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ day]]
                   (let [new-time (-> db
                                      ::app/current-time
                                      (dt/set-day day))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)})))

(rf/reg-event-fx ::set-hour
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ hour]]
                   (let [new-time (-> db
                                      ::app/current-time
                                      (dt/set-hour hour))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)})))

(rf/reg-event-fx ::set-minute
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ minute]]
                   (let [new-time (-> db
                                      ::app/current-time
                                      (dt/set-minute minute))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)})))

(rf/reg-event-fx ::set-second
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [store db]} [_ second]]
                   (let [new-time (-> db
                                      ::app/current-time
                                      (dt/set-second second))]
                     {:db (assoc db ::app/current-time new-time)
                      :store (store-set-current-time store new-time)})))
                                 
(rf/reg-event-db ::ui-toggle-top-bar
                 (fn [db _]
                   (let [current (-> db ::app/ui ::app/top-bar-open)]
                     (assoc-in db [::app/ui ::app/top-bar-open] (not current)))))

(rf/reg-event-db ::add-journal-entry-now
                 (fn [db [_ contents]]
                   (let [now (-> db ::app/current-time)]
                     (update db ::app/journal (fn [entries]
                                                (into [(journal/create-entry now contents)]
                                                      entries))))))