  validate-spec
  (fn [cofx [_ stories]]
    {:db (-> (update (:db cofx) :stories #(concat % stories))
-            (update-in [:front-page :current-page-num] inc))
-    }))
+            (update-in [:front-page :current-page-num] inc))}))
 
 (reg-event-fx
  :failed-loading-front-page-stories
  validate-spec
  (fn [cofx [_ error-response]]
    (throw (ex-info (str error-response "Failed loading stories") {:response error-response}))
-   {:db (:db cofx)}
-   ))
+   {:db (:db cofx)}))
 
 (reg-event-fx
  :loaded-story-comments

(ns hackernews.events
  (:require
   [re-frame.core :refer [reg-event-db after reg-event-fx reg-cofx reg-fx]]
   [ajax.core :as ajax]
   [clojure.spec :as s]
   [hackernews.navigation :as ios-nav]


(def hn-api "https://node-hnapi.herokuapp.com")

(defn- url-encode
  [string]
  (some-> string str (js/encodeURIComponent) (.replace "+" "%20")))

(defn- map->query
  [m]
  (some->> (seq m)
           sort
           (map (fn [[k v]]
                  [(url-encode (name k))
                   "="
                   (url-encode (str v))]))
           (interpose "&")
           flatten
           (apply str)))

(defn fetch
  [{:keys [url params method body on-success on-failure] :as request}]
  (-> (str url "?" (map->query params))
      (js/fetch (clj->js (select-keys request [:method :body])))
      (.then #(.json %))
      (.then #(js->clj % :keywordize-keys true))
      (.then #(on-success %))
      (.catch #(on-failure %))))

(reg-fx
 :fetch-http
 (fn [{:as   request
       :keys [url params method on-success on-failure]
       :or   {on-success      [:http-no-on-success]
              on-failure      [:http-no-on-failure]}}]
   (fetch (assoc request
                 :on-success #(dispatch (conj on-success %))
                 :on-failure #(dispatch (conj on-failure %))))))

(reg-event-fx
 :load-front-page-stories
 (fn [{:keys [db]} [_]]
   {:fetch-http {:method          :get
                 :url             (str hn-api "/news")
                 :params  {:page  (get-in db [:front-page :current-page-num])}
                 :timeout         8000
                 :response-format (ajax/json-response-format {:keywords? true})
                 :on-success      [:loaded-front-page-stories]
