Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2017
136
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, ActivatedRoute } from '@angular/router';
  3. import { AlertService, AuthenticationService, UsersService } from '../common/services/';
  4. import { User } from '../common/models/';
  5.  
  6. @Component({
  7.     selector: 'app-register',
  8.     templateUrl: './register.component.html',
  9.     styleUrls: ['./register.component.css']
  10. })
  11. export class RegisterComponent implements OnInit {
  12.     model: User;
  13.     imageFile: File;
  14.     loading = false;
  15.     returnUrl: string;
  16.  
  17.     constructor(
  18.         private route: ActivatedRoute,
  19.         private router: Router,
  20.         private authenticationService: AuthenticationService,
  21.         private alertService: AlertService,
  22.         private usersService: UsersService) { }
  23.  
  24.     ngOnInit() {
  25.         this.model = {
  26.             username: '',
  27.             password: '',
  28.             confirmPassword: '',
  29.             email: '',
  30.             firstName: '',
  31.             lastName: '',
  32.             imageUrl: ''
  33.         };
  34.  
  35.         // reset login status
  36.         this.authenticationService.logout();
  37.  
  38.         // get return url from route parameters or default to '/'
  39.         this.returnUrl = this.route.snapshot.params['returnUrl'] || '/';
  40.     }
  41.  
  42.     register() {
  43.         this.loading = true;
  44.  
  45.         //save file
  46.         if(this.imageFile) {
  47.             this.model.imageUrl = "path in server";
  48.         }
  49.  
  50.         this.usersService.create(this.model)
  51.             .subscribe(
  52.                 data => {
  53.                     this.router.navigate([this.returnUrl]);
  54.                 },
  55.                 error => {
  56.                     // E11000 duplicate key error collection: a2teamwork.users index: username_1 dup key: { : "Pesho" }
  57.                     let message = this.parseErrorMessage(JSON.parse(error._body).msg.message);
  58.                     this.alertService.error(message);
  59.                     this.loading = false;
  60.                 });
  61.     }
  62.  
  63.     onImageChange($event) {
  64.         this.imageFile = $event.srcElement.files;
  65.     }
  66.  
  67.     parseErrorMessage(message: string) {
  68.         let duplicateField = message.substring((message.indexOf('index: ') + 6), (message.indexOf('dup ') - 3));
  69.         return duplicateField + ' is already taken';
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement