<commit_before>(ns elevent-client.core
    (:require
      [clojure.set :as set]
      [clojure.string :as str]

      [goog.crypt.base64 :as b64]
      [goog.events :as events]
      [goog.history.EventType :as EventType]

      [cljsjs.react :as react]
      [reagent.core :as r :refer [atom]]

      [alandipert.storage-atom :refer [local-storage]]
      [ajax.core :refer [DELETE GET POST PUT]]
      [cljs-time.coerce :refer [from-string from-long to-long]]
      [cljs-time.core :refer [after? at-midnight now plus hours]]
      [cljs-time.format :refer [formatter formatters unparse parse]]
      [datascript :as d]
      [garden.core :refer [css]]
      [secretary.core :as secretary :refer-macros [defroute]]
      [validateur.validation :refer [format-of
                                     inclusion-of
                                     numericality-of
                                     presence-of
                                     valid?
                                     validation-set]])
    (:require-macros [elevent-client.core :refer [endpoints]])
    (:import goog.History))


(.initializeTouchEvents js/React true)


;; Date and Time formats
;; =============================================================================

(def date-format "MMMM d, yyyy")
(def time-format "h:mm a")
(def datetime-format (str time-format " " date-format))
(def input-date-time-format "hh:mm a YYYY-MM-dd")
(def date-formatter (formatter date-format))
(def time-formatter (formatter time-format))
(def datetime-formatter (formatter datetime-format))
(def input-date-time-formatter (formatter input-date-time-format))



;; Location
;; =============================================================================

(defonce location (atom ""))


;; Session
;; =============================================================================
;;
;; An atom that stores state which should be persisted in LocalStorage

(def session (local-storage (atom {}) :session))


;; Messages
;; =============================================================================

(defonce messages (atom {}))
(defn add-message! [type message]
  (swap! messages assoc (keyword (gensym)) [type message]))


(def api-url "https://elevent.solutions:44300")


;; REST
;; =============================================================================

(defn endpoint [uri element-id state]
  (fn dispatch!
    ([operation params handler error-handler]
     (let [options {:format          :json

                    :response-format (when (= operation :read) :json)

                    :keywords?       true

                    :timeout         8000

                    :headers
                    (if (:token @session)
                      {:Authentication
                       (str "Bearer " (:token @session))}
                      {})

                    :params
                    (when (or (= operation :create)
                              (= operation :update)
                              (= operation :bulk))
                      params)

                    :handler
                    (if (= operation :read)
                      (fn [json]
                        (reset! state json)
                        (when handler (handler json)))
                      (fn [json]
                        (when handler (handler json))
                        (dispatch! :read nil nil nil)))

                    :error-handler
                    (fn [error]
                      (if error-handler
                        (error-handler error)
                        (if (= (:failure error) :timeout)
                          (add-message! :negative (str uri " timed out"))
                          (add-message! :negative (str uri (js->clj error))))))}]
       (let [check-id (fn [op] (if (contains? params element-id)
                                 (op (str uri "/" (params element-id)) options)
                                 (throw (str "Element doesn't contain key \""
                                             (prn-str element-id)
                                             "\": "
                                             (prn-str params)))))]
         (case operation
           :create (POST uri options)
           :read   (GET  uri options)
           :update (check-id PUT)
           :delete (check-id DELETE)
           (POST (str uri "/" (name operation)) options)))))
    ([operation params handler]
     (dispatch! operation params handler nil))))

(endpoints
  [attendees     (str api-url "/attendees")     :AttendeeId     true]
  [organizations (str api-url "/organizations") :OrganizationId false]
  [events        (str api-url "/events")        :EventId        false]
  [activities    (str api-url "/activities")    :ActivityId     false]
  [users         (str api-url "/users")         :UserId         true]
  [schedules     (str api-url "/schedules")     :ScheduleId     true])
<commit_msg>Sign out user when API call returns 401 error<commit_after>(ns elevent-client.core
    (:require
      [clojure.set :as set]
      [clojure.string :as str]

      [goog.crypt.base64 :as b64]
      [goog.events :as events]
      [goog.history.EventType :as EventType]

      [cljsjs.react :as react]
      [reagent.core :as r :refer [atom]]

      [alandipert.storage-atom :refer [local-storage]]
      [ajax.core :refer [DELETE GET POST PUT]]
      [cljs-time.coerce :refer [from-string from-long to-long]]
      [cljs-time.core :refer [after? at-midnight now plus hours]]
      [cljs-time.format :refer [formatter formatters unparse parse]]
      [datascript :as d]
      [garden.core :refer [css]]
      [secretary.core :as secretary :refer-macros [defroute]]
      [validateur.validation :refer [format-of
                                     inclusion-of
                                     numericality-of
                                     presence-of
                                     valid?
                                     validation-set]])
    (:require-macros [elevent-client.core :refer [endpoints]])
    (:import goog.History))


(.initializeTouchEvents js/React true)


;; Date and Time formats
;; =============================================================================

(def date-format "MMMM d, yyyy")
(def time-format "h:mm a")
(def datetime-format (str time-format " " date-format))
(def input-date-time-format "hh:mm a YYYY-MM-dd")
(def date-formatter (formatter date-format))
(def time-formatter (formatter time-format))
(def datetime-formatter (formatter datetime-format))
(def input-date-time-formatter (formatter input-date-time-format))



;; Location
;; =============================================================================

(defonce location (atom ""))


;; Session
;; =============================================================================
;;
;; An atom that stores state which should be persisted in LocalStorage

(def session (local-storage (atom {}) :session))


;; Messages
;; =============================================================================

(defonce messages (atom {}))
(defn add-message! [type message]
  (swap! messages assoc (keyword (gensym)) [type message]))


(def api-url "https://elevent.solutions:44300")


;; REST
;; =============================================================================

(declare sign-out!)

(defn endpoint [uri element-id state]
  (fn dispatch!
    ([operation params handler error-handler]
     (let [options {:format          :json

                    :response-format (when (= operation :read) :json)

                    :keywords?       true

                    :timeout         8000

                    :headers
                    (if (:token @session)
                      {:Authentication
                       (str "Bearer " (:token @session))}
                      {})

                    :params
                    (when (or (= operation :create)
                              (= operation :update)
                              (= operation :bulk))
                      params)

                    :handler
                    (if (= operation :read)
                      (fn [json]
                        (reset! state json)
                        (when handler (handler json)))
                      (fn [json]
                        (when handler (handler json))
                        (dispatch! :read nil nil nil)))

                    :error-handler
                    (fn [error]
                      (if error-handler
                        (error-handler error)
                        (if (= (:status error) 401)
                          (sign-out!)
                          (if (= (:failure error) :timeout)
                            (add-message! :negative (str uri " timed out"))
                            (add-message! :negative (str uri (js->clj error)))))))}]
       (let [check-id (fn [op] (if (contains? params element-id)
                                 (op (str uri "/" (params element-id)) options)
                                 (throw (str "Element doesn't contain key \""
                                             (prn-str element-id)
                                             "\": "
                                             (prn-str params)))))]
         (case operation
           :create (POST uri options)
           :read   (GET  uri options)
           :update (check-id PUT)
           :delete (check-id DELETE)
           (POST (str uri "/" (name operation)) options)))))
    ([operation params handler]
     (dispatch! operation params handler nil))))

(endpoints
  [attendees     (str api-url "/attendees")     :AttendeeId     true]
  [organizations (str api-url "/organizations") :OrganizationId false]
  [events        (str api-url "/events")        :EventId        false]
  [activities    (str api-url "/activities")    :ActivityId     false]
  [users         (str api-url "/users")         :UserId         true]
  [schedules     (str api-url "/schedules")     :ScheduleId     true])

{'fraction_non_alphanumeric': 0.18546616091036136, 'fraction_numerical': 0.003194250349371132, 'mean_word_length': 1.6617959617428268, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5375354', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2423, 'n_words': 697}