Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { RequestsService } from '../requests.service';
  4. import { UserModel } from '../../models/user';
  5. import { SocialInformationModel } from '../../models/socialInformation'
  6. import { and } from '@angular/router/src/utils/collection';
  7.  
  8. @Component({
  9.   selector: 'app-register-form',
  10.   templateUrl: './register-form.component.html',
  11.   styleUrls: ['./register-form.component.css']
  12. })
  13. export class RegisterFormComponent implements OnInit {
  14.  
  15.   valuePassword = '';
  16.   valueInvalidPassword = '';
  17.   valueUsername = '';
  18.   valueEmail = '';
  19.   password = '';
  20.   confirmPassword = '';
  21.   username = '';
  22.   statusPassword = false;
  23.   statusValidPassword = false;
  24.   statusUsername = false;
  25.   statusEmail = false;
  26.   danger = '#d9534f';
  27.   sucess = '#5cb85c';
  28.  
  29.   constructor(private router:Router,
  30.               private requester:RequestsService) { }
  31.  
  32.   ngOnInit() {
  33.   }
  34.  
  35.   registerUser(e) {
  36.     e.preventDefault();
  37.     var user : UserModel;
  38.     var social_information: SocialInformationModel;
  39.  
  40.     user = {
  41.       username: e.target.elements[0].value,
  42.       first_name: e.target.elements[1].value,
  43.       last_name: e.target.elements[2].value,
  44.       password: e.target.elements[3].value,
  45.       email: e.target.elements[5].value,
  46.      };  
  47.     // TODO - adicionar validação de criação. Checar http status code = 201.
  48.     // AINDA é TODO /\
  49.     this.requester.postUser(user).subscribe(response => {
  50.       let statusUser = response.status;
  51.       var userId = response.body["id"];
  52.       console.log("STATUS CODE RETURNED ON USER: " + statusUser);
  53.  
  54.       if (this.requester.didSucceed(statusUser)) {
  55.         social_information = {
  56.           owner: userId,
  57.           state: e.target.elements[7].value,
  58.           city: e.target.elements[8].value,
  59.           income: e.target.elements[9].value,
  60.           education: e.target.elements[10].value,
  61.           job: e.target.elements[11].value,
  62.           birth_date: e.target.elements[12].value
  63.         };
  64.    
  65.         this.requester.postSocialInformation(social_information).subscribe(response => {
  66.           let statusSI = response.status;
  67.           console.log("STATUS CODE RETURNED ON SOCIAL_INFORMATION: " + statusSI);
  68.    
  69.           if (this.requester.didSucceed(statusSI)) {
  70.             this.router.navigate(['main-page']);  
  71.           };
  72.    
  73.         });
  74.       };
  75.     });
  76.  
  77.   }
  78.  
  79.   onKeyPassword(e: any) {
  80.     this.password = e.target.value;
  81.     if(!this.isValidPassword(this.password)){
  82.       this.valueInvalidPassword = 'Sua senha deve ter no mínimo 8 caracteres, dos quais: Um é numérico; Um é letra maiúscula; Um é caractere especial.';
  83.       document.getElementById('alert-invalid-password').style.display = "block";
  84.       this.statusValidPassword = false;
  85.       this.borderColor('password', this.danger);
  86.     } else {
  87.       document.getElementById('alert-invalid-password').style.display = "none";
  88.       this.statusValidPassword = true;
  89.       this.borderColor('password', this.sucess);
  90.     }
  91.   }
  92.  
  93.   onKeyConfirmPassword(e: any) {
  94.     this.confirmPassword = e.target.value;
  95.   }
  96.  
  97.   onKeyUsername(e: any) {
  98.     var username = e.target.value;
  99.     if(this.isUsernameValid(username)){
  100.       document.getElementById('alert-username').style.display = "none";
  101.       this.valueUsername = '';
  102.       this.statusUsername = true;
  103.       this.borderColor('username', this.sucess);
  104.     } else {
  105.       this.valueUsername = 'TEST';
  106.       document.getElementById('alert-username').style.display = "block";
  107.       this.statusUsername = false;
  108.       this.borderColor('username', this.danger);
  109.     } if (!this.isUsernameSizeValid(username)) {
  110.       this.valueUsername = 'Nome de usuário deve ter entre 4 e 20 caracteres'
  111.       document.getElementById('alert-username').style.display = "block";
  112.       this.statusUsername = false;
  113.       this.borderColor('username', this.danger);
  114.     }
  115.   }
  116.  
  117.   onKeyEmail(e: any) {
  118.     var email = e.target.value;
  119.     if(this.isEmailValid(email)){
  120.       document.getElementById('alert-email').style.display = "none";
  121.       this.valueEmail = '';
  122.       this.statusEmail = true;
  123.       this.borderColor('email', this.sucess);
  124.     } else {
  125.       document.getElementById('alert-email').style.display = "block";
  126.       this.valueEmail = 'Formato do E-mail está incorreto';
  127.       this.statusEmail = false;
  128.       this.borderColor('email', this.danger);
  129.     } if(email.length < 4) {
  130.       document.getElementById('alert-email').style.display = "block";
  131.       this.valueEmail = 'Formato do E-mail está incorreto';
  132.       this.statusEmail = false;
  133.       this.borderColor('email', this.danger);
  134.     }
  135.   }
  136.  
  137.   onKeyValidatorPassword(e: any) {
  138.     if(this.isConfirmedPassword(this.confirmPassword, this.password)){
  139.       document.getElementById('alert-password').style.display = "none";
  140.       this.valuePassword = '';
  141.       this.statusPassword = true;
  142.       this.borderColor('confirm-password', this.sucess);
  143.     } else {
  144.       this.valuePassword = 'A confirmação de senha não corresponde';
  145.       document.getElementById('alert-password').style.display = "block";
  146.       this.statusPassword = false;
  147.       this.borderColor('confirm-password', this.danger);
  148.     }
  149.   }
  150.  
  151.   isUsernameValid(username) {
  152.     var format = /^[a-zA-Z0-9]+$/;
  153.     if(format.test(username)){
  154.       return true;
  155.     }
  156.     return false;
  157.   }
  158.  
  159.   isUsernameSizeValid(username) {
  160.     if (username.length > 3 && username.length < 21) {
  161.       return true;
  162.     }
  163.     return false;
  164.   }
  165.  
  166.   isEmailValid(email) {
  167.     let EMAILRGX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  168.     if(EMAILRGX.test(email)) {
  169.       return true;
  170.     }
  171.     else {
  172.       return false;
  173.     }
  174.   }
  175.  
  176.   isConfirmedPassword(password, confPassword) {
  177.     if(password === confPassword){
  178.       return true;
  179.     }
  180.     return false;
  181.   }
  182.  
  183.   isValidPassword(password) {
  184.     let PWDRGX = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%¨&*()'`{}~^:;<>,./?°ªº¹²³⁴⁵⁶⁷⁸⁹⁰£¢¬])[A-Za-z\d$@$!%*?&]{8,}/;
  185.     if (PWDRGX.test(password)) {
  186.       return true;
  187.     }
  188.     return false;
  189.   }
  190.  
  191.   clickFirstButton() {
  192.        if(this.statusPassword && this.statusUsername && this.statusEmail && this.statusValidPassword){
  193.             document.getElementById("firstPart").style.display = "none";
  194.             document.getElementById("secondPart").style.display = "block";
  195.             document.querySelector('#registerBtn').removeAttribute('disabled');
  196.        }
  197.        else {
  198.          if(!this.statusPassword) {
  199.             this.borderColor('password', this.danger);
  200.             this.borderColor('confirm-password', this.danger);
  201.          } else {
  202.           this.borderColor('password', this.sucess);
  203.           this.borderColor('confirm-password', this.sucess);
  204.          }
  205.  
  206.          if(!this.statusUsername){
  207.           this.borderColor('username', this.danger);
  208.          } else {
  209.           this.borderColor('username', this.sucess);
  210.          }
  211.  
  212.          if(!this.statusEmail){
  213.           this.borderColor('email', this.danger);
  214.          } else {
  215.            this.borderColor('email', this.sucess);
  216.          }
  217.        }
  218.  }
  219.  
  220.  borderColor(id, color){
  221.     document.getElementById(id).style.borderColor = color;
  222.  }
  223.  
  224.  clickReturnButton() {
  225.       document.getElementById("firstPart").style.display = "block";
  226.       document.getElementById("secondPart").style.display = "none";
  227.       document.querySelector('#registerBtn').setAttribute('disabled', 'disabled');
  228. }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement