Skip to content

Instantly share code, notes, and snippets.

@jsmuster
Last active December 28, 2018 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmuster/6555d509d07b226801637733f7fdf935 to your computer and use it in GitHub Desktop.
Save jsmuster/6555d509d07b226801637733f7fdf935 to your computer and use it in GitHub Desktop.
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { HttpClient } from "@angular/common/http";
import { Router } from "@angular/router";
@Component({
selector: 'input-user-data-form',
templateUrl: './input-user-data-form.component.html',
styleUrls: ['./input-user-data-form.component.css']
})
export class InputUserDataFormComponent implements OnInit {
registered = false;
submitted = false;
userForm: FormGroup;
guid: string;
serviceErrors:any = {};
constructor(private formBuilder: FormBuilder, private http: HttpClient, private router: Router)
{
this.http.get('/api/v1/generate_uid').subscribe((data:any) => {
this.guid = data.guid;
}, error => {
console.log("There was an error generating the proper GUID on the server", error);
});
}
invalidFirstName()
{
return (this.submitted && (this.serviceErrors.first_name != null || this.userForm.controls.first_name.errors != null));
}
invalidLastName()
{
return (this.submitted && (this.serviceErrors.last_name != null || this.userForm.controls.last_name.errors != null));
}
invalidEmail()
{
return (this.submitted && (this.serviceErrors.email != null || this.userForm.controls.email.errors != null));
}
invalidZipcode()
{
return (this.submitted && (this.serviceErrors.zipcode != null || this.userForm.controls.zipcode.errors != null));
}
invalidPassword()
{
return (this.submitted && (this.serviceErrors.password != null || this.userForm.controls.password.errors != null));
}
ngOnInit()
{
this.userForm = this.formBuilder.group({
first_name: ['', [Validators.required, Validators.maxLength(50)]],
last_name: ['', [Validators.required, Validators.maxLength(50)]],
email: ['', [Validators.required, Validators.email, Validators.maxLength(75)]],
zipcode: ['', [Validators.required, Validators.pattern('^[0-9]{5}(?:-[0-9]{4})?$')]],
password: ['', [Validators.required, Validators.minLength(5), Validators.pattern('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$')]],
});
}
onSubmit()
{
this.submitted = true;
if(this.userForm.invalid == true)
{
return;
}
else
{
let data: any = Object.assign({guid: this.guid}, this.userForm.value);
this.http.post('/api/v1/customer', data).subscribe((data:any) => {
let path = '/user/' + data.customer.uid;
this.router.navigate([path]);
}, error =>
{
this.serviceErrors = error.error.error;
});
this.registered = true;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment