 (register-handler
  :fetch-survey-details
  (fn [app-db [_ survey]]
-   (let [updated-surveys (map (fn [it]
-                                (let [year  (:year survey)
-                                      month (:month survey)]
-                                  (if (and (= year (:year it)) (= month (:month it)))
-                                    (if (= "collection-item active" (:css it))
-                                      (assoc it :css "collection-item")
-                                      (do 
-                                        (fetch-survey-details survey)
-                                        (assoc it :css "collection-item active")))
-                                    (assoc it :css "collection-item"))))
-                              (:surveys app-db))]
-
-     (assoc app-db :surveys updated-surveys))))
+   (let [year            (:year survey)
+         month           (:month survey)
+         surveys         (:surveys app-db)
+         features        (get-in app-db [:survey-details :features])
+         updated-surveys (find-updated-surveys surveys year month)
+         final-features  (get-final-features {:year year :month month :features features :surveys surveys})]
+     (map-coords final-features)
+     (-> app-db
+         (assoc-in [:survey-details :features] final-features)
+         (assoc :surveys updated-surveys)))))
 
 (register-handler
  :received-survey-deatils
- (fn [app-db [_ details]]
-   (map-coords details)
-   (assoc app-db :survey-details details)))
+ (fn [app-db [_ features]]
+   (let [geo-json (concat (get-in app-db [:survey-details :features]) features)]
+     (map-coords geo-json)
+     (assoc-in app-db 
+                [:survey-details :features]  
+                geo-json))))

                       (map #(assoc % :css "collection-item")))]
      (dispatch [:received-surveys surveys]))))

(defn validate-geojson
  [coords]
  (go
    (let [resp (<! (http/post "http://geojsonlint.com/validate" {:json-params coords}))]
      (.log js/console "validate: " (clj->js resp)))))

(defn fetch-survey-details
  [survey]
  (go

  (:require-macros [cljs.core.async.macros :refer [go]]))

(def app-state {:surveys        []
                :survey-details {:type "FeatureCollection" :features []}
                :rendered-map?  false})


