This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
FoodLoop-Web/src/app/pages/register.component.ts

122 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Component } from '@angular/core';
2017-05-09 13:39:48 +01:00
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
import { ValidationManager } from "ng2-validation-manager";
2017-04-27 22:50:11 +01:00
import { Http, Response } from '@angular/http';
2017-05-09 13:39:48 +01:00
import { ApiService } from '../providers/api-service';
2017-05-10 16:26:57 +01:00
import {Router } from '@angular/router';
2017-05-09 13:39:48 +01:00
import 'rxjs/add/operator/map';
@Component({
2017-05-09 13:39:48 +01:00
templateUrl: 'register.component.html',
providers: [ApiService]
})
export class RegisterComponent {
signupForm: ValidationManager;
customerForm: ValidationManager;
organisationForm: ValidationManager;
2017-05-09 13:39:48 +01:00
ageRanges: Object[];
constructor(
private http: Http,
private formBuilder: FormBuilder,
2017-05-10 16:26:57 +01:00
private router: Router,
private api: ApiService,
) {
this.api.getAgeRanges()
.subscribe(
result => {
console.log(result);
this.ageRanges = result.ages;
}
);
this.signupForm = new ValidationManager({
token: 'required',
usertype: 'required',
email: 'required',
password: 'required',
confirmpassword: 'required|equalTo:password'
});
this.customerForm = new ValidationManager({
display_name: 'required',
full_name: 'required',
postcode: 'required',
age_range: 'required',
});
this.organisationForm = new ValidationManager({
name: 'required',
street_name: 'required',
town: 'required',
postcode: 'required',
});
2017-05-09 13:39:48 +01:00
}
onSubmitCustomer() {
console.log(this.signupForm.isValid());
if (!this.signupForm.isValid() && !this.customerForm.isValid()) {
console.log("Not Valid!");
return;
}
let signupForm = this.signupForm.getForm().value;
let customerForm = this.customerForm.getForm().value;
let data = {
token: signupForm.token,
usertype: signupForm.usertype,
email: signupForm.email,
password: signupForm.password,
display_name: customerForm.display_name,
full_name: customerForm.full_name,
postcode: customerForm.postcode,
age_range: customerForm.age_range,
};
console.log(data);
2017-06-05 18:47:34 +01:00
this.api
.register(data)
.subscribe(
result => {
console.log('registered!');
this.router.navigate(['/dashboard']);
},
error => {
console.log( error._body );
}
2017-06-05 18:47:34 +01:00
);
}
onSubmitOrganisation() {
console.log(this.signupForm.isValid());
if (!this.signupForm.isValid() || !this.organisationForm.isValid()) {
console.log("Not Valid!");
return;
}
let signupForm = this.signupForm.getForm().value;
let organisationForm = this.organisationForm.getForm().value;
2017-05-10 13:50:54 +01:00
let data = {
token: signupForm.token,
usertype: signupForm.usertype,
email: signupForm.email,
password: signupForm.password,
name: organisationForm.name,
street_name: organisationForm.street_name,
town: organisationForm.town,
postcode: organisationForm.postcode,
};
console.log(data);
2017-06-05 18:47:34 +01:00
this.api
.register(data)
2017-05-10 13:50:54 +01:00
.subscribe(
result => {
2017-05-10 16:26:57 +01:00
console.log('registered!');
this.router.navigate(['/dashboard']);
2017-05-10 13:50:54 +01:00
},
error => {
console.log( error._body );
}
2017-06-05 18:47:34 +01:00
);
2017-04-27 22:50:11 +01:00
}
2017-05-09 13:39:48 +01:00
}