<commit_before>(ns holiday-ping-ui.channels.events
  (:require [clojure.string :as string]
            [re-frame.core :as re-frame]
            [ajax.core :as ajax]
            [holiday-ping-ui.common.events :as events]))

(defmethod events/load-view
  :channel-list
  [{:keys [db]} _]
  {:http-xhrio {:method          :get
                :uri             "/api/channels"
                :timeout         8000
                :headers         {:authorization (str "Bearer " (:access-token db))}
                :response-format (ajax/json-response-format {:keywords? true})
                :on-success      [:channel-list-success]
                :on-failure      [:error-message "Channel loading failed."]}})

(defmethod events/load-view
  :channel-edit
  [{:keys [db]} [_ channel-name]]
  {:http-xhrio {:method          :get
                :uri             (str "/api/channels/" channel-name)
                :timeout         8000
                :headers         {:authorization (str "Bearer " (:access-token db))}
                :response-format (ajax/json-response-format {:keywords? true})
                :on-success      [:channel-detail-success]
                :on-failure      [:switch-view :not-found]}})

(re-frame/reg-event-db
 :channel-list-success
 (fn [db [_ response]]
   (-> db
       (assoc :channels response)
       (assoc :loading-view? false))))

(re-frame/reg-event-db
 :channel-detail-success
 (fn [db [_ response]]
   (-> db
       (assoc :channel-to-edit response)
       (assoc :loading-view? false))))

(re-frame/reg-event-fx
 :channel-delete
 (fn [{db :db} [_ channel]]
   {:http-xhrio {:method          :delete
                 :uri             (str "/api/channels/" channel)
                 :timeout         8000
                 :headers         {:authorization (str "Bearer " (:access-token db))}
                 :format          (ajax/json-request-format)
                 :response-format (ajax/json-response-format {:keywords? true})
                 :on-success      [:channel-delete-success channel]
                 :on-failure      [:error-message "Channel deleting failed."]}}))

(re-frame/reg-event-db
 :channel-delete-success
 (fn [db [_ channel]]
   (update db :channels
           (fn [channels]
             (remove #(= (:name %) channel) channels)))))

(re-frame/reg-event-fx
 :channel-edit-submit
 (fn [{db :db} [_ {:keys [name type url targets username emoji days-before
                          same-day]}]]
   (let [targets     (string/split targets #"\s+")
         days-before (js/parseInt days-before)
         params      {:name          name
                      :type          type
                      :same_day      same-day
                      :days_before   (if (zero? days-before) nil days-before)
                      :configuration {:channels targets
                                      :url      url
                                      :username username
                                      :emoji    emoji}}]
     {:http-xhrio {:method          :put
                   :uri             (str "/api/channels/" name)
                   :headers         {:authorization (str "Bearer " (:access-token db))}
                   :timeout         8000
                   :format          (ajax/json-request-format)
                   :params          params
                   :response-format (ajax/text-response-format)
                   :on-success      [:navigate :channel-list]
                   :on-failure      [:error-message "Channel submission failed"]}})))

(re-frame/reg-event-fx
 :wizard-submit
 (fn [{db :db} [_ {:keys [type channel-config reminder-config]}]]
   (let [days-before  (js/parseInt (:days-before reminder-config))
         channel-name (:name channel-config)
         targets      (string/split (:targets channel-config) #"\s+")
         params       {:name          channel-name
                       :type          type
                       :same_day      (:same-day reminder-config)
                       :days_before   (if (zero? days-before) nil days-before)
                       :configuration (-> channel-config
                                          (assoc :channels targets)
                                          (dissoc :targets)
                                          (dissoc :name))}]
     {:db         (assoc db :loading-view? true)
      :http-xhrio {:method          :put
                   :uri             (str "/api/channels/" channel-name)
                   :headers         {:authorization (str "Bearer " (:access-token db))}
                   :timeout         8000
                   :format          (ajax/json-request-format)
                   :params          params
                   :response-format (ajax/text-response-format)
                   :on-success      [:wizard-submit-success channel-name]
                   :on-failure      [:error-message "Channel submission failed"]}})))<commit_msg>clean emoji<commit_after>(ns holiday-ping-ui.channels.events
  (:require [clojure.string :as string]
            [re-frame.core :as re-frame]
            [ajax.core :as ajax]
            [holiday-ping-ui.common.events :as events]))

(defmethod events/load-view
  :channel-list
  [{:keys [db]} _]
  {:http-xhrio {:method          :get
                :uri             "/api/channels"
                :timeout         8000
                :headers         {:authorization (str "Bearer " (:access-token db))}
                :response-format (ajax/json-response-format {:keywords? true})
                :on-success      [:channel-list-success]
                :on-failure      [:error-message "Channel loading failed."]}})

(defmethod events/load-view
  :channel-edit
  [{:keys [db]} [_ channel-name]]
  {:http-xhrio {:method          :get
                :uri             (str "/api/channels/" channel-name)
                :timeout         8000
                :headers         {:authorization (str "Bearer " (:access-token db))}
                :response-format (ajax/json-response-format {:keywords? true})
                :on-success      [:channel-detail-success]
                :on-failure      [:switch-view :not-found]}})

(re-frame/reg-event-db
 :channel-list-success
 (fn [db [_ response]]
   (-> db
       (assoc :channels response)
       (assoc :loading-view? false))))

(re-frame/reg-event-db
 :channel-detail-success
 (fn [db [_ response]]
   (-> db
       (assoc :channel-to-edit response)
       (assoc :loading-view? false))))

(re-frame/reg-event-fx
 :channel-delete
 (fn [{db :db} [_ channel]]
   {:http-xhrio {:method          :delete
                 :uri             (str "/api/channels/" channel)
                 :timeout         8000
                 :headers         {:authorization (str "Bearer " (:access-token db))}
                 :format          (ajax/json-request-format)
                 :response-format (ajax/json-response-format {:keywords? true})
                 :on-success      [:channel-delete-success channel]
                 :on-failure      [:error-message "Channel deleting failed."]}}))

(re-frame/reg-event-db
 :channel-delete-success
 (fn [db [_ channel]]
   (update db :channels
           (fn [channels]
             (remove #(= (:name %) channel) channels)))))

(defn clean-emoji
  [emoji]
  (let [emoji (string/trim emoji)]
    (when-not (string/blank? emoji)
      (str (when-not (= (first emoji) ":") ":")
           emoji
           (when-not (= (last emoji) ":") ":")))))

(re-frame/reg-event-fx
 :channel-edit-submit
 (fn [{db :db} [_ {:keys [name type url targets username emoji days-before
                          same-day]}]]
   (let [targets     (string/split targets #"\s+")
         days-before (js/parseInt days-before)
         params      {:name          name
                      :type          type
                      :same_day      same-day
                      :days_before   (if (zero? days-before) nil days-before)
                      :configuration {:channels targets
                                      :url      url
                                      :username username
                                      :emoji    (clean-emoji emoji)}}]
     {:http-xhrio {:method          :put
                   :uri             (str "/api/channels/" name)
                   :headers         {:authorization (str "Bearer " (:access-token db))}
                   :timeout         8000
                   :format          (ajax/json-request-format)
                   :params          params
                   :response-format (ajax/text-response-format)
                   :on-success      [:navigate :channel-list]
                   :on-failure      [:error-message "Channel submission failed"]}})))

(re-frame/reg-event-fx
 :wizard-submit
 (fn [{db :db} [_ {:keys [type channel-config reminder-config]}]]
   (let [days-before  (js/parseInt (:days-before reminder-config))
         channel-name (:name channel-config)
         targets      (string/split (:targets channel-config) #"\s+")
         params       {:name          channel-name
                       :type          type
                       :same_day      (:same-day reminder-config)
                       :days_before   (if (zero? days-before) nil days-before)
                       :configuration (-> channel-config
                                          (update :emoji clean-emoji)
                                          (assoc :channels targets)
                                          (dissoc :targets)
                                          (dissoc :name))}]
     {:db         (assoc db :loading-view? true)
      :http-xhrio {:method          :put
                   :uri             (str "/api/channels/" channel-name)
                   :headers         {:authorization (str "Bearer " (:access-token db))}
                   :timeout         8000
                   :format          (ajax/json-request-format)
                   :params          params
                   :response-format (ajax/text-response-format)
                   :on-success      [:wizard-submit-success channel-name]
                   :on-failure      [:error-message "Channel submission failed"]}})))
{'fraction_non_alphanumeric': 0.12571092371053147, 'fraction_numerical': 0.003922337713277113, 'mean_word_length': 1.3440588370489543, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491068', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2625, 'n_words': 620}