<commit_before>(ns just-married.home.handlers
  (:require [re-frame.core :as re-frame :refer [reg-sub dispatch reg-event-db reg-event-fx]]
            [ajax.core :as ajax]
            [just-married.home.config :as config]
            [day8.re-frame.http-fx]))

(def default-db
  ;; what other possibly useful information could be here?
  {:language       :en
   :expanded-story false
   :rvsp           {:name     nil
                    :email    nil
                    :how-many config/default-how-many
                    :comment  nil}})

(defn- getter
  [key]
  (fn [db _]
    (key db)))

(defn- setter
  [key]
  (fn [db [_ val]]
    (assoc-in db key val)))

(reg-sub
 :expanded-story
 (getter :expanded-story))

;; register simple setters for all the rvsp fields dynamically
(doseq [field (-> default-db :rvsp keys)]
  (reg-event-db
   (keyword (str "set-" (name field)))
   (setter [:rvsp field])))

(defn rvsp
  [{:keys [db]} [_ value]]
  {:db         db
   :http-xhrio {:method          :post
                :uri             "/api/rvsp"
                :params          (assoc (:rvsp db) :coming value)
                :format          (ajax/json-request-format)
                :response-format (ajax/json-response-format {:keywords? true})
                :on-success      [:confirmation-sent]
                :on-failure      [:confirmation-not-sent]}})

(reg-event-fx
 :send-notification
 rvsp)

(reg-event-db
 :confirmation-sent
 (fn [db [_ response]]
   (js/alert (:original-text response))
   db))

(reg-event-db
 :confirmation-not-sent
 (fn [db [_ response]]
   (js/alert "Could not submit")
   db))

(reg-event-db
 :initialize-db
 (fn  [_ _]
   default-db))

(reg-event-db
 :set-expanded-story
 (fn [db [_ expanded-story]]
   (assoc db :expanded-story expanded-story)))<commit_msg>handle both cases in the same way<commit_after>(ns just-married.home.handlers
  (:require [re-frame.core :as re-frame :refer [reg-sub dispatch reg-event-db reg-event-fx]]
            [ajax.core :as ajax]
            [just-married.home.config :as config]
            [day8.re-frame.http-fx]))

(def default-db
  ;; what other possibly useful information could be here?
  {:language       :en
   :expanded-story false
   :rvsp           {:name     nil
                    :email    nil
                    :how-many config/default-how-many
                    :comment  nil}})

(defn- getter
  [key]
  (fn [db _]
    (key db)))

(defn- setter
  [key]
  (fn [db [_ val]]
    (assoc-in db key val)))

(reg-sub
 :expanded-story
 (getter :expanded-story))

;; register simple setters for all the rvsp fields dynamically
(doseq [field (-> default-db :rvsp keys)]
  (reg-event-db
   (keyword (str "set-" (name field)))
   (setter [:rvsp field])))

(defn rvsp
  [{:keys [db]} [_ value]]
  {:db         db
   :http-xhrio {:method          :post
                :uri             "/api/rvsp"
                :params          (assoc (:rvsp db) :coming value)
                ;; could use EDN as well here potentially
                :format          (ajax/json-request-format)
                :response-format (ajax/json-response-format {:keywords? true})
                :on-success      [:confirmation-sent]
                :on-failure      [:confirmation-not-sent]}})

(reg-event-fx
 :send-notification
 rvsp)

(defn handle
  [response]
  (when (= 201 (:status response))
    (js/alert "Thanks for letting us know")))

;;XXX: getting strangely cjls-ajax to report failure
;;even if in fact it worked and it was a 201 response
(reg-event-db
 :confirmation-sent
 (fn [db [_ response]]
   (handle response)
   db))

(reg-event-db
 :confirmation-not-sent
 (fn [db [_ response]]
   (handle response)
   db))

(reg-event-db
 :initialize-db
 (fn  [_ _]
   default-db))

(reg-event-db
 :set-expanded-story
 (fn [db [_ expanded-story]]
   (assoc db :expanded-story expanded-story)))
{'fraction_non_alphanumeric': 0.140527950310559, 'fraction_numerical': 0.002070393374741201, 'mean_word_length': 2.0895283772981617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6122235', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1259, 'n_words': 337}