Almost at the point of transmitting intentions.

This commit is contained in:
Simon Brooke 2018-07-26 17:47:31 +01:00
parent 981a4d0d34
commit 9564c76ae6
6 changed files with 132 additions and 58 deletions

View file

@ -107,6 +107,8 @@
(db/create-visit! db/*db* params))))
;; http://localhost:3000/rest/create-intention?address_id=18&elector-id=62&elector_id=62&intention=Yes&locality=5482391
(defn create-intention-and-visit!
"Doing visit creation logic server side; request params are expected to
include an `option`, an `elector_id` and an `address_id`, or an `option` and
@ -117,6 +119,7 @@
a new `visit` record."
[request]
(let [params (massage-params request)]
(log/debug "Creating intention with params: " params)
(if-let [address-id (-> params :address_id)]
(db/create-intention!
db/*db*

View file

@ -8,6 +8,7 @@
[markdown.core :refer [md->html]]
[reagent.core :as r]
[re-frame.core :as rf]
[re-frame.fx]
[secretary.core :as secretary]
[youyesyet.canvasser-app.ajax :refer [load-interceptors!]]
[youyesyet.canvasser-app.gis :refer [get-current-location]]
@ -196,6 +197,7 @@
(rf/dispatch [:fetch-locality])
(rf/dispatch [:fetch-options])
(rf/dispatch [:fetch-issues])
(rf/dispatch [:dispatch-later [{:ms 60000 :dispatch [:process-queue]}]])
(load-interceptors!)
(hook-browser-navigation!)
(mount-components))

View file

@ -48,8 +48,8 @@
(js/console.log (str "Current location is: "
(.-latitude (.-coords position)) ", "
(.-longitude (.-coords position))))
(dispatch-sync [:set-latitude (.-latitude (.-coords position))])
(dispatch-sync [:set-longitude (.-longitude (.-coords position))])
(dispatch [:set-latitude (.-latitude (.-coords position))])
(dispatch [:set-longitude (.-longitude (.-coords position))])
(locality (.-latitude (.-coords position)) (.-longitude (.-coords position))))))
(js/console.log "Geolocation not available")
(catch js/Object any
@ -113,11 +113,12 @@
(defn map-remove-pins
"Remove all pins from this map `view`. Side-effecty; liable to be problematic."
[view]
(.eachLayer view
#(if
(instance? js/L.Marker %)
(.removeLayer view %)))
view)
(if view
(.eachLayer view
#(if
(instance? js/L.Marker %)
(.removeLayer view %)))
view))
(defn refresh-map-pins

View file

@ -6,6 +6,7 @@
[cemerick.url :refer (url url-encode)]
[cljs.reader :refer [read-string]]
[clojure.walk :refer [keywordize-keys]]
[re-frame.fx]
[day8.re-frame.http-fx]
[re-frame.core :refer [dispatch reg-event-db reg-event-fx subscribe]]
[youyesyet.canvasser-app.gis :refer [refresh-map-pins get-current-location]]
@ -50,6 +51,16 @@
:anchor nil))
(defn compose-packet
[item]
"Convert this `item` into a URI which can be sent as a GET call"
(assoc
(url js/window.location)
:path (str "/rest/" (name (:action item)))
:query (dissoc item :action)
:fragment nil))
(def feedback-messages
{:fetch-locality "Fetching local data."
:send-request "Request has been queued."
@ -65,7 +76,8 @@
(defn add-to-outqueue
[db message]
add-to-key db :outqueue message)
(dispatch [:process-queue])
(add-to-key db :outqueue message))
(defn add-to-feedback
@ -85,6 +97,11 @@
(remove-from-key db :feedback x))
(defn remove-from-outqueue
[db x]
(remove-from-key db :outqueue x))
(defn coerce-to-number [v]
"If it is possible to do so, coerce `v` to a number.
NOTE: I tried to do this in *cljc*, but it did not work. Leave it alone."
@ -190,7 +207,7 @@
:process-locality
(fn
[db [_ response]]
(js/console.log (str "Updating locality data: " (count response) " addresses"))
(js/console.log (str "Updating locality data: " (count response) " addresses " ))
;; loop to do it again
(dispatch [:dispatch-later [{:ms 60000 :dispatch [:fetch-locality]}
{:ms 1000 :dispatch [:get-current-location]}]])
@ -310,8 +327,9 @@
:address_id (-> db :address :id)
:locality (-> db :address :locality)
:elector_id (-> db :elector :id)
:action :set-intention))
:elector (assoc (:elector db) :intention intention)))))))
:action :create-intention))
:elector (assoc (:elector db) :intention intention)
:page :elector))))))
(reg-event-db
@ -328,7 +346,7 @@
:address_id (-> db :address :id)
:method_id "Phone"
:method_detail (-> db :method_detail)
:action :add-request})
:action :create-request})
:send-request))
(assoc db :error "Please supply a telephone number to call"))))
@ -336,6 +354,7 @@
(reg-event-db
:set-active-page
(fn [db [_ k]]
(js/console.log (str "Setting page to " k))
(if k
(assoc (clear-messages db) :page k)
db)))
@ -346,9 +365,11 @@
(fn [db [_ address-id]]
(let [id (coerce-to-number address-id)
address (first (remove nil? (map #(if (= id (:id %)) %) (:addresses db))))]
(js/console.log (str "Set address to " address " "))
(clear-messages
(if
(assoc db (= (count (:dwellings address)) 1)
(= (count (:dwellings address)) 1)
(assoc db
:address address
:dwelling (first (:dwellings address))
:electors (:electors (first (:dwellings address)))
@ -374,6 +395,7 @@
(reg-event-db
:set-dwelling
(fn [db [_ dwelling-id]]
(js/console.log (str "Setting dwelling to " dwelling-id " "))
(let [id (coerce-to-number dwelling-id)
dwelling (first
(remove
@ -452,3 +474,47 @@
(if (integer? zoom)
(assoc db :zoom zoom)
db)))
(reg-event-fx
:process-queue
(fn [{db :db} _]
(if-let [item (first (:outqueue db))]
;; if there's something in the queue, transmit it...
(let [uri (compose-packet item)]
(js/console.log (str "Transmitting item" uri))
{:http-xhrio {:method :get
:uri uri
:format (json-request-format)
:response-format (json-response-format {:keywords? true})
:on-success [:tx-success]
:on-failure [:tx-failure]}
:db (assoc
(add-to-feedback db :process-queue)
:tx-item item
:outqueue (remove #(= % item) (:outqueue db)))})
;; else try again in a minute
(do
(js/console.log "Nothing to send to server")
(dispatch [:dispatch-later [{:ms 60000 :dispatch [:process-queue]}]])
{:db db}))))
(reg-event-db
:tx-success
(fn
[db [_ response]]
(let [r (js->clj response)]
(js/console.log (str "Transmission success: " r))
;; while we've got comms working, get as many items through as we can.
(dispatch [:process-queue])
db)))
(reg-event-db
:tx-failure
(fn [db _]
(js/console.log (str "Transmission failed, requeueing" (:tx-item db)))
(assoc
(add-to-outqueue db (:tx-item db))
:error "Transmission failed, requeueing")))