   )
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Init DB
+(defonce conn (d/create-conn uidb/schema))
+
+(defn init-app []
+  (d/transact! conn [{:db/id -1 :app/id 1}
+                     {:db/id -1 :selected-page :competitions}
+                     ;{:db/id -1 :app/selected-competition {}}
+                     ]))
+
+(defn app-started? [conn]
+  (not
+   (empty? (d/q '[:find ?e
+                  :where
+                  [?e :app/id 1]] (d/db conn)))))
+
+;(log conn)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Server message handling
 
 (declare reconciler)
 (defn handle-query-result [d]
-  (let [clean-data  (uidb/sanitize d)]
-    ;;{:competition/name "TestNamn" :competition/location "Location"}
-    (log "Handle Q R")
-     ;(log clean-data)   
-    ;(log (apply str (map :competition/name clean-data)))
-                                        ;(d/transact! (d/db conn) )
-    ;;(d/transact! conn [clean-data])
-    ;; (log (d/q '[:find [(pull ?c [:class/name]) ...]
-    ;;             :where
-    ;;             [?e :competition/name "Rikstävling disco"]
-    ;;             [?e :competition/classes ?c]]
-    ;;           (d/db conn)))
-    ;(d/transact! conn [{:db/id 1 :competition/name (apply str (map :competition/name clean-data))}])
-    ;(om/transact! reconciler `[(app/add-competition `{:X "X"}) :app/competitions])
-    ;(log clean-data)
-    ;; (om/transact! reconciler `[(app/add-competition ~clean-data) :app/competitions
-    ;;                            ])
-    (om/transact! reconciler `[(app/add-competition ~clean-data) :app/competitions])
-    ))
+  (log "Handle Q R")
+    ;; VERY TEMPORARY (KILL ME IF I DO NOT FIX IT)
+    ;; Need to make difference between query for all comps. vs query for details for a comp.
+
+  (if (vector? d)
+    (let [clean-data  {:competitions d}      ;(uidb/sanitize (first d))
+          ]
+      (log "Compsss")
+      (om/transact! reconciler `[(app/add-competition ~clean-data) :app/competitions])
+      )
+    (let [clean-data (uidb/sanitize d)]
+      (log "Crazzzy")
+      (om/transact! reconciler `[(app/add-competition ~clean-data) :app/competitions]))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Sente message handling

   (let [competition (om/props this)
         name (:competition/name competition)]
     (dom/tr
      #js {:onClick #(om/transact! this `[(app/select-competition {:name ~name})])}
      (dom/td nil name)
      (dom/td nil (:competition/location competition))))))


(defmulti event-msg-handler :id) ; Dispatch on event-id
;; Wrap for logging, catching, etc.:
(defn event-msg-handler* [{:keys [id ?data event] :as ev-msg}]
  (do
    (log "Enter event-msg-handler*")
    (event-msg-handler {:id (first ev-msg)
                        :?data (second ev-msg)})
    (log "Exit event-msg-handler*")))

(defmethod event-msg-handler :default ; Fallback
  [{:keys [event] :as ev-msg}]
