~sjm/redwood

de4c8b31042635c5d5f635d9b8f4469b55933399 — Sam Marshall 8 months ago 8739a8f
save journal to localstorage
3 files changed, 58 insertions(+), 18 deletions(-)

M src/redwood/core.cljs
M src/redwood/data/journal.cljs
M src/redwood/events.cljs
M src/redwood/core.cljs => src/redwood/core.cljs +0 -1
@@ 52,4 52,3 @@
  (rf/dispatch-sync [::e/init])
  (rd/render [root]
             (.getElementById js/document "app")))


M src/redwood/data/journal.cljs => src/redwood/data/journal.cljs +24 -1
@@ 1,6 1,7 @@
(ns redwood.data.journal
  (:require [clojure.spec.alpha :as s]
            [cljs-time.core :as t]))
            [cljs-time.core :as t]
            [redwood.data.time :as dt]))

(s/def ::time t/date?)



@@ 14,3 15,25 @@
  {::time time
   ::contents contents})

(defn entry-js->clj
  [{:keys [:time :contents]}]
  (create-entry 
    (dt/string->time time)
    contents))

(defn journal->string 
  [journal]
  (->> journal
      (map (fn [{:keys [::time ::contents]}]
             {:time (dt/time->string time)
              :contents contents}))
      (clj->js)
      (.stringify js/JSON)))

(defn string->journal
  [string]
  (let [parse #(js->clj % :keywordize-keys true)]
    (->> string
        (.parse js/JSON)
        (parse)
        (map entry-js->clj))))

M src/redwood/events.cljs => src/redwood/events.cljs +34 -16
@@ 14,17 14,29 @@
(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)))
                   (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 (assoc store :current-time (-> state
                                                            ::app/current-time
                                                            dt/time->string))})))
                     {: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]]


@@ 94,9 106,15 @@
                   (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))))))


(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)})))