Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, ElementRef, OnInit, ViewChild } from "@angular/core";
- import { Sentry } from "nativescript-sentry";
- import { Router } from "@angular/router";
- import { Color } from "color";
- import { Page } from "ui/page";
- import { TextField } from "ui/text-field";
- import { View } from "ui/core/view";
- import { User } from "../../shared/user/user";
- import { UserService } from "../../shared/user/user.service";
- import { setHintColor } from "../../utils/hint-util";
- @Component({
- selector: "my-app",
- moduleId: module.id,
- providers: [UserService],
- templateUrl: "./login.html",
- styleUrls: ["./login-common.css", "./login.css"],
- })
- export class LoginComponent implements OnInit {
- user: User;
- isLoggingIn = true;
- @ViewChild("container") container: ElementRef;
- @ViewChild("email") email: ElementRef;
- @ViewChild("password") password: ElementRef;
- constructor(private router: Router, private userService: UserService, private page: Page) {
- this.user = new User();
- }
- ngOnInit() {
- this.page.actionBarHidden = true;
- this.page.backgroundImage = this.page.ios ? "res://bg_login.jpg" : "res://bg_login";
- }
- submit() {
- if (!this.user.isValidEmail()) {
- Sentry.captureMessage("Enter a valid email address.", {});
- alert("Enter a valid email address.");
- return;
- }
- if (this.isLoggingIn) {
- this.login();
- } else {
- this.signUp();
- }
- }
- login() {
- this.userService.login(this.user)
- .subscribe(
- () => this.router.navigate(["/list"]),
- (error) => alert("Unfortunately we could not find your account.")
- );
- }
- signUp() {
- this.userService.register(this.user)
- .subscribe(
- () => {
- alert("Your account was successfully created.");
- this.toggleDisplay();
- },
- () => alert("Unfortunately we were unable to create your account.")
- );
- }
- toggleDisplay() {
- this.isLoggingIn = !this.isLoggingIn;
- this.setTextFieldColors();
- let container = <View>this.container.nativeElement;
- container.animate({
- backgroundColor: this.isLoggingIn ? new Color("white") : new Color("#301217"),
- duration: 200
- });
- }
- setTextFieldColors() {
- let emailTextField = <TextField>this.email.nativeElement;
- let passwordTextField = <TextField>this.password.nativeElement;
- let mainTextColor = new Color(this.isLoggingIn ? "black" : "#C4AFB4");
- emailTextField.color = mainTextColor;
- passwordTextField.color = mainTextColor;
- let hintColor = new Color(this.isLoggingIn ? "#ACA6A7" : "#C4AFB4");
- setHintColor({ view: emailTextField, color: hintColor });
- setHintColor({ view: passwordTextField, color: hintColor });
- }
- }
Add Comment
Please, Sign In to add comment