                                                                     (vec (session/get :all-movements)))})]
            [movements-ac-comp {:id      (str "mtags" i)
                                :class   "eit" :placeholder "type to find and add movement.."
-                               :on-save #(when (some #{%} (session/get :all-movements))
-                                          (swap! template-state update-in [:parts i :specific-movements] conj %))}])]]
-       [:div.pure-g (for [c (get-in @template-state [:parts i :specific-movements])]
-                      ^{:key c} [:div.pure-u {:style {:margin-right "5px"}} c])]])))
+                               :on-save #(when (and (some #{%} (session/get :all-movements))
+                                                    (not (some #{%} (get-in @template-state [:parts i :specific-movements]))))
+                                          (swap! template-state update-in [:parts i :specific-movements] conj %))}])]]])))
+
+(defn save-template-component [error-atom]
+  [:div.pure-g
+   [:button.pure-u {:on-click #(do
+                                (swap! template-state assoc :user (session/get :user))
+                                (let [title (:title @template-state)
+                                      parts (:parts @template-state)]
+                                  (cond
+                                    (nil? title) (reset! error-atom "The template needs a title.")
+                                    (empty? parts) (reset! error-atom "A session must have 1 or more parts.")
+
+                                    :else (print @template-state) #_(POST "template"
+                                                {:params        @template-state
+                                                 :handler       (fn [response] (print response))
+                                                 :error-handler (fn [response] (reset! error-atom (:response response)))}))))}
+    "Save"]])
 
 (defn title-component []
   [:div.pure-g

          [:div.pure-u
           [categories-ac-comp {:id      (str "ctags" i)
                                :class   "edit" :placeholder "type to find and add category.."
                                :on-save #(when (some #{%} (session/get :all-categories))
                                           (swap! template-state update-in [:parts i :categores] conj %))}]])]
       (when @show-categories-list
         (for [c (session/get :all-categories)]
           ^{:key c} [:div.pure-g
                      [:div.pure-u (str c)]]))

       [:div.pure-g (for [c (get-in @template-state [:parts i :categories])]
                      ^{:key c} [:div.pure-u {:style {:margin-right "5px"}} c])]

       [:div.pure-g (for [c categories]
                      ^{:key c} [:div.pure-u {:style {:margin-right "5px"}} c])]




       [:div.pure-g
        [:label.pure-u "It consists of "
         [:span {:style {:color "red"}} n] " generated movements,"]
        [:button.pure-u
         {:on-click #(swap! template-state update-in [:parts i :n] inc)} "+"]
        [:button.pure-u
         {:on-click #(when (> n 0)
                      (swap! template-state update-in [:parts i :n] dec))} "-"]]


       [:div.pure-g

(ns movement.template
  (:require [reagent.core :refer [atom]]
            [reagent.session :as session]
            [reagent-forms.core :refer [bind-fields]]
            [secretary.core :include-macros true :refer [dispatch!]]
            [movement.menu :refer [menu-component]]
            [movement.util :refer [text-input POST]]
            [movement.text :refer [text-input-component auto-complete-did-mount]]
            [movement.state :refer [handler-fn]]
            [clojure.string :as str]))

(def template-state (atom {:parts []}))

(defn movement-component [title image]
  [:div.pure-u.movement
   [:div.pure-g
    [:div.pure-u.refresh {:on-click #()}]
    [:h3.pure-u.title title]
    [:div.pure-u.destroy {:on-click #()}]]
   [:img.graphic.pure-img-responsive {:src image}]
   [:div.pure-g
    [:div.pure-u 10]
    [:div.pure-u "reps"]]
   [:div.pure-g
    [:div.pure-u 3]
    [:div.pure-u "set"]]
   [:div.pure-g
    [:div.pure-u "-"]
    [:div.pure-u "meters"]]
   [:div.pure-g
    [:div.pure-u "-"]
    [:div.pure-u "seconds"]]])

(defn part-creator-component []
  (let [showing-categories-list (atom false)]
    (fn [{:keys [title n categories]} i]
      [:div
       [:div.pure-g
        [:h2.pure-u [:input {:type        "text"
                             :placeholder "\"warmup\", \"repeat for 3 rounds\" "
                             :on-change   #(swap! template-state assoc-in [:parts i :title] (-> % .-target .-value))
                             :value       (get-in @template-state [:parts i :title])}]]]

       [:div.pure-g
        (for [m (get-in @template-state [:parts i :specific-movements])]
          ^{:key (str m (rand-int 1000))} [:div {:on-click #(let [movements (vec (remove #{m} (get-in @template-state [:parts i :specific-movements])))]
                                       (swap! template-state assoc-in [:parts i :specific-movements] movements))}
                     (movement-component (str m) (str "images/" (str/replace (str/lower-case m) " " "-") ".png"))])
        (for [n (range (get-in @template-state [:parts i :n]))]
          ^{:key (str n (rand-int 1000))} (movement-component "movement name" "images/push-up.png"))]


       [:div.pure-g
        [:div.pure-u
         [:button
          {:on-click #(swap! template-state update-in [:parts i :n] inc)} "+"]
         [:button
          {:on-click #(when (> n 0)
                       (swap! template-state update-in [:parts i :n] dec))} "-"]
         [:span {:style {:color "red"}} n] " generated movements should be randomly drawn from the following categories: "]]

       [:div.pure-g (for [c categories]
                      ^{:key c}
                      [:div.pure-u
                       [:div {:style {:margin-right "5px"}} c]
                       [:div {:on-click #(let [categories (vec (remove #{c} (get-in @template-state [:parts i :categories])))]
                                          (swap! template-state assoc-in [:parts i :categories] categories))} "x"]])]
       [:div.pure-g
        (let [id (str "ctags" i)
              categories-ac-comp (with-meta text-input-component
