Commit e0da801f authored by Ricardo J. Mendez's avatar Ricardo J. Mendez

Posting snapshots to the backend

parent 64d08d1a
......@@ -4,6 +4,7 @@
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "1.7.122"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[cljs-ajax "0.3.14"]
[cljsjs/react-bootstrap "0.25.1-0" :exclusions [org.webjars.bower/jquery]]
[com.lucasbradstreet/cljs-uuid-utils "1.0.2"]
[khroma "0.1.0-SNAPSHOT"]
......@@ -15,10 +16,12 @@
[lein-chromebuild "0.3.0"]]
:cljsbuild {:builds {:main
{:source-paths ["src"]
:compiler {:output-to "target/unpacked/booklet.js"
:output-dir "target/js"
:optimizations :whitespace
:pretty-print true}}}}
:compiler {:output-to "target/unpacked/booklet.js"
:output-dir "target/js"
:optimizations :whitespace
:closure-defines {booklet.core/api-uri "http://localhost/from-the-config"}
; :closure-defines {"booklet.core.api_uri" "http://localhost/from-the-config"}
:pretty-print true}}}}
:chromebuild {:resource-paths ["resources/js"
"resources/html"
......
......@@ -15,7 +15,7 @@
"128": "icon128.png"
},
"permissions": [
"tabs", "storage", "idle"
"tabs", "storage", "idle", "http://localhost:3000/"
],
"background": {
"scripts": [
......
(ns booklet.core
(:require [cljs.core.async :refer [>! <!]]
(:require [ajax.core :refer [GET POST PUT]]
[cljs.core.async :refer [>! <!]]
[cljs.core :refer [random-uuid]]
[cljsjs.react-bootstrap]
[cljs-uuid-utils.core :as uuid]
[khroma.idle :as idle]
[khroma.log :as console]
[khroma.runtime :as runtime]
......@@ -10,7 +11,8 @@
[khroma.windows :as windows]
[reagent.core :as reagent]
[re-frame.core :refer [dispatch register-sub register-handler subscribe dispatch-sync]])
(:require-macros [cljs.core.async.macros :refer [go]]
(:require-macros [cljs.core :refer [goog-define]]
[cljs.core.async.macros :refer [go]]
[reagent.ratom :refer [reaction]]))
......@@ -18,6 +20,8 @@
;;;; Queries
;;;;------------------------------
(goog-define api-uri "http://localhost:3000")
(defn general-query
[db path]
(reaction (get-in @db path)))
......@@ -29,6 +33,8 @@
(register-sub :app-state general-query)
;;;;----------------------------
;;;; Functions
;;;;----------------------------
......@@ -47,6 +53,7 @@
"Takes a tabset and returns a new group containing them and some extra data"
[tabs]
{:date (.now js/Date)
:id (.-uuid (random-uuid))
:tabs tabs})
(defn group-label [group]
......@@ -210,13 +217,39 @@
)
))
(register-handler
:snapshot-post
(fn [app-state [_]]
(let [to-send (select-keys (:data app-state) [:instance-id :snapshots])]
(GET (str api-uri "/api/echo/" "hello")
{:handler #(console/log "GET Handler" %)
:error-handler #(console/log "GET Error" %)})
(POST (str api-uri "/api/snapshot/many")
{:params to-send
:handler #(dispatch [:snapshot-post-success (:snapshots to-send)])
:error-handler #(dispatch [:log-content %])})
)
app-state
)
)
(register-handler
:snapshot-post-success
(fn [app-state [_ snapshots]]
(console/log "Saved" snapshots)
;; TODO: Remove only the snapshots we saved
(dispatch [:data-set :snapshots nil])
app-state
))
(register-handler
:storage-loaded
(fn [app-state [_ data]]
;; We create a new id if fir any reason we don't have one
(when (empty? (:instance-id data))
(dispatch [:data-set :instance-id (uuid/uuid-string (uuid/make-random-uuid))]))
(dispatch [:data-set :instance-id (.-uuid (random-uuid))]))
(dispatch [:snapshot-post])
;; Return new data state. We don't return the new id because data-set also
;; saves it, the call below only sets the internal state
(->> data
......@@ -403,7 +436,7 @@
]])
)
(defn tab-groups []
(defn div-tab-groups []
(let [tab-groups (subscribe [:data :groups])
group-edit (subscribe [:ui-state :group-edit])
label (subscribe [:ui-state :group-label])
......@@ -416,7 +449,7 @@
])
))
(defn snapshots []
(defn div-snapshots []
(let [snapshots (subscribe [:data :snapshots])
to-list (reaction (sort-by #(* -1 (:date %)) @snapshots))]
(fn []
......@@ -461,10 +494,10 @@
(def component-dir {:monitor current-tabs
:groups tab-groups
:groups div-tab-groups
:export data-export
:import data-import
:snapshots snapshots})
:snapshots div-snapshots})
(defn main-section []
......@@ -497,6 +530,7 @@
(defn init []
(console/log "Initialized booklet.core")
(console/log api-uri)
(go (let [window (<! (windows/get-current))
state (<! (idle/query-state 30))]
(dispatch-sync [:initialize (:tabs window)])
......
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