2017-08-03 11:33:09 +00:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
2017-07-28 18:30:35 +00:00
|
|
|
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
|
|
|
|
import { Http, Response } from '@angular/http';
|
|
|
|
import { ApiService } from '../providers/api-service';
|
|
|
|
import 'rxjs/add/operator/map';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: 'account-edit.component.html',
|
|
|
|
})
|
2017-08-24 15:20:55 +00:00
|
|
|
export class AccountEditComponent implements OnInit {
|
2017-07-28 18:30:35 +00:00
|
|
|
settingForm: FormGroup;
|
|
|
|
settingOrganisationForm: FormGroup;
|
|
|
|
settingCustomerForm: FormGroup;
|
|
|
|
accountType: any;
|
2017-09-04 15:30:56 +00:00
|
|
|
// @ViewChild('fileInput') fileInput;
|
2017-08-25 11:18:14 +00:00
|
|
|
submitStatus: any;
|
2017-09-15 16:33:34 +00:00
|
|
|
submitStatusError: string = 'Error received, please try again.';
|
2017-07-28 18:30:35 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private http: Http,
|
2017-08-24 15:20:55 +00:00
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private api: ApiService,
|
|
|
|
) {
|
|
|
|
this.settingForm = this.formBuilder.group({
|
2017-08-03 11:33:09 +00:00
|
|
|
email : ['', [Validators.required]],
|
|
|
|
postcode : ['', [Validators.required]],
|
|
|
|
password : ['', [Validators.required]],
|
|
|
|
new_password : [''],
|
2017-08-24 15:20:55 +00:00
|
|
|
});
|
2017-07-28 18:30:35 +00:00
|
|
|
this.settingOrganisationForm = this.formBuilder.group({
|
|
|
|
name : ['', [Validators.required]],
|
|
|
|
street_name : ['', [Validators.required]],
|
|
|
|
town : ['', [Validators.required]],
|
2017-09-04 16:26:01 +00:00
|
|
|
sector : ['', [Validators.required]],
|
2017-08-24 15:20:55 +00:00
|
|
|
});
|
2017-07-28 18:30:35 +00:00
|
|
|
this.settingCustomerForm = this.formBuilder.group({
|
|
|
|
full_name : ['', [Validators.required]],
|
|
|
|
display_name : ['', [Validators.required]],
|
|
|
|
});
|
2017-08-31 17:44:17 +00:00
|
|
|
this.accountType = localStorage.getItem('usertype');
|
2017-07-28 18:30:35 +00:00
|
|
|
}
|
2017-08-24 15:20:55 +00:00
|
|
|
|
2017-07-28 18:30:35 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
this.api.accountFullLoad().subscribe(
|
|
|
|
result => {
|
|
|
|
console.log(result);
|
2017-08-03 11:33:09 +00:00
|
|
|
this.settingForm.patchValue({
|
2017-07-28 18:30:35 +00:00
|
|
|
email: result.email,
|
|
|
|
postcode: result.postcode,
|
|
|
|
password: '',
|
|
|
|
new_password: '',
|
|
|
|
});
|
|
|
|
this.settingOrganisationForm.patchValue({
|
|
|
|
name: result.name,
|
|
|
|
street_name: result.street_name,
|
|
|
|
town: result.town,
|
2017-09-04 16:26:01 +00:00
|
|
|
sector: result.sector,
|
2017-07-28 18:30:35 +00:00
|
|
|
});
|
|
|
|
this.settingCustomerForm.patchValue({
|
|
|
|
full_name: result.full_name,
|
|
|
|
display_name: result.display_name,
|
|
|
|
});
|
2017-09-04 16:26:01 +00:00
|
|
|
this.api.setUserInfo( result.email, result.display_name || result.name );
|
2017-07-28 18:30:35 +00:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log( error._body );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-08-24 15:20:55 +00:00
|
|
|
|
2017-07-28 18:30:35 +00:00
|
|
|
onSubmitOrganisation() {
|
2017-08-24 15:20:55 +00:00
|
|
|
console.log(this.settingForm.valid);
|
|
|
|
if (!this.settingForm.valid && !this.settingOrganisationForm.valid) {
|
|
|
|
console.log('Not Valid!');
|
2017-08-25 11:18:14 +00:00
|
|
|
this.submitStatus = "validation_failed";
|
|
|
|
console.log(this.submitStatus);
|
2017-08-24 15:20:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settingForm = this.settingForm.value;
|
|
|
|
const settingOrganisationForm = this.settingOrganisationForm.value;
|
|
|
|
|
2017-08-03 11:33:09 +00:00
|
|
|
// image upload code
|
2017-09-04 15:30:56 +00:00
|
|
|
// const fi = this.fileInput.nativeElement;
|
|
|
|
// const data = new FormData();
|
2017-08-24 15:20:55 +00:00
|
|
|
|
2017-09-04 15:30:56 +00:00
|
|
|
// if (fi.files && fi.files[0]) {
|
|
|
|
// const fileToUpload = fi.files[0];
|
|
|
|
// data.append('file', fileToUpload);
|
|
|
|
// }
|
2017-08-03 11:33:09 +00:00
|
|
|
|
2017-08-24 15:20:55 +00:00
|
|
|
const submitData = {
|
2017-07-28 18:30:35 +00:00
|
|
|
email: settingForm.email,
|
|
|
|
postcode: settingForm.postcode,
|
|
|
|
password: settingForm.password,
|
|
|
|
new_password: settingForm.new_password,
|
|
|
|
name: settingOrganisationForm.name,
|
|
|
|
street_name: settingOrganisationForm.street_name,
|
|
|
|
town: settingOrganisationForm.town,
|
2017-09-04 16:26:01 +00:00
|
|
|
sector: settingOrganisationForm.sector,
|
2017-08-24 15:20:55 +00:00
|
|
|
};
|
|
|
|
|
2017-09-04 15:30:56 +00:00
|
|
|
// data.append('form', JSON.stringify(submitData));
|
2017-08-24 15:20:55 +00:00
|
|
|
|
2017-09-04 15:30:56 +00:00
|
|
|
console.log(submitData);
|
2017-08-24 15:20:55 +00:00
|
|
|
this.api
|
2017-09-04 15:30:56 +00:00
|
|
|
.accountEditUpdate(submitData)
|
2017-07-28 18:30:35 +00:00
|
|
|
.subscribe(
|
|
|
|
result => {
|
|
|
|
console.log('data submitted!');
|
2017-08-25 11:18:14 +00:00
|
|
|
this.submitStatus = "success";
|
|
|
|
console.log(this.submitStatus);
|
2017-07-28 18:30:35 +00:00
|
|
|
},
|
|
|
|
error => {
|
2017-09-15 16:33:34 +00:00
|
|
|
console.log('Edit Error');
|
|
|
|
console.log(error);
|
|
|
|
try {
|
|
|
|
console.log(error.error);
|
|
|
|
let jsonError = error.json();
|
|
|
|
console.log("boop");
|
|
|
|
this.submitStatusError = '"' + jsonError.error + '" Error, ' + jsonError.message;
|
|
|
|
} catch(e) {
|
|
|
|
this.submitStatusError = 'There was a server error, please try again later.';
|
|
|
|
}
|
2017-08-25 11:18:14 +00:00
|
|
|
this.submitStatus = "send_failed";
|
|
|
|
console.log(this.submitStatus);
|
2017-07-28 18:30:35 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-08-24 15:20:55 +00:00
|
|
|
|
2017-07-28 18:30:35 +00:00
|
|
|
onSubmitCustomer() {
|
2017-08-24 15:20:55 +00:00
|
|
|
console.log(this.settingForm.valid);
|
|
|
|
if (!this.settingForm.valid && !this.settingCustomerForm.valid) {
|
|
|
|
console.log('Not Valid!');
|
2017-08-25 11:18:14 +00:00
|
|
|
this.submitStatus = "validation_failed";
|
|
|
|
console.log(this.submitStatus);
|
2017-08-24 15:20:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settingForm = this.settingForm.value;
|
|
|
|
const settingCustomerForm = this.settingCustomerForm.value;
|
|
|
|
|
2017-08-03 11:33:09 +00:00
|
|
|
// image upload code
|
2017-09-04 15:30:56 +00:00
|
|
|
// const fi = this.fileInput.nativeElement;
|
2017-08-24 15:20:55 +00:00
|
|
|
const data = new FormData();
|
|
|
|
|
2017-09-04 15:30:56 +00:00
|
|
|
// if (fi.files && fi.files[0]) {
|
|
|
|
// const fileToUpload = fi.files[0];
|
|
|
|
// data.append('file', fileToUpload);
|
|
|
|
// }
|
2017-08-03 11:33:09 +00:00
|
|
|
|
2017-08-24 15:20:55 +00:00
|
|
|
const submitData = {
|
2017-07-28 18:30:35 +00:00
|
|
|
email: settingForm.email,
|
|
|
|
postcode: settingForm.postcode,
|
|
|
|
password: settingForm.password,
|
|
|
|
new_password: settingForm.new_password,
|
|
|
|
full_name: settingCustomerForm.full_name,
|
|
|
|
display_name: settingCustomerForm.display_name,
|
2017-08-24 15:20:55 +00:00
|
|
|
};
|
|
|
|
|
2017-09-04 15:30:56 +00:00
|
|
|
// data.append('form', JSON.stringify(submitData));
|
2017-08-24 15:20:55 +00:00
|
|
|
|
|
|
|
this.api
|
2017-09-04 15:30:56 +00:00
|
|
|
.accountEditUpdate(submitData)
|
2017-07-28 18:30:35 +00:00
|
|
|
.subscribe(
|
|
|
|
result => {
|
|
|
|
console.log('data submitted!');
|
2017-08-25 11:18:14 +00:00
|
|
|
this.submitStatus = "success";
|
|
|
|
console.log(this.submitStatus);
|
2017-07-28 18:30:35 +00:00
|
|
|
},
|
|
|
|
error => {
|
2017-09-15 16:33:34 +00:00
|
|
|
console.log('Edit Error');
|
|
|
|
console.log(error);
|
|
|
|
try {
|
|
|
|
console.log(error.error);
|
|
|
|
let jsonError = error.json();
|
|
|
|
console.log("boop");
|
|
|
|
this.submitStatusError = '"' + jsonError.error + '" Error, ' + jsonError.message;
|
|
|
|
} catch(e) {
|
|
|
|
this.submitStatusError = 'There was a server error, please try again later.';
|
|
|
|
}
|
2017-08-25 11:18:14 +00:00
|
|
|
this.submitStatus = "send_failed";
|
|
|
|
console.log(this.submitStatus);
|
2017-07-28 18:30:35 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|