             [reagent-forms.core :refer [bind-fields init-field value-of]]
             [geoforms.db :as db]))
 
-;;; state
+;;; state and logic
 
 (def app-state
   (atom

                           :on-change #(let [supported? (-> % .-target .-checked)]
                                         (db/set-idea-support! id supported?))}
            [:label [:input {:type :checkbox :id id}] title]])
         [idea-form d]
         [:br]]))]))

(defn add-idea-component []
  [:div
   (input-placeholder "Idea title" :text :idea.idea)
   [select-cat-component]
   [:p
    [:textarea.form-control
     {:rows "4" :placeholder "If necessary, add a description"}]]
   (for [i (range 3)]
     [:div {:key i}
      (input-placeholder "Add a reference URL" :text (symbol (str "idea.url." i)))])
   [:button.btn.btn-default
    {:on-click #(db/create-idea! {:title     "More pizza"
                                  :desc      "Food for me"


(defn district-ideas [d]
  (filter (comp #(some #{d} %) :districts) @db/ideas))

(def email-regex #"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")

(defn validate-user
  "Build map of {key error} pairs, where each {error} relates to persion.{key}"
  [doc]
  (let [person (:person doc)]
    (cond-> {}
      (empty? (:first-name person))
      (assoc :first-name "First name is empty")

      (empty? (:last-name person))
      (assoc :last-name "Last name is empty")

      (empty? (:email person))
      (assoc :email "Email is empty")

      (when-let [email (:email person)]
        (nil? (re-find email-regex email)))
      (assoc :email "Email is not valid")

      (nil? (:age person))
      (assoc :age "Age is not set"))))

(defn validate-user!
  "Update errors atom, and return true if there were any errors."
  [doc]
  (let [errors (validate-user @doc)]
    (swap! doc assoc :errors errors)
    (empty? errors)))

(defn submit!
  "Ensure the signing user exists and their support is noted."
  [doc]
  (db/create-user! (:person doc) @db/supported-ideas))

;;; view helpers

