<commit_before>/**
 * This file is covered by the AGPLv3 license, which can be found at the LICENSE file in the root of this project.
 * @copyright 2020 subtitulamos.tv
 */
import "./search";
import {
  onDomReady,
  easyFetch,
  $getEle,
  showOverlayFromTpl,
  closeOverlay,
  $getAllEle,
  invertDropdown,
  invertCheckbox,
} from "./utils";

function doLogin(e) {
  e.preventDefault();

  const $loginError = document.getElementById("login-error");
  const $pwdField = document.getElementById("password");
  const username = document.getElementById("username").value.trim();
  const pwd = $pwdField.value;

  const $loginBtn = document.getElementById("login-button");
  $loginBtn.classList.toggle("is-loading", true);
  $loginError.innerHTML = ""; // Clear previous errors, just so it's clearer they're new

  // Login the user via ajax
  easyFetch("/login", {
    method: "post",
    rawBody: {
      username: username,
      password: <PASSWORD>,
      // remember: document.getElementById("login_remember_me").checked, // FIXME: This should exist?
    },
  })
    .then(function () {
      window.location.reload();
    })
    .catch((err) => {
      $loginBtn.classList.toggle("is-loading", false);
      $pwdField.classList.toggle("is-danger", true);
      $loginError.classList.toggle("hidden", false);

      err.response
        .json()
        .then((cleanErr) => {
          $loginError.innerHTML = cleanErr[0];
        })
        .catch(() => {
          $loginError.innerHTML =
            "Error desconocido al intentar acceder. Por favor, inténtalo de nuevo.";
        });
    });
}

function register(e) {
  e.preventDefault();

  // Mark button as loading
  const $regButton = document.getElementById("register-button");
  $regButton.classList.toggle("is-loading", true);

  // Clean up old errors
  document
    .querySelectorAll("#register-form .is-danger")
    .forEach(($ele) => $ele.classList.toggle("is-danger"));
  document.querySelectorAll("#register-form [data-reg-error]").forEach(($ele) => $ele.remove());

  // Login the user via ajax
  easyFetch("/register", {
    method: "post",
    rawBody: {
      username: document.getElementById("reg_username").value,
      password: document.getElementById("reg_password").value,
      password_confirmation: document.getElementById("reg_password_confirmation").value,
      email: document.getElementById("reg_email").value,
      terms: document.getElementById("reg_terms").checked,
    },
  })
    .then(function () {
      window.location.reload(true);
    })
    .catch((err) => {
      $regButton.classList.toggle("is-loading", false);

      err.response
        .json()
        .then((cleanErrList) => {
          cleanErrList.forEach((curErr) => {
            const field = Object.keys(curErr)[0];
            const $field = document.getElementById("reg_" + field);
            $field.classList.toggle("is-danger", true);

            const $error = document.createElement("p");
            $error.classList.add("help", "is-danger");
            $error.dataset["regError"] = "";
            $error.innerHTML = curErr[field];
            $field.parentNode.parentNode.appendChild($error);
          });
        })
        .catch(() => {
          alert(
            "Error desconocido al intentar completar el registro. Por favor, inténtalo de nuevo."
          );
        });
    });
}

function showLoginForm() {
  showOverlayFromTpl("tpl-login");

  const $loginForm = document.getElementById("login-form");
  $loginForm.addEventListener("submit", doLogin);
  document.getElementById("username").focus();
}
<commit_msg>Fix login autocomplete showing up too early on Firefox<commit_after>/**
 * This file is covered by the AGPLv3 license, which can be found at the LICENSE file in the root of this project.
 * @copyright 2020 subtitulamos.tv
 */
import "./search";
import {
  onDomReady,
  easyFetch,
  $getEle,
  showOverlayFromTpl,
  closeOverlay,
  $getAllEle,
  invertDropdown,
  invertCheckbox,
} from "./utils";

function doLogin(e) {
  e.preventDefault();

  const $loginError = document.getElementById("login-error");
  const $pwdField = document.getElementById("password");
  const username = document.getElementById("username").value.trim();
  const pwd = $pwdField.value;

  const $loginBtn = document.getElementById("login-button");
  $loginBtn.classList.toggle("is-loading", true);
  $loginError.innerHTML = ""; // Clear previous errors, just so it's clearer they're new

  // Login the user via ajax
  easyFetch("/login", {
    method: "post",
    rawBody: {
      username: username,
      password: <PASSWORD>,
      // remember: document.getElementById("login_remember_me").checked, // FIXME: This should exist?
    },
  })
    .then(function () {
      window.location.reload();
    })
    .catch((err) => {
      $loginBtn.classList.toggle("is-loading", false);
      $pwdField.classList.toggle("is-danger", true);
      $loginError.classList.toggle("hidden", false);

      err.response
        .json()
        .then((cleanErr) => {
          $loginError.innerHTML = cleanErr[0];
        })
        .catch(() => {
          $loginError.innerHTML =
            "Error desconocido al intentar acceder. Por favor, inténtalo de nuevo.";
        });
    });
}

function register(e) {
  e.preventDefault();

  // Mark button as loading
  const $regButton = document.getElementById("register-button");
  $regButton.classList.toggle("is-loading", true);

  // Clean up old errors
  document
    .querySelectorAll("#register-form .is-danger")
    .forEach(($ele) => $ele.classList.toggle("is-danger"));
  document.querySelectorAll("#register-form [data-reg-error]").forEach(($ele) => $ele.remove());

  // Login the user via ajax
  easyFetch("/register", {
    method: "post",
    rawBody: {
      username: document.getElementById("reg_username").value,
      password: document.getElementById("reg_password").value,
      password_confirmation: document.getElementById("reg_password_confirmation").value,
      email: document.getElementById("reg_email").value,
      terms: document.getElementById("reg_terms").checked,
    },
  })
    .then(function () {
      window.location.reload(true);
    })
    .catch((err) => {
      $regButton.classList.toggle("is-loading", false);

      err.response
        .json()
        .then((cleanErrList) => {
          cleanErrList.forEach((curErr) => {
            const field = Object.keys(curErr)[0];
            const $field = document.getElementById("reg_" + field);
            $field.classList.toggle("is-danger", true);

            const $error = document.createElement("p");
            $error.classList.add("help", "is-danger");
            $error.dataset["regError"] = "";
            $error.innerHTML = curErr[field];
            $field.parentNode.parentNode.appendChild($error);
          });
        })
        .catch(() => {
          alert(
            "Error desconocido al intentar completar el registro. Por favor, inténtalo de nuevo."
          );
        });
    });
}

function showLoginForm() {
  showOverlayFromTpl("tpl-login");

  const $loginForm = document.getElementById("login-form");
  $loginForm.addEventListener("submit", doLogin);
  setTimeout(() => document.getElementById("username").focus(), 500);
}

{'fraction_non_alphanumeric': 0.1524608193566126, 'fraction_numerical': 0.002337091009073412, 'mean_word_length': 2.984118291347207, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '123039', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2085, 'n_words': 424}