                            {:display "User" :role :kixi.hecuba.security/user :value "user"}
                            {:display "None" :role "none" :value "none"}])
 
-(defn dropdown [{:keys [default items]} owner {:keys [dropdown-chan id path]}]
+(defn dropdown [{:keys [default items]} owner {:keys [event-chan id path]}]
   (om/component
    (html
     [:select.form-control
      {:default-value default :id id
       :on-change (fn [e]
                    (let [v    (.-value (aget (.-options (.-target e)) (.-selectedIndex (.-options (.-target e)))))]
-                     (put! dropdown-chan {:path path
-                                          :value (if-not (= v "none") v nil)})))}
+                     (put! event-chan {:event :dropdown :value {:path path
+                                                                :value (if-not (= v "none") v nil)}})))}
      (for [item items]
        [:option {:value (:role item)} (:display item)])])))
 


(defn load-user-data
  "Initialises drop down, programmes and projects with user data."
  [data user]
  (let [{:keys [programmes projects role]} (:data user)]
    (om/update! data [:user :class] "has-success")
    (om/update! data [:user :username] (:username user))
    (om/update! data [:user :role] role)
    (om/update! data [:user :programmes] programmes)
    (om/update! data [:user :projects] projects)
    (data/fetch-programmes data)
    (fetch-projects data)))

(defn find-username
  "Check if typed username exists and load user data if it does. Display error alert otherwise."
  [data username]
  (if username
    (let [user (first (filter #(= (:username %) username) (-> @data :user :data)))]
      (if user
        (load-user-data data user)
        (do
          (om/update! data [:user :class] "has-error")
          (om/update! data :alert {:status true
                                   :class "alert alert-danger"
                                   :text "Username not found."}))))))

(defn clear-form
  "Reset app-state when user deletes their selection."
  [data]
  (om/update! data [:user :role] {})
  (om/update! data [:user :class] "")
  (om/update! data [:user :username] nil)
  (om/update! data [:user :projects] {})
  (om/update! data [:user :programmes] {}))

(defn username-selection-form [data]
  (fn [cursor owner]
    (reify
      om/IRender
      (render [_]
        (html
         (let [selected-username (-> cursor :username)
               class             (-> cursor :class)]
           [:div.form-inline {:role "form" :style {:width "100%"}}
            [:div {:class (str "form-group " class)}
             [:input {:type "text"
                      :style {:width "100%"}
                      :class "form-control"
                      :default-value selected-username
                      :on-change (fn [e] (om/set-state! owner :value (.-value (.-target e)))
                                   (when (empty? (om/get-state owner :value))
                                     (clear-form data)))
                      :on-key-press (fn [e] (when (= (.-keyCode e) 13)
                                              (find-username data (om/get-state owner :value))))}]]
            [:button {:type "button" :class "btn btn-primary"
                      :on-click (fn [e] (find-username data (om/get-state owner :value)))} "Find user"]]))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Projects

(defn project-row [project owner {:keys [dropdown-chan]}]
  (reify
    om/IRender
    (render [_]

           (om/build-all project-row (sort-by :project_id
                                              (mapv #(assoc % :role (get-in existing-selections [(:project_id %)] "none"))
                                                    projects))
                         {:key :project_id :opts {:event-chan event-chan}})]]]]))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Programmes

(defn programmes-row [programme owner {:keys [event-chan]}]
  (reify
    om/IRender
    (render [_]
