<commit_before>(ns ^:figwheel-hooks tutorial.moan.frontend.main
  (:require-macros
    [tutorial.moan.frontend.main :refer [html]])
  (:require
    [brutha.core :as br]
    [cljs.reader :refer [read-string]]))

(defonce state (atom {:page {:name :home}}))

(defn update-tweet
  [tweets id f & args]
  (map
    (fn [tweet]
      (if (= id (:id tweet))
        (apply f tweet args)
        tweet))
    tweets))

(def Tweet
  (br/component
    'Tweet
    (fn [tweet]
      (html
        [:div.card.tweet
         [:div.tweet__content
          [:span.tweet__author-name (-> tweet :author :name)]
          [:a.tweet__author-username
           {:href "javascript:;"
            :onClick (fn [e]
                       (.preventDefault e))}
           (str "@" (-> tweet :author :username))]
          [:p.tweet__body (:text tweet)]]
         [:div.tweet__actions
          [:a.tweet__action {:href "#"}
           [:span.eye.tweet__action-icon]
           "Hide"]
          [:a.tweet__action
           {:href "#"
            :class (when (:favorite? tweet)
                     "tweet__action--active")
            :onClick (fn [e]
                       (.preventDefault e)
                       (swap! state
                              update :tweets
                              update-tweet (:id tweet)
                              update :favorite? not))}
           [:span.star.tweet__action-icon]
           "Favorite"]]]))))

(defn Tweets
  [tweets]
  (html
    [:div.tweets
     (for [tweet tweets]
       (when-not (:hidden? tweet)
         (Tweet tweet {:key (:id tweet)})))]))

(defn Loader
  []
  (html
    [:div.Aligner
     [:div.loader
      [:div.loader__box]
      [:div.loader__hill]
      [:p.loader__text "Fetching data…"]]]))

(defn home
  [state]
  (html
    (Tweets (-> state :page :data))))

(defn favorites
  [state]
  (html (Tweets (-> state :page :data))))

(defmulti page-fetch :name)

(defmethod page-fetch :favorites
  [_]
  (js/fetch "/favorites"))

(defmethod page-fetch :home
  [_]
  (js/fetch "/all"))

(defmethod page-fetch :default
  [_]
  nil)

(defn fetch-page
  [page]
  (some-> (page-fetch page)
          (.then (fn [data] (.text data)))
          (.then (fn [text] (read-string text)))
          (.then (fn [x] (swap! state (fn [state]
                                        (if (= page (:page state))
                                          (assoc-in state [:page :data] x)
                                          state)))))))

(defn nav-link
  [opts & children]
  (let [children (if (map? opts)
                   children
                   (cons opts children))]
    (html
      [:li.primary-nav__item
       [:a {:href "javascript:;"
            :onClick (fn [e]
                       (.preventDefault e)
                       (when-let [new-page (:new-page opts)]
                         (swap! state assoc :page new-page)
                         (fetch-page new-page)))}
        children]])))
<commit_msg>Add favorite endpoint to frontend<commit_after>(ns ^:figwheel-hooks tutorial.moan.frontend.main
  (:require-macros
    [tutorial.moan.frontend.main :refer [html]])
  (:require
    [brutha.core :as br]
    [cljs.reader :refer [read-string]]))

(defonce state (atom {:page {:name :home}}))

(declare fetch-global-page)

(defn update-tweet
  [tweets id f & args]
  (map
    (fn [tweet]
      (if (= id (:id tweet))
        (apply f tweet args)
        tweet))
    tweets))

(def Tweet
  (br/component
    'Tweet
    (fn [tweet]
      (html
        [:div.card.tweet
         [:div.tweet__content
          [:span.tweet__author-name (-> tweet :author :name)]
          [:a.tweet__author-username
           {:href "javascript:;"
            :onClick (fn [e]
                       (.preventDefault e))}
           (str "@" (-> tweet :author :username))]
          [:p.tweet__body (:text tweet)]]
         [:div.tweet__actions
          [:a.tweet__action {:href "#"}
           [:span.eye.tweet__action-icon]
           "Hide"]
          [:a.tweet__action
           {:href "#"
            :class (when (:favorite? tweet)
                     "tweet__action--active")
            :onClick (fn [e]
                       (.preventDefault e)
                       (->
                         (js/fetch (str "/" (:id tweet) "/favorite")
                                   #js {:method "POST"})
                         (.then (fn [_]
                                  (fetch-global-page)))))}
           [:span.star.tweet__action-icon]
           "Favorite"]]]))))

(defn Tweets
  [tweets]
  (html
    [:div.tweets
     (for [tweet tweets]
       (when-not (:hidden? tweet)
         (Tweet tweet {:key (:id tweet)})))]))

(defn Loader
  []
  (html
    [:div.Aligner
     [:div.loader
      [:div.loader__box]
      [:div.loader__hill]
      [:p.loader__text "Fetching data…"]]]))

(defn home
  [state]
  (html
    (Tweets (-> state :page :data))))

(defn favorites
  [state]
  (html (Tweets (-> state :page :data))))

(defmulti page-fetch :name)

(defmethod page-fetch :favorites
  [_]
  (js/fetch "/favorites"))

(defmethod page-fetch :home
  [_]
  (js/fetch "/all"))

(defmethod page-fetch :default
  [_]
  nil)

(defn fetch-page
  [page]
  (some-> (page-fetch page)
          (.then (fn [data] (.text data)))
          (.then (fn [text] (read-string text)))
          (.then (fn [x] (swap! state (fn [state]
                                        (if (= page (:page state))
                                          (assoc-in state [:page :data] x)
                                          state)))))))

(defn fetch-global-page
  []
  (fetch-page (-> @state :page)))

(defn nav-link
  [opts & children]
  (let [children (if (map? opts)
                   children
                   (cons opts children))]
    (html
      [:li.primary-nav__item
       [:a {:href "javascript:;"
            :onClick (fn [e]
                       (.preventDefault e)
                       (when-let [new-page (:new-page opts)]
                         (swap! state assoc :page new-page)
                         (fetch-page new-page)))}
        children]])))

{'fraction_non_alphanumeric': 0.15487884741322855, 'fraction_numerical': 0.0, 'mean_word_length': 1.5275134464211833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4258172', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1922, 'n_words': 458}