import { Component, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { NgForm, FormsModule, FormGroup, FormControl, Validators } from '@angular/forms';
import { ApartmentService } from '../apartments/apartment.service';
import { Apartment } from '../apartments/apartment.model';
@Component({
selector: 'app-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.css']
})
export class SignupComponent implements OnInit, AfterViewInit {
@ViewChild('terms') terms: ElementRef ;
signupForm: FormGroup;
constructor(private apartmentService: ApartmentService) { }
date: Date = new Date();
// Used to set maxdate to current date
maxDate = (new Date().getFullYear()).toString()+"-0"+(new Date().getMonth()+1).toString()+"-"+(new Date().getDate()).toString();
ngOnInit() {
this.signupForm = new FormGroup({
'firstName': new FormControl(null, Validators.required),
'lastName': new FormControl(null, Validators.required),
'email': new FormControl(null, [Validators.required, Validators.email]),
'passwordFields': new FormGroup({
'password': new FormControl(null, Validators.required),
'verifyPassword': new FormControl(null, [Validators.required, this.passwordMatchFail.bind(this)]),
}),
'birthdate': new FormControl(null),
'flatPurchaceDate': new FormControl(null, Validators.required),
'profilePicture': new FormControl(null),
'flatDetails': new FormGroup({
'flatBlock': new FormControl(null, Validators.required),
'flatNumber': new FormControl(null, Validators.required)
}),
'mobileNumber': new FormControl(null, Validators.required),
'terms': new FormControl(null)
});
}
// terms validation
ngAfterViewInit() {
var termsAccepted = this.terms.nativeElement.checked;
console.log(termsAccepted);
}
onSubmit(form: NgForm) {
console.log(form)
const firstName: string = form.value.firstName;
const lastName: string = form.value.lastName;
const flatBlock: string = form.value.flatDetails.flatBlock;
const flatNumber: string = form.value.flatDetails.flatNumber;
const email: string = form.value.email;
var newApartment = {
fBlock: flatBlock,
fNumber: flatNumber,
oName: firstName
}
this.apartmentService.addApartment(newApartment)
console.log("Apt ADd")
console.log(newApartment);
}
passwordMatchFail(control: FormControl): {[s: string]: boolean} {
if(this.signupForm.get('passwordFields.password').value !== control.value) {
return {'passwordMatchFail': true};
}
return null;
}
}