WARNING: this is a REGRESSION, and does not work.

This commit is contained in:
Simon Brooke 2018-07-26 08:41:59 +01:00
parent 913beffb61
commit 18a267fd8c
4 changed files with 85 additions and 91 deletions

View file

@ -192,7 +192,7 @@
(defn init! [] (defn init! []
(rf/dispatch-sync [:initialize-db]) (rf/dispatch-sync [:initialize-db])
(get-current-location) (rf/dispatch-sync [:get-current-location])
(rf/dispatch [:fetch-locality]) (rf/dispatch [:fetch-locality])
(rf/dispatch [:fetch-options]) (rf/dispatch [:fetch-options])
(rf/dispatch [:fetch-issues]) (rf/dispatch [:fetch-issues])

View file

@ -8,6 +8,7 @@
[ajax.core :refer [GET]] [ajax.core :refer [GET]]
[ajax.json :refer [json-request-format json-response-format]] [ajax.json :refer [json-request-format json-response-format]]
[youyesyet.canvasser-app.state :as db] [youyesyet.canvasser-app.state :as db]
[youyesyet.locality :refer [locality]]
)) ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -37,7 +38,8 @@
;; references, so do it here. ;; references, so do it here.
(defn get-current-location [] (defn get-current-location []
"Get the current location from the device." "Get the current location from the device, setting it in the database and
returning the locality."
(try (try
(if (.-geolocation js/navigator) (if (.-geolocation js/navigator)
(.getCurrentPosition (.getCurrentPosition
@ -46,11 +48,13 @@
(js/console.log (str "Current location is: " (js/console.log (str "Current location is: "
(.-latitude (.-coords position)) ", " (.-latitude (.-coords position)) ", "
(.-longitude (.-coords position)))) (.-longitude (.-coords position))))
(dispatch [:set-latitude (.-latitude (.-coords position))]) (dispatch-sync [:set-latitude (.-latitude (.-coords position))])
(dispatch [:set-longitude (.-longitude (.-coords position))]))) (dispatch-sync [:set-longitude (.-longitude (.-coords position))])))
(js/console.log "Geolocation not available")) (js/console.log "Geolocation not available")
(locality (.-latitude (.-coords position)) (.-longitude (.-coords position)))
(catch js/Object any (catch js/Object any
(js/console.log "Exception while trying to access location: " + any)))) (js/console.log "Exception while trying to access location: " + any)
0)))
(defn pin-image (defn pin-image

View file

@ -168,14 +168,22 @@
:response-format (json-response-format {:keywords? true}) :response-format (json-response-format {:keywords? true})
:on-success [:process-locality] :on-success [:process-locality]
:on-failure [:bad-locality]} :on-failure [:bad-locality]}
:db (add-to-feedback db :fetch-locality)}))) :db (assoc
(add-to-feedback db :fetch-locality)
:locality locality)})))
(reg-event-db (reg-event-db
:get-current-location :get-current-location
(fn [db _] (fn [db _]
(let [locality (get-current-location)]
(js/console.log "Updating current location") (js/console.log "Updating current location")
(assoc db :froboz (get-current-location)))) (if
(and (locality > 0) (not (= locality (:locality db))))
(do
(dispatch :fetch-locality) ;; if the locality has changed, fetch it immediately
(assoc db :locality locality))
db))))
(reg-event-db (reg-event-db
@ -184,7 +192,7 @@
[db [_ response]] [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 ;; loop to do it again
(dispatch [:dispatch-later [{:ms 5000 :dispatch [:fetch-locality]} (dispatch [:dispatch-later [{:ms 60000 :dispatch [:fetch-locality]}
{:ms 1000 :dispatch [:get-current-location]}]]) {:ms 1000 :dispatch [:get-current-location]}]])
(refresh-map-pins) (refresh-map-pins)
(assoc (assoc
@ -198,13 +206,19 @@
;; TODO: signal something has failed? It doesn't matter very much, unless it keeps failing. ;; TODO: signal something has failed? It doesn't matter very much, unless it keeps failing.
(js/console.log "Failed to fetch locality data") (js/console.log "Failed to fetch locality data")
;; loop to do it again ;; loop to do it again
(dispatch [:dispatch-later [{:ms 60000 :dispatch [:fetch-locality]} (dispatch [:dispatch-later [{:ms 60000 :dispatch [:fetch-locality]}]])
{:ms 1000 :dispatch [:get-current-location]}]])
(assoc (assoc
(remove-from-feedback db :fetch-locality) (remove-from-feedback db :fetch-locality)
:error (cons :fetch-locality (:error db))))) :error (cons :fetch-locality (:error db)))))
(reg-event-fx
:process-outqueue
(fn [{db :db} _]
(if
(empty? (:outqueue db))
(reg-event-fx (reg-event-fx
:fetch-options :fetch-options
(fn [{db :db} _] (fn [{db :db} _]
@ -231,6 +245,7 @@
(reg-event-db (reg-event-db
;; TODO: should try again
:bad-options :bad-options
(fn [db _] (fn [db _]
(js/console.log "Failed to fetch options") (js/console.log "Failed to fetch options")
@ -269,6 +284,7 @@
(reg-event-db (reg-event-db
;; TODO: should try again
:bad-issues :bad-issues
(fn [db _] (fn [db _]
(js/console.log "Failed to fetch issues") (js/console.log "Failed to fetch issues")
@ -281,53 +297,22 @@
:send-intention :send-intention
(fn [db [_ args]] (fn [db [_ args]]
(let [intention (:intention args) (let [intention (:intention args)
elector-id (:elector-id args) elector-id (:elector-id args)]
old-elector (first (if
(remove nil? (nil? (-> db :elector))
(map (assoc db :error (cons "No elector found; not setting intention" (:error db)))
#(if (= elector-id (:id %)) %)
(:electors (:dwelling db)))))
new-elector (assoc old-elector :intention intention)
old-dwelling (:dwelling db)
new-dwelling (assoc
old-dwelling
:electors
(cons
new-elector
(remove
#(= % old-elector)
(:electors old-dwelling))))
old-address (:address db)
new-address (assoc
old-address
:dwellings
(cons
new-dwelling
(remove
#(= % old-dwelling)
(:dwellings old-address))))]
(cond
(nil? old-elector)
(assoc db :error (cons "No elector found; not setting intention" (:error db))
(= intention (:intention old-elector))
(do
(js/console.log "Elector's intention hasn't changed; not setting intention")
db))
true
(do (do
(js/console.log (str "Setting intention of elector " old-elector " to " intention)) (js/console.log (str "Setting intention of elector " old-elector " to " intention))
(merge (->
(clear-messages db) db
{:addresses clear-messages
(cons #(add-to-outqueue % (assoc
new-address args
(remove #(= % old-address) (:addresses db))) :address_id (-> db :address :id)
:address new-address :locality (-> db :address :locality)
:dwelling new-dwelling :elector_id (-> db :elector :id)
:elector new-elector :action :set-intention))
:outqueue (cons #(assoc % :elector (assoc (:elector db) :intention intention))))))))
(assoc args :action :set-intention)
(:outqueue db))}))))))
(reg-event-db (reg-event-db
@ -337,8 +322,11 @@
(do (do
(js/console.log "Sending request") (js/console.log "Sending request")
(-> db (-> db
#(add-to-outqueue % {:elector-id (:id (:elector db)) #(add-to-outqueue % {:elector_id (-> db :elector :id)
:issue (:issue db) :issue_id (-> db :issue :id)
:address_id (-> db :address :id)
:method_id "Phone"
:method_detail (-> db :method_detail)
:action :add-request}) :action :add-request})
#(add-to-feedback % :send-request))) #(add-to-feedback % :send-request)))
(assoc db :error "Please supply a telephone number to call")))) (assoc db :error "Please supply a telephone number to call"))))
@ -357,18 +345,20 @@
(fn [db [_ address-id]] (fn [db [_ address-id]]
(let [id (coerce-to-number address-id) (let [id (coerce-to-number address-id)
address (first (remove nil? (map #(if (= id (:id %)) %) (:addresses db))))] address (first (remove nil? (map #(if (= id (:id %)) %) (:addresses db))))]
(if (-> db
clear-messages
#(if
(= (count (:dwellings address)) 1) (= (count (:dwellings address)) 1)
(assoc (clear-messages db) (assoc %
:address address :address address
:dwelling (first (:dwellings address)) :dwelling (first (:dwellings address))
:electors (:electors (first (:dwellings address))) :electors (:electors (first (:dwellings address)))
:page :dwelling) :page :dwelling)
(assoc (clear-messages db) (assoc %
:address address :address address
:dwelling nil :dwelling nil
:electors nil :electors nil
:page :building))))) :page :building))))))
(reg-event-db (reg-event-db
@ -445,10 +435,10 @@
(reg-event-db (reg-event-db
:set-telephone :set-method-detail
(fn [db [_ telephone]] (fn [db [_ detail]]
(js/console.log (str "Setting telephone to " telephone)) (js/console.log (str "Setting method detail to " detail))
(assoc (clear-messages db) :telephone telephone))) (assoc (clear-messages db) :method_detail detail)))
(reg-event-db (reg-event-db

View file

@ -69,9 +69,9 @@
#(let [] #(let []
[:option {:key % :value %} %]) (keys issues))]] [:option {:key % :value %} %]) (keys issues))]]
[:p.widget [:p.widget
[:label {:for "telephone"} "Telephone number"] [:label {:for "method_detail"} "Telephone number"]
[:input {:type "text" :id "telephone" :name "telephone" [:input {:type "text" :id "method_detail" :name "method_detail"
:on-change #(dispatch [:set-telephone (.-value (.-target %))])}]] :on-change #(dispatch [:set-method-detail (.-value (.-target %))])}]]
[:p.widget [:p.widget
[:label {:for "send"} "To request a call"] [:label {:for "send"} "To request a call"]
[:button {:id "send" :on-click #(dispatch [:send-request])} "Send this!"]]] [:button {:id "send" :on-click #(dispatch [:send-request])} "Send this!"]]]