~sjm/redwood

ref: de4c8b31042635c5d5f635d9b8f4469b55933399 redwood/src/redwood/events.cljs -rw-r--r-- 5.5 KiB
de4c8b31Sam Marshall save journal to localstorage 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(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 [store-exists (not (nil? store))
                         journal (cond 
                                   store-exists (journal/string->journal 
                                                      (:journal store))
                                   :else [])
                         current-time (cond 
                                        store-exists (dt/string->time 
                                                       (:current-time store))
                                        :else "")
                         state (cond
                                 store-exists (let [with-time (assoc 
                                                                (app/initial-state)
                                                                ::app/current-time 
                                                                current-time)]
                                                (assoc with-time ::app/journal journal))
                                 :else (app/initial-state))]
                     {:db state
                      :store (cond store-exists store
                                   :else (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-fx ::add-journal-entry-now
                 [(rf/inject-cofx :store)]
                 (fn [{:keys [db store]} [_ contents]]
                   (let [now (-> db ::app/current-time)
                         entries (::app/journal db)
                         new-journal (into [(journal/create-entry now contents)] entries)]
                     {:store (assoc store :journal (journal/journal->string new-journal))
                      :db (assoc db ::app/journal new-journal)})))