From de4c8b31042635c5d5f635d9b8f4469b55933399 Mon Sep 17 00:00:00 2001 From: Sam Marshall Date: Mon, 22 Jun 2020 14:26:44 +0100 Subject: [PATCH] save journal to localstorage --- src/redwood/core.cljs | 1 - src/redwood/data/journal.cljs | 25 +++++++++++++++++- src/redwood/events.cljs | 50 ++++++++++++++++++++++++----------- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/redwood/core.cljs b/src/redwood/core.cljs index 28f3cb3..805e7bc 100644 --- a/src/redwood/core.cljs +++ b/src/redwood/core.cljs @@ -52,4 +52,3 @@ (rf/dispatch-sync [::e/init]) (rd/render [root] (.getElementById js/document "app"))) - diff --git a/src/redwood/data/journal.cljs b/src/redwood/data/journal.cljs index 7fea4a8..584574d 100644 --- a/src/redwood/data/journal.cljs +++ b/src/redwood/data/journal.cljs @@ -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)))) diff --git a/src/redwood/events.cljs b/src/redwood/events.cljs index 646ae9e..38fa8c6 100644 --- a/src/redwood/events.cljs +++ b/src/redwood/events.cljs @@ -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)}))) + + -- 2.30.2