#43 Zoom and lat/long now captured in state.

This commit is contained in:
simon 2017-07-21 15:51:16 +01:00
parent 4dc7ad396a
commit 3a18cacf0f
4 changed files with 61 additions and 3 deletions

View file

@ -169,8 +169,34 @@
(assoc (clear-messages db) :issue issue))) (assoc (clear-messages db) :issue issue)))
(reg-event-db
:set-latitude
(fn [db [_ issue]]
(assoc db :latitude issue)))
(reg-event-db
:set-longitude
(fn [db [_ issue]]
(assoc db :longitude issue)))
(reg-event-db (reg-event-db
:set-telephone :set-telephone
(fn [db [_ telephone]] (fn [db [_ telephone]]
(js/console.log (str "Setting telephone to " telephone)) (js/console.log (str "Setting telephone to " telephone))
(assoc (clear-messages db) :telephone telephone))) (assoc (clear-messages db) :telephone telephone)))
(reg-event-db
:set-view
(fn [db [_ view]]
(assoc db :view view)))
(reg-event-db
:set-zoom
(fn [db [_ zoom]]
(if (integer? zoom)
(assoc db :zoom zoom)
db)))

View file

@ -82,4 +82,8 @@
;;; the queue of items waiting to be transmitted. ;;; the queue of items waiting to be transmitted.
:outqueue () :outqueue ()
;;; the currently displayed page within the app. ;;; the currently displayed page within the app.
:page :home}) :page :home
:view nil
:latitude 55.82
:longitude -4.25
:zoom 12})

View file

@ -76,6 +76,16 @@
(fn [db _] (fn [db _]
(:issues db))) (:issues db)))
(reg-sub
:latitude
(fn [db _]
(:latitude db)))
(reg-sub
:longitude
(fn [db _]
(:longitude db)))
(reg-sub (reg-sub
:page :page
(fn [db _] (fn [db _]
@ -91,3 +101,12 @@
(fn [db _] (fn [db _]
(:outqueue db))) (:outqueue db)))
(reg-sub
:view
(fn [db _]
(:view db)))
(reg-sub
:zoom
(fn [db _]
(:zoom db)))

View file

@ -77,6 +77,11 @@
so back links work." so back links work."
[id] [id]
(js/console.log (str "Click handler for address #" id)) (js/console.log (str "Click handler for address #" id))
(let [view @(subscribe [:view])
centre (.getCenter view)]
(dispatch [:set-zoom (.getZoom view)])
(dispatch [:set-latitude (.-lat centre)])
(dispatch [:set-longitude (.-lng centre)]))
(set! window.location.href (str "#building/" id))) (set! window.location.href (str "#building/" id)))
;; This way is probably more idiomatic React, but history doesn't work: ;; This way is probably more idiomatic React, but history doesn't work:
;; (defn map-pin-click-handler ;; (defn map-pin-click-handler
@ -121,7 +126,10 @@
(defn map-did-mount-osm (defn map-did-mount-osm
"Did-mount function loading map tile data from Open Street Map." "Did-mount function loading map tile data from Open Street Map."
[] []
(let [view (.setView (.map js/L "map" (clj->js {:zoomControl false})) #js [55.82 -4.25] 13) (let [view (.setView
(.map js/L "map" (clj->js {:zoomControl false}))
#js [@(subscribe [:latitude]) @(subscribe [:longitude])]
@(subscribe [:zoom]))
addresses @(subscribe [:addresses])] addresses @(subscribe [:addresses])]
(js/console.log (str "Adding " (count addresses) " pins")) (js/console.log (str "Adding " (count addresses) " pins"))
(doall (map #(add-map-pin % view) addresses)) (doall (map #(add-map-pin % view) addresses))
@ -129,7 +137,8 @@
(clj->js {:attribution osm-attrib (clj->js {:attribution osm-attrib
:maxZoom 18})) :maxZoom 18}))
view) view)
)) (dispatch [:set-view view])
view))
(defn map-did-mount (defn map-did-mount