SHARE
TWEET

Untitled

a guest May 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Users } from './../models/users';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Validators, FormBuilder} from '@angular/forms';
  4. import { UsersService } from '../entities/users.service';
  5. import { SnackbarService } from '../entities/snackbar.service';
  6.  
  7. @Component({
  8.   selector: 'app-create',
  9.   templateUrl: './create.component.html',
  10.   styleUrls: ['./create.component.css']
  11. })
  12. export class CreateComponent implements OnInit {
  13.  
  14.   formData : FormData;
  15.   base64textString:string;
  16.  
  17.   constructor(private formBuilder:FormBuilder, private usersService:UsersService, private snackbar:SnackbarService) { }
  18.  
  19.   ngOnInit() {
  20.   }
  21.  
  22.   userForm = this.formBuilder.group({
  23.     email : ['', [Validators.required, Validators.email]],
  24.     password : ['', [Validators.required, Validators.pattern('')]],
  25.     fname : ['', [Validators.required, Validators.pattern('')]],
  26.     lname : ['', [Validators.required, Validators.pattern('')]],
  27.     age : ['', [Validators.required, Validators.pattern('')]],
  28.   });
  29.  
  30.   get email() {
  31.     return this.userForm.get('email');
  32.   }
  33.   get password() {
  34.     return this.userForm.get('password');
  35.   }
  36.   get fname() {
  37.     return this.userForm.get('fname');
  38.   }
  39.   get lname() {
  40.     return this.userForm.get('lname');
  41.   }
  42.   get age() {
  43.     return this.userForm.get('age');
  44.   }
  45.   getEmailErrorMessage() {
  46.     return this.userForm.get('email').hasError('required') ? 'You must enter a value' :
  47.       this.userForm.get('email').hasError('email') ? 'Not a valid email' : '';
  48.   }
  49.  
  50.   getPasswordErrorMessage() {
  51.     return this.userForm.get('password').hasError('required') ? 'You must enter a value' :
  52.       this.userForm.get('password').hasError('password') ? 'Not a valid password' : '';
  53.   }
  54.  
  55.   getFnameErrorMessage() {
  56.     return this.userForm.get('fname').hasError('required') ? 'You must enter a value' :
  57.       this.userForm.get('fname').hasError('fname') ? 'Not a valid first name' : '';
  58.   }
  59.  
  60.   getLnameErrorMessage() {
  61.     return this.userForm.get('lname').hasError('required') ? 'You must enter a value' :
  62.       this.userForm.get('lname').hasError('lname') ? 'Not a valid last name' :'';
  63.   }
  64.  
  65.   getAgeErrorMessage() {
  66.     return this.userForm.get('age').hasError('required') ? 'You must enter a value' :
  67.       this.userForm.get('age').hasError('age') ? 'Not a valid age' :
  68.             '';
  69.   }
  70.  
  71.   openUpload() {
  72.     var el = document.getElementById('fileInputID');
  73.     el.click();
  74.   }
  75.  
  76.   fileChange(event) {
  77.     let fileList: FileList = event.target.files;
  78.     if (fileList.length > 0) {
  79.       let file: File = fileList[0];
  80.       this.formData = new FormData();
  81.       this.formData.append('uploadFile', file, file.name);
  82.      
  83.       var reader = new FileReader();
  84.       reader.readAsDataURL(event.target.files[0]);
  85.       let that = this;
  86.       reader.onload = function () {
  87.         that.base64textString = reader.result.toString();
  88.         var solution= that.base64textString.split("base64,");
  89.         that.base64textString = solution[1].toString();
  90.       };
  91.      
  92.     }
  93.   }
  94.  
  95.   createUser() {
  96.     if (this.userForm.invalid) {
  97.       this.snackbar.openSnackBar('Invalid form!');
  98.       return;
  99.     }
  100.     let user = new Users();
  101.     user.email = this.email.value;
  102.     user.password = this.password.value;
  103.     user.fname = this.fname.value;
  104.     user.lname = this.lname.value;
  105.     user.age = this.age.value;
  106.     user.avatar = this.base64textString;
  107.  
  108.     this.usersService.post(user).then(response => {
  109.       this.snackbar.openSnackBar(response);
  110.     }).catch(error => {
  111.         this.snackbar.openSnackBar("Status code : " + error.status);
  112.     });
  113.   }
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top