Add proof of concept for saving/restoring app-db

This commit is contained in:
Daniel Compton 2017-11-22 15:09:32 +13:00
parent 39d29f6252
commit cb711cf3e2
2 changed files with 34 additions and 1 deletions

View File

@ -5,6 +5,7 @@
[day8.re-frame.trace.panels.subvis :as subvis]
[day8.re-frame.trace.panels.traces :as traces]
[day8.re-frame.trace.panels.subs :as subs]
[day8.re-frame.trace.panels.save :as save]
[re-frame.trace]
[reagent.core :as r]))
@ -29,6 +30,7 @@
(tab-button :traces "Traces")
(tab-button :app-db "App DB")
(tab-button :subs "Subs")
(tab-button :save "Save")
#_(tab-button :subvis "SubVis")
(when-not external-window?
[:img.popout-icon
@ -40,4 +42,5 @@
:app-db [app-db/render-state db/app-db]
:subvis [subvis/render-subvis traces]
:subs [subs/subs-panel]
:save [save/save-panel]
[app-db/render-state db/app-db])]))

View File

@ -0,0 +1,30 @@
(ns day8.re-frame.trace.panels.save
(:require [mranderson047.re-frame.v0v10v2.re-frame.core :as rf]
[re-frame.db]
[day8.re-frame.trace.utils.localstorage :as localstorage]))
(rf/reg-event-db
:save/save-app-db
(fn [db _]
(localstorage/save! "saved-app-db" @re-frame.db/app-db)
db
))
(rf/reg-event-db
:save/restore-app-db
(fn [db _]
(if-let [app-db (localstorage/get "saved-app-db")]
(reset! re-frame.db/app-db app-db)
db)))
(defn save-panel []
[:div
[:h1 "Save State"]
[:br]
[:button.text-button
{:on-click #(rf/dispatch [:save/save-app-db])}
"Save"]
[:br]
[:button.text-button
{:on-click #(rf/dispatch [:save/restore-app-db])}
"Reload"]])