A src/redwood/components/journal/add_entry.cljs => src/redwood/components/journal/add_entry.cljs +18 -0
@@ 0,0 1,18 @@
+(ns redwood.components.journal.add-entry
+ (:require [re-frame.core :as rf]
+ [redwood.events :as e]
+ [redwood.queries :as q]))
+
+(defn add-entry []
+ [:form {:on-submit #(do
+ (.preventDefault %)
+ (let [entry (-> %
+ .-target
+ .-elements
+ (aget 0)
+ .-value)]
+ (rf/dispatch [::e/add-journal-entry-now entry])))}
+ ; dispatch an event to add the value to the store
+ [:input {:type "text"
+ :min-length 3
+ :placeholder "journal entry"}]])
A src/redwood/components/journal/display.cljs => src/redwood/components/journal/display.cljs +19 -0
@@ 0,0 1,19 @@
+(ns redwood.components.journal.display
+ (:require [re-frame.core :as rf]
+ [redwood.data.time :as t]
+ [redwood.queries :as q]
+ [redwood.data.journal :as j]))
+
+(defn display-entry [{:keys [::j/time ::j/contents]}]
+ [:div
+ [:div (t/time->string time)]
+ [:div contents]])
+
+(defn display-journal []
+ "Display the full journal"
+ (let [journal @(rf/subscribe [::q/journal])]
+ [:div
+ (map (fn [entry]
+ [display-entry entry])
+ journal)]))
+
M src/redwood/core.cljs => src/redwood/core.cljs +5 -1
@@ 9,6 9,8 @@
[redwood.events :as e]
[redwood.styles :as st]
[redwood.queries :as q]
+ [redwood.components.journal.add-entry :refer [add-entry]]
+ [redwood.components.journal.display :refer [display-journal]]
[redwood.components.date :refer [date]]
[redwood.components.top-bar :refer [top-bar]]
[redwood.components.top-bar.set-date :refer [set-date]]
@@ 35,7 37,9 @@
[:p {:style {:cursor "pointer"}
:on-click #(rf/dispatch [::e/add-time (t/seconds 30)])}
- "30s"]]]])
+ "30s"]]]
+ [add-entry]
+ [display-journal]])
(reg-co-fx!
M src/redwood/data/app.cljs => src/redwood/data/app.cljs +6 -1
@@ 2,6 2,7 @@
(:require [clojure.spec.alpha :as s]
[redwood.data.time :as dt]
[redwood.data.ui :as ui]
+ [redwood.data.journal :as journal]
[cljs-time.core :as t]))
(s/def ::current-time ::dt/current)
@@ 10,6 11,9 @@
(s/def ::ui
(s/keys :req [::top-bar-open]))
+(s/def ::journal
+ (s/coll-of ::journal/entry))
+
(s/def ::app-db
(s/keys :req [::current-time ::ui]))
@@ 17,4 21,5 @@
[]
{
::current-time (t/now)
- ::ui {::top-bar-open false}})
+ ::ui {::top-bar-open false}
+ ::journal []})
A src/redwood/data/journal.cljs => src/redwood/data/journal.cljs +16 -0
@@ 0,0 1,16 @@
+(ns redwood.data.journal
+ (:require [clojure.spec.alpha :as s]
+ [cljs-time.core :as t]))
+
+(s/def ::time t/date?)
+
+(s/def ::contents string?)
+
+(s/def ::entry
+ (s/keys :req [::time ::contents]))
+
+(defn create-entry
+ [time contents]
+ {::time time
+ ::contents contents})
+
M src/redwood/events.cljs => src/redwood/events.cljs +7 -1
@@ 2,6 2,7 @@
(: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
@@ 93,4 94,9 @@
(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))))))
M src/redwood/queries.cljs => src/redwood/queries.cljs +5 -0
@@ 3,6 3,10 @@
[cljs-time.core :as t]
[redwood.data.app :as app]))
+(rf/reg-sub ::journal
+ (fn [db _]
+ (::app/journal db)))
+
(rf/reg-sub ::ui
(fn [db _]
(::app/ui db)))
@@ 45,3 49,4 @@
:<- [::ui]
(fn [ui _]
(::app/top-bar-open ui)))
+