<commit_before>(ns souptitle-desktop.events
  (:require [souptitle-desktop.db :refer [default-db]]
            [re-frame.core :refer [reg-event-db reg-event-fx reg-fx dispatch]]
            [serials-model.core :as sm]
            [souptitle-desktop.common.utils.load-srt :refer [load-srt]]))

;; ----- Effects -------------

(reg-fx
 :load-srt
 (fn load-srt-effect [{:keys [url on-load]}]
   (-> (load-srt url)
       (.then #(dispatch [on-load %]))
       (.catch #(print %)))))

;; ----- Event Handlers -------

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

(reg-event-db
 :set-active-serial
 (fn [db [_ id]]
   (-> db
       (assoc :active-content id))))

(reg-event-db
 :set-active-season
 (fn [db [_ id]]
   (-> db
       (assoc :active-content id))))

(reg-event-db
 :set-active-chapter
 (fn [db [_ id]]
   (-> db
       (assoc :active-content id))))

(defn update-chapter-meta [key]
  (fn [{:keys [content active-content] :as db} [_ val]]
    (-> db
        (assoc
         :content
         (sm/update-chapter
          content
          active-content
          (fn [chapter]
            (assoc-in chapter [:meta key] val)))))))

(reg-event-db
 :update-chapter-title
 (update-chapter-meta :title))

(reg-event-db
 :update-chapter-raw-srt
 (update-chapter-meta :src))

(reg-event-fx
 :load-link-with-srt
 (fn [_ [_ url]]
   {:load-srt {:url url :on-load :update-chapter-raw-srt}}))<commit_msg>desktop: Create events for new entities creation<commit_after>(ns souptitle-desktop.events
  (:require [souptitle-desktop.db :refer [default-db]]
            [re-frame.core :refer [reg-event-db reg-event-fx reg-fx dispatch]]
            [serials-model.core :as sm]
            [souptitle-desktop.common.utils.load-srt :refer [load-srt]]))

;; ----- Effects -------------

(reg-fx
 :load-srt
 (fn load-srt-effect [{:keys [url on-load]}]
   (-> (load-srt url)
       (.then #(dispatch [on-load %]))
       (.catch #(print %)))))

;; ----- Event Handlers -------

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

(reg-event-db
 :set-active-serial
 (fn [db [_ id]]
   (-> db
       (assoc :active-content id))))

(reg-event-db
 :set-active-season
 (fn [db [_ id]]
   (-> db
       (assoc :active-content id))))

(reg-event-db
 :set-active-chapter
 (fn [db [_ id]]
   (-> db
       (assoc :active-content id))))

(defn update-chapter-meta [key]
  (fn [{:keys [content active-content] :as db} [_ val]]
    (-> db
        (assoc
         :content
         (sm/update-chapter
          content
          active-content
          (fn [chapter]
            (assoc-in chapter [:meta key] val)))))))

(reg-event-db
 :update-chapter-title
 (update-chapter-meta :title))

(reg-event-db
 :update-chapter-raw-srt
 (update-chapter-meta :src))

;; TODO: make new entity active

(reg-event-db
 :add-new-serial
 (fn [{:keys [content active-content] :as db} [_ val]]
   (-> db
       (update
        :content
        (fn [content]
          (-> content
              (conj (sm/create-node {:title "New serial"}))))))))

(reg-event-db
 :add-new-season
 (fn [{:keys [content active-content] :as db} [_ val]]
   (-> db
       (update
        :content
        (sm/update-seasons
         content
         active-content
         (fn [seasons]
           (-> seasons
               (conj (sm/create-node {:title "New season"})))))))))

(reg-event-db
 :add-new-chapter
 (fn [{:keys [content active-content] :as db} [_ val]]
   (-> db
       (update
        :content
        (sm/update-chapters
         content
         active-content
         (fn [chapters]
           (-> chapters
               (conj (sm/create-leaf {:title "New season"})))))))))

(reg-event-fx
 :load-link-with-srt
 (fn [_ [_ url]]
   {:load-srt {:url url :on-load :update-chapter-raw-srt}}))
{'fraction_non_alphanumeric': 0.19356554107950014, 'fraction_numerical': 0.0, 'mean_word_length': 2.1323896752706077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '1081171', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1322, 'n_words': 290}