 (ns geoforms.forms
-  (:require [json-html.core :refer [edn->hiccup]]
-            [reagent.core :as reagent :refer [atom]]
-            [reagent-forms.core :refer [bind-fields init-field value-of]]))
-
-
-;; Example taken from https://github.com/reagent-project/reagent-forms/tree/master/forms-example
-
-
-;;; DATA
-
-;; should be in firebase
-
-(defonce app-cms
-  (atom
-   {:title "Support local ideas!"
-    :subtitle ""
-    :instructions-district "1. Choose your district"
-    :instructions-vote     "2. Check ideas you want to support!"
-    :instructions-add      "3. Add your won ideas"
-    :instructions-sign     "4. Sign your choices"}))
-
-(def app-ideas
-  (atom
-   [{:id "i-1"
-     :timestamp "2013-08-10 11:20:22"
-     :districts ["Brooklyn"]
-     :idea "New skatepark"
-     :desc "Would be really nice to have a new skatepark"
-     :links ["http://lapresse.ca/article-2"]
-     :supporters [""]
-     :category "Other..."}
-    {:id "i-2"
-     :timestamp "2013-08-10 11:20:24"
-     :districts ["Manhattan"]
-     :idea "More police to prevent steeling"
-     :desc ""
-     :links ["http://lapresse.ca/article-1"]
-     :supporters [""]
-     :category "Security"}
-    {:id "i-3"
-     :timestamp "2013-08-10 11:20:26"
-     :districts ["Manhattan"]
-     :idea "Create a park near fifth avenue"
-     :desc "Would be awesome to have a park on broadway near fith avenue."
-     :links ["http://lapresse.ca/article-56"]
-     :supporters ["email@email.com" "email2@email.com" "john@hotmail.com"]
-     :category "Green"}]))
-
-(def app-users
-  (atom
-   [{:created "2013-08-10 11:20:22"
-     :fullname "Leon Talbot"
-     :email "email@email.com"
-     :zip-code "G21 2C5" ;; this is canadian zip code.
-     :age "32"
-     :annual-revenue ""
-     :subscribe-idea-alerts true
-     :subscribe-volonteer-idea-alerts false
-     :subscribe-district-alerts true
-     :comments ""}
-    {:created "2013-08-10 11:20:22"
-     :fullname "John Talbot"
-     :email "john@hotmail.com"
-     :zip-code "G21 2C3"
-     :age "33"
-     :annual-revenue ""
-     :subscribe-idea-alerts true
-     :subscribe-volonteer-idea-alerts false
-     :subscribe-district-alerts true
-     :comments ""}
-    {:created "2013-08-10 11:20:22"
-     :fullname "Marc Talbot"
-     :email "email2@email.com"
-     :zip-code "G21 2C2"
-     :age "34"
-     :annual-revenue ""
-     :subscribe-idea-alerts true
-     :subscribe-volonteer-idea-alerts false
-     :subscribe-district-alerts true
-     :comments "better UX please. Thanks."}]))
-
-(def app-cats
-  (atom
-   ["Shops" "Security" "Green" "Other..."]))
-
-(def app-districts
-  (atom ["Manhattan" "Brooklyn" "Queens" "Other districts"]))
-
-
-;;; STATE
+  (:require [reagent.core :as reagent :refer [atom]]
+            [reagent-forms.core :refer [bind-fields init-field value-of]]
+            [geoforms.db :as db]))
+
+;;; state
 
 (def app-state
   (atom
-   {:selected-districts @app-districts
+   {:selected-districts nil
     :added-ideas nil
     :added-user  nil}))
 
-
-;;; FNS
+(defn toggle-district! [d]
+  (swap! app-state update :selected-districts
+         #(if (% d) (disj % d) (conj % d))))
+
+(defn selected-district? [d]
+  (if-let [selected (:selected-districts @app-state)]
+    (contains? selected d)
+    (do (swap! app-state assoc :selected-districts (set @db/districts))
+        true)))
+
+(defn district-ideas [d]
+  (filter (comp #(some #{d} %) :districts) @db/ideas))
+
+;;; view helpers
 
 (defn row [label input]
   [:div.row

  [:div
   [:p label]
   (radio "Yes" name true)
   (radio "No" name false)])

(defn input [label type id]
  (row label [:input.form-control {:field type :id id}]))

   #_[signature-component]

   [:div
    (input "first name" :text :person.first-name)
    [:div.row
     [:div.col-md-2]
     [:div.col-md-5
      [:div.alert.alert-danger
       {:field :alert :id :errors.first-name}]]]

    (input "last name" :text :person.last-name)
    [:div.row
     [:div.col-md-2]
     [:div.col-md-5
      [:div.alert.alert-success
       {:field :alert :id :person.last-name :event empty?}
       "last name is empty!"]]]

    [:div.form-group
     [:label "age"]
     [:select.form-control {:field :list :id :person.age}
      (for [a age-groups]
        [:option {:value a :key a} (name a)])]]

    (input "email" :email :person.email)
    [:div.row
     [:div.col-md-2]
     [:div.col-md-5
      [:div.alert.alert-danger
       {:field :alert :id :errors.email :event empty?}
       "email is empty!"]]]

    (radios-yes-no
     "I want to get noticed about my supported ideas progress."
