<commit_before>(ns stonecutter.view.sign-in
  (:require [traduki.core :as t]
            [net.cgrand.enlive-html :as html]
            [stonecutter.routes :as r]
            [stonecutter.view.view-helpers :as vh]))

(defn add-error-class [enlive-m field-row-selector]
  (html/at enlive-m field-row-selector (html/add-class "form-row--validation-error")))

(def error-translations
  {:email {:invalid "content:sign-in-form/email-address-invalid-validation-message"
           :too-long "content:sign-in-form/email-address-too-long-validation-message"}
   :password {:blank "content:sign-in-form/password-blank-validation-message"
              :too-long "content:sign-in-form/password-too-long-validation-message"
              :too-short "content:sign-in-form/password-too-short-validation-message"}
   :credentials {:invalid "content:sign-in-form/invalid-credentials-validation-message"}})

(defn set-registration-link [enlive-m]
  (html/at enlive-m
           [:.clj--register__link] (html/set-attr :href (r/path :show-registration-form))))

(defn add-email-error [enlive-m err]
  (if (contains? err :email)
    (let [error-translation (get-in error-translations [:email (:email err)])]
      (-> enlive-m
          (add-error-class [:.clj--sign-in-email])
          (html/at [:.clj--sign-in-email__validation] (html/set-attr :data-l8n (or error-translation "content:sign-in-form/unknown-error")))))
    (vh/remove-element enlive-m [:.clj--sign-in-email__validation])))

(defn add-password-error [enlive-m err]
  (if (contains? err :password)
    (let [error-translation (get-in error-translations [:password (:password err)])]
      (-> enlive-m
          (add-error-class [:.clj--sign-in-password])
          (html/at [:.clj--sign-in-password__validation] (html/set-attr :data-l8n (or error-translation "content:sign-in-form/unknown-error")))))
    (vh/remove-element enlive-m [:.clj--sign-in-password__validation])))

(defn add-invalid-credentials-error [enlive-m err]
  (if (contains? err :credentials)
    (let [error-translation (get-in error-translations [:credentials (:credentials err)])]
      (-> enlive-m
          (html/at [:.clj--validation-summary] (html/add-class "validation-summary--show"))
          (html/at [:.clj--validation-summary__item] (html/set-attr :data-l8n (or error-translation "content:sign-in-form/unknown-error")))))
    (vh/remove-element enlive-m [:.clj--validation-summary])))

(defn add-sign-in-errors [err enlive-m]
  (-> enlive-m
      (add-email-error err)
      (add-password-error err)
      (add-invalid-credentials-error err)))

(defn add-params [params enlive-m]
  (html/at enlive-m
           [:.clj--email__input] (html/set-attr :value (:email params))))

(defn set-form-action [enlive-m]
  (html/at enlive-m [:form] (html/set-attr :action (r/path :sign-in))))

(defn sign-in-form [request]
  (let [context (:context request)
        params (:params request)
        err (:errors context)]
    (->> (vh/load-template "public/sign-in.html")
         set-registration-link
         set-form-action
         (add-sign-in-errors err)
         (add-params params)
         vh/add-anti-forgery)))<commit_msg>CW |0| remove work in progress in sign in page<commit_after>(ns stonecutter.view.sign-in
  (:require [traduki.core :as t]
            [net.cgrand.enlive-html :as html]
            [stonecutter.routes :as r]
            [stonecutter.view.view-helpers :as vh]))

(defn add-error-class [enlive-m field-row-selector]
  (html/at enlive-m field-row-selector (html/add-class "form-row--validation-error")))

(def error-translations
  {:email {:invalid "content:sign-in-form/email-address-invalid-validation-message"
           :too-long "content:sign-in-form/email-address-too-long-validation-message"}
   :password {:blank "content:sign-in-form/password-blank-validation-message"
              :too-long "content:sign-in-form/password-too-long-validation-message"
              :too-short "content:sign-in-form/password-too-short-validation-message"}
   :credentials {:invalid "content:sign-in-form/invalid-credentials-validation-message"}})

(defn set-registration-link [enlive-m]
  (html/at enlive-m
           [:.clj--register__link] (html/set-attr :href (r/path :show-registration-form))))

(defn add-email-error [enlive-m err]
  (if (contains? err :email)
    (let [error-translation (get-in error-translations [:email (:email err)])]
      (-> enlive-m
          (add-error-class [:.clj--sign-in-email])
          (html/at [:.clj--sign-in-email__validation] (html/set-attr :data-l8n (or error-translation "content:sign-in-form/unknown-error")))))
    (vh/remove-element enlive-m [:.clj--sign-in-email__validation])))

(defn add-password-error [enlive-m err]
  (if (contains? err :password)
    (let [error-translation (get-in error-translations [:password (:password err)])]
      (-> enlive-m
          (add-error-class [:.clj--sign-in-password])
          (html/at [:.clj--sign-in-password__validation] (html/set-attr :data-l8n (or error-translation "content:sign-in-form/unknown-error")))))
    (vh/remove-element enlive-m [:.clj--sign-in-password__validation])))

(defn add-invalid-credentials-error [enlive-m err]
  (if (contains? err :credentials)
    (let [error-translation (get-in error-translations [:credentials (:credentials err)])]
      (-> enlive-m
          (html/at [:.clj--validation-summary] (html/add-class "validation-summary--show"))
          (html/at [:.clj--validation-summary__item] (html/set-attr :data-l8n (or error-translation "content:sign-in-form/unknown-error")))))
    (vh/remove-element enlive-m [:.clj--validation-summary])))

(defn add-sign-in-errors [err enlive-m]
  (-> enlive-m
      (add-email-error err)
      (add-password-error err)
      (add-invalid-credentials-error err)))

(defn add-params [params enlive-m]
  (html/at enlive-m
           [:.clj--email__input] (html/set-attr :value (:email params))))

(defn set-form-action [enlive-m]
  (html/at enlive-m [:form] (html/set-attr :action (r/path :sign-in))))

(defn sign-in-form [request]
  (let [context (:context request)
        params (:params request)
        err (:errors context)]
    (->> (vh/load-template "public/sign-in.html")
         set-registration-link
         set-form-action
         (add-sign-in-errors err)
         (add-params params)
         vh/remove-work-in-progress
         vh/add-anti-forgery)))
{'fraction_non_alphanumeric': 0.17323081751137612, 'fraction_numerical': 0.00109838380668445, 'mean_word_length': 3.1175710594315245, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6518453', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2184, 'n_words': 391}