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