WARNING: this is a REGRESSION, and does not work.
This commit is contained in:
		
							parent
							
								
									913beffb61
								
							
						
					
					
						commit
						18a267fd8c
					
				|  | @ -192,7 +192,7 @@ | |||
| 
 | ||||
| (defn init! [] | ||||
|   (rf/dispatch-sync [:initialize-db]) | ||||
|   (get-current-location) | ||||
|   (rf/dispatch-sync [:get-current-location]) | ||||
|   (rf/dispatch [:fetch-locality]) | ||||
|   (rf/dispatch [:fetch-options]) | ||||
|   (rf/dispatch [:fetch-issues]) | ||||
|  |  | |||
|  | @ -8,6 +8,7 @@ | |||
|             [ajax.core :refer [GET]] | ||||
|             [ajax.json :refer [json-request-format json-response-format]] | ||||
|             [youyesyet.canvasser-app.state :as db] | ||||
|             [youyesyet.locality :refer [locality]] | ||||
|             )) | ||||
| 
 | ||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||
|  | @ -37,7 +38,8 @@ | |||
| ;; references, so do it here. | ||||
| 
 | ||||
| (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 | ||||
|     (if (.-geolocation js/navigator) | ||||
|       (.getCurrentPosition | ||||
|  | @ -46,11 +48,13 @@ | |||
|           (js/console.log (str "Current location is: " | ||||
|                                (.-latitude (.-coords position)) ", " | ||||
|                                (.-longitude (.-coords position)))) | ||||
|           (dispatch [:set-latitude (.-latitude (.-coords position))]) | ||||
|           (dispatch [:set-longitude (.-longitude (.-coords position))]))) | ||||
|       (js/console.log "Geolocation not available")) | ||||
|           (dispatch-sync [:set-latitude (.-latitude (.-coords position))]) | ||||
|           (dispatch-sync [:set-longitude (.-longitude (.-coords position))]))) | ||||
|       (js/console.log "Geolocation not available") | ||||
|       (locality (.-latitude (.-coords position)) (.-longitude (.-coords position))) | ||||
|     (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 | ||||
|  |  | |||
|  | @ -155,27 +155,35 @@ | |||
|  :fetch-locality | ||||
|  (fn [{db :db} _] | ||||
|    (let [locality (locality (:latitude db) (:longitude db)) | ||||
|           uri (str source-host | ||||
|          uri (str source-host | ||||
|                   "rest/get-local-data?locality=" | ||||
|                   locality)] | ||||
|      (js/console.log | ||||
|       (str | ||||
|        "Fetching locality data: " uri)) | ||||
|      ;; we return a map of (side) effects | ||||
|      {:http-xhrio {:method          :get | ||||
|                    :uri             uri | ||||
|                    :format          (json-request-format) | ||||
|                    :response-format (json-response-format {:keywords? true}) | ||||
|                    :on-success      [:process-locality] | ||||
|                    :on-failure      [:bad-locality]} | ||||
|       :db  (add-to-feedback db :fetch-locality)}))) | ||||
|          (js/console.log | ||||
|           (str | ||||
|            "Fetching locality data: " uri)) | ||||
|          ;; we return a map of (side) effects | ||||
|          {:http-xhrio {:method          :get | ||||
|                        :uri             uri | ||||
|                        :format          (json-request-format) | ||||
|                        :response-format (json-response-format {:keywords? true}) | ||||
|                        :on-success      [:process-locality] | ||||
|                        :on-failure      [:bad-locality]} | ||||
|           :db  (assoc | ||||
|                  (add-to-feedback db :fetch-locality) | ||||
|                  :locality locality)}))) | ||||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|   :get-current-location | ||||
|   (fn [db _] | ||||
|     (js/console.log "Updating current location") | ||||
|     (assoc db :froboz (get-current-location)))) | ||||
|  :get-current-location | ||||
|  (fn [db _] | ||||
|    (let [locality (get-current-location)] | ||||
|      (js/console.log "Updating 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 | ||||
|  | @ -184,7 +192,7 @@ | |||
|     [db [_ response]] | ||||
|     (js/console.log (str "Updating locality data: " (count response) " addresses")) | ||||
|     ;; 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]}]]) | ||||
|     (refresh-map-pins) | ||||
|     (assoc | ||||
|  | @ -198,13 +206,19 @@ | |||
|     ;; TODO: signal something has failed? It doesn't matter very much, unless it keeps failing. | ||||
|     (js/console.log "Failed to fetch locality data") | ||||
|     ;; loop to do it again | ||||
|     (dispatch [:dispatch-later [{:ms 60000 :dispatch [:fetch-locality]} | ||||
|                                 {:ms 1000 :dispatch [:get-current-location]}]]) | ||||
|     (dispatch [:dispatch-later [{:ms 60000 :dispatch [:fetch-locality]}]]) | ||||
|     (assoc | ||||
|       (remove-from-feedback db :fetch-locality) | ||||
|       :error (cons :fetch-locality (:error db))))) | ||||
| 
 | ||||
| 
 | ||||
| (reg-event-fx | ||||
|  :process-outqueue | ||||
|  (fn [{db :db} _] | ||||
|    (if | ||||
|      (empty? (:outqueue db)) | ||||
| 
 | ||||
| 
 | ||||
| (reg-event-fx | ||||
|   :fetch-options | ||||
|   (fn [{db :db} _] | ||||
|  | @ -231,6 +245,7 @@ | |||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|  ;; TODO: should try again | ||||
|   :bad-options | ||||
|   (fn [db _] | ||||
|     (js/console.log "Failed to fetch options") | ||||
|  | @ -269,6 +284,7 @@ | |||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|  ;; TODO: should try again | ||||
|   :bad-issues | ||||
|   (fn [db _] | ||||
|     (js/console.log "Failed to fetch issues") | ||||
|  | @ -281,53 +297,22 @@ | |||
|   :send-intention | ||||
|   (fn [db [_ args]] | ||||
|     (let [intention (:intention args) | ||||
|           elector-id (:elector-id args) | ||||
|           old-elector (first | ||||
|                         (remove nil? | ||||
|                                 (map | ||||
|                                   #(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 | ||||
|           elector-id (:elector-id args)] | ||||
|       (if | ||||
|         (nil? (-> db :elector)) | ||||
|         (assoc db :error (cons "No elector found; not setting intention" (:error db))) | ||||
|         (do | ||||
|           (js/console.log (str "Setting intention of elector " old-elector " to " intention)) | ||||
|           (merge | ||||
|             (clear-messages db) | ||||
|             {:addresses | ||||
|              (cons | ||||
|                new-address | ||||
|                (remove #(= % old-address) (:addresses db))) | ||||
|              :address new-address | ||||
|              :dwelling new-dwelling | ||||
|              :elector new-elector | ||||
|              :outqueue (cons | ||||
|                          (assoc args :action :set-intention) | ||||
|                          (:outqueue db))})))))) | ||||
|           (-> | ||||
|            db | ||||
|            clear-messages | ||||
|            #(add-to-outqueue % (assoc | ||||
|                            args | ||||
|                            :address_id (-> db :address :id) | ||||
|                            :locality (-> db :address :locality) | ||||
|                            :elector_id (-> db :elector :id) | ||||
|                            :action :set-intention)) | ||||
|            #(assoc % :elector (assoc (:elector db) :intention intention)))))))) | ||||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|  | @ -337,8 +322,11 @@ | |||
|       (do | ||||
|         (js/console.log "Sending request") | ||||
|         (-> db | ||||
|             #(add-to-outqueue % {:elector-id (:id (:elector db)) | ||||
|                                  :issue (:issue db) | ||||
|             #(add-to-outqueue % {:elector_id (-> db :elector :id) | ||||
|                                  :issue_id (-> db :issue :id) | ||||
|                                  :address_id (-> db :address :id) | ||||
|                                  :method_id "Phone" | ||||
|                                  :method_detail (-> db :method_detail) | ||||
|                                  :action :add-request}) | ||||
|             #(add-to-feedback % :send-request))) | ||||
|       (assoc db :error "Please supply a telephone number to call")))) | ||||
|  | @ -357,18 +345,20 @@ | |||
|  (fn [db [_ address-id]] | ||||
|    (let [id (coerce-to-number  address-id) | ||||
|          address (first (remove nil? (map #(if (= id (:id %)) %) (:addresses db))))] | ||||
|      (if | ||||
|        (= (count (:dwellings address)) 1) | ||||
|        (assoc (clear-messages db) | ||||
|          :address address | ||||
|          :dwelling (first (:dwellings address)) | ||||
|          :electors (:electors (first (:dwellings address))) | ||||
|          :page :dwelling) | ||||
|        (assoc (clear-messages db) | ||||
|          :address address | ||||
|          :dwelling nil | ||||
|          :electors nil | ||||
|          :page :building))))) | ||||
|      (-> db | ||||
|          clear-messages | ||||
|          #(if | ||||
|             (= (count (:dwellings address)) 1) | ||||
|             (assoc % | ||||
|               :address address | ||||
|               :dwelling (first (:dwellings address)) | ||||
|               :electors (:electors (first (:dwellings address))) | ||||
|               :page :dwelling) | ||||
|             (assoc % | ||||
|               :address address | ||||
|               :dwelling nil | ||||
|               :electors nil | ||||
|               :page :building)))))) | ||||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|  | @ -445,10 +435,10 @@ | |||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|  :set-telephone | ||||
|  (fn [db [_ telephone]] | ||||
|    (js/console.log (str "Setting telephone to " telephone)) | ||||
|    (assoc (clear-messages db) :telephone telephone))) | ||||
|  :set-method-detail | ||||
|  (fn [db [_ detail]] | ||||
|    (js/console.log (str "Setting method detail to " detail)) | ||||
|    (assoc (clear-messages db) :method_detail detail))) | ||||
| 
 | ||||
| 
 | ||||
| (reg-event-db | ||||
|  |  | |||
|  | @ -69,9 +69,9 @@ | |||
|            #(let [] | ||||
|               [:option {:key % :value %} %]) (keys issues))]] | ||||
|         [:p.widget | ||||
|          [:label {:for "telephone"} "Telephone number"] | ||||
|          [:input {:type "text" :id "telephone" :name "telephone" | ||||
|                   :on-change #(dispatch [:set-telephone (.-value (.-target %))])}]] | ||||
|          [:label {:for "method_detail"} "Telephone number"] | ||||
|          [:input {:type "text" :id "method_detail" :name "method_detail" | ||||
|                   :on-change #(dispatch [:set-method-detail (.-value (.-target %))])}]] | ||||
|         [:p.widget | ||||
|          [:label {:for "send"} "To request a call"] | ||||
|          [:button {:id "send" :on-click #(dispatch [:send-request])} "Send this!"]]] | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue