|
| 1 | +import { REGEXP } from "../../utils/constants.js"; |
| 2 | +import { createUser } from "../signup/signUpModel.js"; |
| 3 | + |
| 4 | +export const signUpController = (form) => { |
| 5 | + |
| 6 | + form.addEventListener("submit", (event) => { |
| 7 | + |
| 8 | + /* By default, when a form is submitted the browser reloads the page and |
| 9 | + all JavaScript and current app state information is lost. */ |
| 10 | + event.preventDefault(); |
| 11 | + |
| 12 | + const nameElement = form.querySelector('#name'); |
| 13 | + const name = nameElement.value; |
| 14 | + |
| 15 | + const emailElement = form.querySelector('#email'); |
| 16 | + const email = emailElement.value; |
| 17 | + |
| 18 | + const passwordElement = form.querySelector('#password'); |
| 19 | + const password = passwordElement.value; |
| 20 | + |
| 21 | + const passwordConfirmElement = form.querySelector('#password-confirm'); |
| 22 | + const passwordConfirm = passwordConfirmElement.value; |
| 23 | + |
| 24 | + const errors = [] |
| 25 | + |
| 26 | + // validate email format |
| 27 | + const emailRegExp = REGEXP.mail; |
| 28 | + |
| 29 | + /* ".test(email)" is a method of regular expressions in JavaScript. |
| 30 | + It returns "true" if the email matches the pattern and "false" if it does not. */ |
| 31 | + |
| 32 | + /* Regular expressions (or regex) are patterns used to search, validate or replace text. */ |
| 33 | + |
| 34 | + /* ".test()" is a JavaScript method used with regular expressions to check if a text conforms to a pattern. */ |
| 35 | + if (!emailRegExp.test(email)) { |
| 36 | + errors.push('The email format is incorrect.') |
| 37 | + } |
| 38 | + |
| 39 | + // check that the passwords are the same |
| 40 | + if (password !== passwordConfirm) { |
| 41 | + errors.push('Passwords are not the same.') |
| 42 | + } |
| 43 | + |
| 44 | + if (errors.length === 0) { |
| 45 | + handleCreateUser(name, email, password, form) |
| 46 | + } else { |
| 47 | + errors.forEach(error => { |
| 48 | + const event = new CustomEvent("register-error", { |
| 49 | + detail: error |
| 50 | + }); |
| 51 | + form.dispatchEvent(event) |
| 52 | + }) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + const handleCreateUser = async (name, email, password, form) => { |
| 57 | + try { |
| 58 | + |
| 59 | + /* Insert User to API REST VVVVVVVVVVVVVVVVVVV */ |
| 60 | + await createUser(name, email, password); |
| 61 | + const event = new CustomEvent("register-ok", { |
| 62 | + detail: { |
| 63 | + message: 'You have successfully registered.', |
| 64 | + type: 'success' |
| 65 | + } |
| 66 | + }); |
| 67 | + /* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */ |
| 68 | + |
| 69 | + form.dispatchEvent(event) |
| 70 | + setTimeout(() => { |
| 71 | + window.location = '/' |
| 72 | + }, 5000); |
| 73 | + |
| 74 | + } catch (error) { |
| 75 | + |
| 76 | + const event = new CustomEvent("register-error", { |
| 77 | + detail: error.message |
| 78 | + }); |
| 79 | + |
| 80 | + // This event is sent to "signup" as it is being listened to. |
| 81 | + form.dispatchEvent(event) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments