Commit 87966bf4 authored by Ricardo J. Mendez's avatar Ricardo J. Mendez

Re-implemented export/import

- Exporting data as a transit json string instead of just json, to
preserve keywords
- Reloading data on background page when we get a message

parent 84ba82cc
......@@ -61,6 +61,7 @@
;; handle immediately.
(on-channel alarms/on-alarm dispatch ::on-alarm)
(on-channel browser/on-clicked dispatch ::on-clicked-button)
(on-channel runtime/on-message dispatch ::on-message)
(on-channel runtime/on-suspend dispatch-sync :suspend)
(on-channel runtime/on-suspend-canceled dispatch-sync :log-content)
(on-channel tabs/on-activated dispatch ::tab-activated)
......@@ -113,7 +114,7 @@
(fn [app-state [_ new-data]]
(console/trace "New data on import" new-data)
(console/trace "New data on load" new-data)
;; Create a new id if we don't have one
(when (empty? (:instance-id new-data))
(dispatch [:data-set :instance-id (.-uuid (random-uuid))]))
......@@ -216,6 +217,15 @@
(fn [app-state [_ {:keys [message sender]}]]
; (console/log "GOT INTERNAL MESSAGE" message "from" sender)
(condp = (keyword message)
:reload-data (go (dispatch [:data-load (<! (data/load))])))
......@@ -5,10 +5,16 @@
(:require-macros [cljs.core.async.macros :refer [go]]))
(defn save-raw
"Saves the data raw, without converting it to transit first."
[data callback]
(storage/set {:data data} storage/local callback))
(defn save
"Saves our data block on the extension's storage"
"Saves our data on the extension's storage after converting it to transit."
(storage/set {:data (to-transit data)}))
(save-raw (to-transit data) nil))
(defn load
"Returns a channel where we'll put the entire data block read from the
......@@ -5,9 +5,11 @@
[khroma.idle :as idle]
[khroma.log :as console]
[khroma.runtime :as runtime]
[ :as storage]
[reagent.core :as reagent]
[re-frame.core :refer [dispatch register-sub register-handler subscribe dispatch-sync]])
[re-frame.core :refer [dispatch register-sub register-handler subscribe dispatch-sync]]
[ :as data])
(:require-macros [cljs.core :refer [goog-define]]
[cljs.core.async.macros :refer [go go-loop]]
[reagent.ratom :refer [reaction]]))
......@@ -23,6 +25,7 @@
;; Application data, will be saved
(register-sub :data general-query)
(register-sub :raw-data general-query)
;; Transient data items
(register-sub :ui-state general-query)
(register-sub :app-state general-query)
......@@ -57,6 +60,18 @@
(assoc-in app-state path item)))
(fn [app-state [_ transit-data]]
;; We actually just need to save it, since ::storage-changed takes care
;; of loading it and importing it.
(data/save-raw transit-data #(runtime/send-message :reload-data))
(-> app-state
(assoc-in [:ui-state :section] :time-track)
(assoc-in [:app-state :import] nil))
(fn [_]
......@@ -78,7 +93,7 @@
(let [new-value (get-in message [:changes :data :newValue])
data (from-transit new-value)]
(if (not-empty data)
(assoc app-state :data data)
(assoc app-state :data data :raw-data new-value)
......@@ -192,17 +207,15 @@
(defn data-export []
(let [data (subscribe [:data])
as-string (reaction (.stringify js/JSON (clj->js @data) nil 2))]
(let [data (subscribe [:raw-data])]
(fn []
[:div {:class "page-header"} [:h2 "Current data"]]
[:p (str "Copy the JSON below to a safe location. Size: " (count @as-string))]
[:p (str "Copy the text below to a safe location. Size: " (count @data))]
[:textarea {:class "form-control"
:rows 30
:read-only true
;; We will not export the current tab list
:value @as-string}
:value @data}
......@@ -214,7 +227,7 @@
[:div {:class "page-header"} [:h2 "Import data"]]
[:div {:class "alert alert-warning"}
[:h4 "Warning!"]
[:p "Any data item for which there is a key on the JSON below be replaced!"]]
[:p "Your entire data will be replaced with the information below."]]
[:textarea {:class "form-control"
:rows 30
:value @import-data
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment