app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function showRegisterModal() {
  2. document.getElementById('registerModal').classList.remove('hidden');
  3. }
  4. function hideRegisterModal() {
  5. document.getElementById('registerModal').classList.add('hidden');
  6. }
  7. // Formateo automático del RUT mientras se escribe
  8. document.getElementById('rutInput').addEventListener('input', function(e) {
  9. let rut = e.target.value.replace(/[^0-9kK]/g, '');
  10. if (rut.length > 1) {
  11. let body = rut.slice(0, -1);
  12. let dv = rut.slice(-1);
  13. if (body.length > 0) {
  14. // Agregar puntos cada 3 dígitos desde la derecha
  15. body = body.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
  16. rut = body + '-' + dv;
  17. }
  18. }
  19. e.target.value = rut;
  20. });
  21. // Manejo del envío del formulario
  22. document.getElementById('registerForm').addEventListener('submit', function(e) {
  23. e.preventDefault();
  24. showGlobalLoader();
  25. const formData = new FormData(e.target);
  26. const data = {
  27. name: formData.get('name'),
  28. email: formData.get('email'),
  29. rut: formData.get('rut')
  30. };
  31. console.log('Datos del formulario:', data);
  32. fetch('/api/users/register', {
  33. method: 'POST',
  34. headers: {
  35. 'Content-Type': 'application/json'
  36. },
  37. body: JSON.stringify(data)
  38. })
  39. .then(response => {
  40. if (!response.ok) {
  41. return response.json().then(errorData => {
  42. throw new Error(errorData.message || 'Error al registrar el usuario.');
  43. });
  44. }
  45. return response.json();
  46. })
  47. .then(data => {
  48. console.log('Registro exitoso:', data);
  49. alert('Registro exitoso! Revisa tu correo electrónico para el PIN.');
  50. hideRegisterModal();
  51. }).finally(() => {
  52. hideGlobalLoader();
  53. })
  54. });
  55. function createGlobalLoader() {
  56. if (document.getElementById('globalLoader')) return;
  57. globalLoaderElement = document.createElement('div');
  58. globalLoaderElement.id = 'globalLoader';
  59. globalLoaderElement.className = 'fixed inset-0 bg-black bg-opacity-80 flex flex-col items-center justify-center z-[2000] transition-opacity duration-300 ease-in-out pointer-events-none';
  60. globalLoaderElement.style.opacity = '0';
  61. globalLoaderElement.innerHTML = `
  62. <div style="border: 6px solid rgba(255, 255, 255, 0.2); border-radius: 50%; border-top: 6px solidrgb(172, 85, 85); width: 60px; height: 60px; animation: spin 1s linear infinite;"></div>
  63. <p class="text-white text-xl mt-4">Procesando su registro...</p>
  64. `;
  65. document.body.appendChild(globalLoaderElement);
  66. }
  67. function showGlobalLoader() {
  68. if (!globalLoaderElement) createGlobalLoader();
  69. globalLoaderElement.style.display = 'flex';
  70. setTimeout(() => { if (globalLoaderElement) globalLoaderElement.style.opacity = '1'; }, 10);
  71. }
  72. function hideGlobalLoader() {
  73. if (globalLoaderElement) {
  74. globalLoaderElement.style.opacity = '0';
  75. setTimeout(() => { if (globalLoaderElement) globalLoaderElement.style.display = 'none'; }, 300);
  76. }
  77. }
  78. document.addEventListener('DOMContentLoaded', function() {
  79. createGlobalLoader();
  80. hideGlobalLoader();
  81. showRegisterModal();
  82. });