login.component.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Location } from '@angular/common';
  2. import { Router, ActivatedRoute } from '@angular/router';
  3. import { AuthServiceService } from './../shared/auth-service.service';
  4. import { Component, OnInit, ViewChild } from '@angular/core';
  5. import { NgForm } from '@angular/forms';
  6. import { Response } from '@angular/http';
  7. import { ToastrService } from 'ngx-toastr';
  8. import { NgxSpinnerService } from 'ngx-spinner';
  9. @Component({
  10. selector: 'app-login',
  11. templateUrl: './login.component.html',
  12. styleUrls: ['./login.component.css']
  13. })
  14. export class LoginComponent implements OnInit {
  15. constructor(private authSer: AuthServiceService,
  16. private router: Router,
  17. private route: ActivatedRoute,
  18. private location: Location,
  19. private spinner: NgxSpinnerService,
  20. private toastr: ToastrService) { }
  21. @ViewChild('f') loginFormData: NgForm;
  22. myInnerHeight = window.innerHeight;
  23. ngOnInit() {
  24. // this.authSer.showSearchHeader = false;
  25. this.authSer.notificationLogin = false;
  26. // this.authSer.showHeaderLogin = true;
  27. this.authSer.showHeaderDashBoard = false;
  28. this.authSer.showDashboardHeader = true;
  29. this.authSer.arabicTemplate = true;
  30. this.spinner.hide();
  31. }
  32. //login function from services
  33. onSubmitedForm(){
  34. console.log(this.loginFormData.value);
  35. const dataForm = this.loginFormData.value;
  36. this.authSer.login(dataForm).subscribe(
  37. (responce: Response) => {
  38. localStorage.setItem('token', responce['token']);
  39. localStorage.setItem('userType', responce['user'].type);
  40. const id = responce['user'].id;
  41. console.log(id);
  42. this.authSer.setDataUser(responce);
  43. this.loginFormData.reset();
  44. this.router.navigate(['/services/' + id]);
  45. // this.location.back();
  46. },
  47. (error) => {
  48. console.log(error);
  49. this.toastr.error( 'حدث خطأ' ,' تأكد من رقم الهويه او كلمه المرور ');
  50. }
  51. );
  52. }
  53. //pattern="^[1-9]+[0-9]*$" => (number only)
  54. }