 ;;    :on-success (js/alert "You should never see this")
 ;;    :on-error (js/alert "Remotes correctly handle error conditions"))
 
-(defn- basket-id
-  ([] (when-let [matches (re-matches #".*/#([1-9]*)"
-                                     (.-href (.-location js/window)))]
-        (last matches)))
-  ([id] (.assign (.-location js/window) (str "#" id))))
+(defn- basket-id []
+  (when-let [matches (re-matches #".*/#([1-9]*)"
+                                 (.-href (.-location js/window)))]
+    (last matches)))
 
 (defn to-int [s]
   (js/parseInt s))

            [domina.css :as css]
            [domina.xpath :as x]
            [eatme.client.render :as render]
            [clojure.string :as string])
  (:require-macros [shoreleave.remotes.macros :as srm]))

(def query-args (common/query-args-map))
(def hash-args (common/hash-args-map))

;; Integrate history/back-button to the search
;For example:
;    (history/navigate-callback #(process-search (subs (:token %) 4) false))
;Where process-search is the main action to take when processing the page/url

;; ### Browser REPL
;; If you add a `repl` as a query-string arg, even on the live site,

      (event/listen! (css/sel new-item "button[rel=decrement]") :click #(quantity-changed :dec true (event/target %)))
      (event/listen-once! (css/sel new-item "button[rel=complete]") :click #(completed-item (event/target %))))))

(defn load-basket []
  (when-let [basket-id (basket-id)]
    (srm/rpc
     (api/load-basket basket-id) [basket]
     :on-success (do
                   (d/destroy-children! items-list)
                   (doseq [item (:items basket)]
                     (add-item-to-list item)))
     :on-error (js/alert (str "Error loading basket: " basket-id)))))


(defn nav-handler [{:keys [token navigation?]}]
  (when navigation?
    (load-basket)))

(def session-history (history/history nav-handler))

(defn save-basket []
    (srm/rpc
     (api/save-basket (serialise-basket)) [response]
     :on-success (do (js/alert (str "Basket saved! Id: " (:id response)))
                     (history/set-token (str (:id response)))
                     (set-basket-saved!)
                     (load-user-baskets))
     :on-error (js/alert (str "Error saving basket"))))


(def item-added (pubsub/publishize
                 (fn [] {:item-name (d/value item-name-field)
                         :qty (to-int (d/value item-qty-field))}) bus))

(defn on-enter [e f]
  (when (= 13 (:keyCode e)) (event/prevent-default e) (f)))

(defn on-plus [e f]
  (when (= 43 (:charCode e)) (event/prevent-default e) (f)))

(defn on-minus [e f]
  (when (= 45 (:charCode e)) (event/prevent-default e) (f)))

(event/listen! item-name-field :keypress
               #(on-plus % (partial quantity-changed :inc false item-name-field)))

(event/listen! item-name-field :keypress
               #(on-minus % (partial quantity-changed :dec false item-name-field)))

(event/listen! add-item-button :click #(item-added))
(event/listen! item-name-field :keypress #(on-enter % item-added))
(event/listen! item-qty-field :keypress #(on-enter % item-added))
(event/listen! save-basket-button :click #(save-basket))

(defn item-completed [{:keys [item]}]
  (d/add-class! item "btn-success")
  (d/append! completed-items-list (d/detach! (x/xpath item "../.."))))
