Guest User

Untitled

a guest
Oct 21st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import { Component, ElementRef, OnInit, ViewChild } from "@angular/core";
  2. import { Sentry } from "nativescript-sentry";
  3. import { Router } from "@angular/router";
  4. import { Color } from "color";
  5. import { Page } from "ui/page";
  6. import { TextField } from "ui/text-field";
  7. import { View } from "ui/core/view";
  8.  
  9. import { User } from "../../shared/user/user";
  10. import { UserService } from "../../shared/user/user.service";
  11. import { setHintColor } from "../../utils/hint-util";
  12.  
  13. @Component({
  14. selector: "my-app",
  15. moduleId: module.id,
  16. providers: [UserService],
  17. templateUrl: "./login.html",
  18. styleUrls: ["./login-common.css", "./login.css"],
  19. })
  20. export class LoginComponent implements OnInit {
  21. user: User;
  22. isLoggingIn = true;
  23.  
  24. @ViewChild("container") container: ElementRef;
  25. @ViewChild("email") email: ElementRef;
  26. @ViewChild("password") password: ElementRef;
  27.  
  28. constructor(private router: Router, private userService: UserService, private page: Page) {
  29. this.user = new User();
  30. }
  31.  
  32. ngOnInit() {
  33. this.page.actionBarHidden = true;
  34. this.page.backgroundImage = this.page.ios ? "res://bg_login.jpg" : "res://bg_login";
  35. }
  36.  
  37. submit() {
  38. if (!this.user.isValidEmail()) {
  39. Sentry.captureMessage("Enter a valid email address.", {});
  40. alert("Enter a valid email address.");
  41. return;
  42. }
  43.  
  44. if (this.isLoggingIn) {
  45. this.login();
  46. } else {
  47. this.signUp();
  48. }
  49. }
  50.  
  51. login() {
  52. this.userService.login(this.user)
  53. .subscribe(
  54. () => this.router.navigate(["/list"]),
  55. (error) => alert("Unfortunately we could not find your account.")
  56. );
  57. }
  58.  
  59. signUp() {
  60. this.userService.register(this.user)
  61. .subscribe(
  62. () => {
  63. alert("Your account was successfully created.");
  64. this.toggleDisplay();
  65. },
  66. () => alert("Unfortunately we were unable to create your account.")
  67. );
  68. }
  69.  
  70. toggleDisplay() {
  71. this.isLoggingIn = !this.isLoggingIn;
  72. this.setTextFieldColors();
  73. let container = <View>this.container.nativeElement;
  74. container.animate({
  75. backgroundColor: this.isLoggingIn ? new Color("white") : new Color("#301217"),
  76. duration: 200
  77. });
  78. }
  79.  
  80. setTextFieldColors() {
  81. let emailTextField = <TextField>this.email.nativeElement;
  82. let passwordTextField = <TextField>this.password.nativeElement;
  83.  
  84. let mainTextColor = new Color(this.isLoggingIn ? "black" : "#C4AFB4");
  85. emailTextField.color = mainTextColor;
  86. passwordTextField.color = mainTextColor;
  87.  
  88. let hintColor = new Color(this.isLoggingIn ? "#ACA6A7" : "#C4AFB4");
  89. setHintColor({ view: emailTextField, color: hintColor });
  90. setHintColor({ view: passwordTextField, color: hintColor });
  91. }
  92. }
Add Comment
Please, Sign In to add comment