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/dashboard/account-edit.component.ts

187 lines
5.6 KiB
TypeScript
Raw Normal View History

2017-08-03 11:33:09 +00:00
import { Component, OnInit, ViewChild } from '@angular/core';
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
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 {
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;
submitStatusError = 'Error received, please try again.';
constructor(
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
});
this.settingOrganisationForm = this.formBuilder.group({
name : ['', [Validators.required]],
street_name : ['', [Validators.required]],
town : ['', [Validators.required]],
sector : ['', [Validators.required]],
2017-08-24 15:20:55 +00:00
});
this.settingCustomerForm = this.formBuilder.group({
full_name : ['', [Validators.required]],
display_name : ['', [Validators.required]],
});
this.accountType = localStorage.getItem('usertype');
}
2017-08-24 15:20:55 +00:00
ngOnInit(): void {
this.api.accountFullLoad().subscribe(
result => {
console.log(result);
2017-08-03 11:33:09 +00:00
this.settingForm.patchValue({
email: result.email,
postcode: result.postcode,
password: '',
new_password: '',
});
this.settingOrganisationForm.patchValue({
name: result.name,
street_name: result.street_name,
town: result.town,
sector: result.sector,
});
this.settingCustomerForm.patchValue({
full_name: result.full_name,
display_name: result.display_name,
});
this.api.setUserInfo( result.email, result.display_name || result.name );
},
error => {
console.log( error._body );
}
);
}
2017-08-24 15:20:55 +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!');
this.submitStatus = 'validation_failed';
2017-08-25 11:18:14 +00:00
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 = {
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,
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)
.subscribe(
result => {
console.log('data submitted!');
this.submitStatus = 'success';
2017-08-25 11:18:14 +00:00
console.log(this.submitStatus);
},
error => {
2017-09-15 16:33:34 +00:00
console.log('Edit Error');
console.log(error);
try {
console.log(error.error);
const jsonError = error.json();
console.log('boop');
2017-09-15 16:33:34 +00:00
this.submitStatusError = '"' + jsonError.error + '" Error, ' + jsonError.message;
} catch (e) {
2017-09-15 16:33:34 +00:00
this.submitStatusError = 'There was a server error, please try again later.';
}
this.submitStatus = 'send_failed';
2017-08-25 11:18:14 +00:00
console.log(this.submitStatus);
}
);
}
2017-08-24 15:20:55 +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!');
this.submitStatus = 'validation_failed';
2017-08-25 11:18:14 +00:00
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 = {
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)
.subscribe(
result => {
console.log('data submitted!');
this.submitStatus = 'success';
2017-08-25 11:18:14 +00:00
console.log(this.submitStatus);
},
error => {
2017-09-15 16:33:34 +00:00
console.log('Edit Error');
console.log(error);
try {
console.log(error.error);
const jsonError = error.json();
console.log('boop');
2017-09-15 16:33:34 +00:00
this.submitStatusError = '"' + jsonError.error + '" Error, ' + jsonError.message;
} catch (e) {
2017-09-15 16:33:34 +00:00
this.submitStatusError = 'There was a server error, please try again later.';
}
this.submitStatus = 'send_failed';
2017-08-25 11:18:14 +00:00
console.log(this.submitStatus);
}
);
}
}