(ns com.oly.static-sites.do-blog.pages.archive
  (:require [reagent.core :as r]
            [reitit.frontend.easy :as rfe]
            [tachyons.definition-lists :refer [simple]]
            [com.oly.static-sites.do-blog.pages.helpers.utils :refer [format-date]]
            [com.oly.static-sites.do-blog.pages.helpers.database :refer [search-articles state]]))

(defn archive-page [{:keys [path-params] :as route}]
  (let [search (r/cursor state [:search :terms])
        org-index (r/reaction (search-articles (merge {:search @search} path-params)))]
    (fn [{:keys [path-params] :as route}]
      [:div {:key "search"}
       [:h1 "Past articles"]
       [simple
        {:list
         (map-indexed (fn [idx doc]
                        {:text  [:div [:a.f4.lh-copy {:href (str (rfe/href :posts {:path ""}) (:relative-path doc))
                                                      :key idx}
                                       (str (format-date (:date doc))
                                            #_(convert-date (:date doc)) " " (:title doc))]]})
                      @org-index)}]])))
