import {Component, OnInit, ViewEncapsulation} from '@angular/core'; import {FormGroup} from '@angular/forms'; import {FieldType, FormlyFieldConfig} from '@ngx-formly/core'; import {AllService} from '../../../all.service'; @Component({ selector: 'jhi-step-organisme', templateUrl: './step-organisme.component.html', styleUrls: ['./step-organisme.component.scss'], encapsulation: ViewEncapsulation.None }) export class StepOrganismeComponent extends FieldType implements OnInit { listBanksFromApi = []; resultSearchOrganisme = []; bankNameSelected = ''; codePostal = ''; bankSelected = null; searchError = ''; showCustomInformations = false; fieldRequired = true; submited = false; form = new FormGroup({}); fields: FormlyFieldConfig[] = [ { className: 'auto-fill-width', fieldGroup: [{ key: 'name', type: 'input', templateOptions: { label: 'Nom / raison sociale de l\'organisme prêteur', }, expressionProperties: { 'templateOptions.required': () => { return this.fieldRequired; } } }, { key: 'address', type: 'input', templateOptions: { label: 'Adresse de l\'organisme prêteur', }, expressionProperties: { 'templateOptions.required': () => { return this.fieldRequired; } } }, ] }, { className: 'auto-fill-width', fieldGroup: [{ key: 'zipCode', type: 'input', templateOptions: { label: 'Code postal de l\'organisme prêteur', }, validators: { zipcode: { expression: (c) => !c.value || /^(?:[0-8]\d|9[0-8])\d{3}$/.test(c.value), message: (error, field) => `"${field.formControl.value}" n'est pas un code postal valide (Exemple: 75000)` } }, expressionProperties: { 'templateOptions.required': () => { return this.fieldRequired; } } }, { key: 'city', type: 'input', templateOptions: { label: 'Ville de l\'organisme prêteur', }, expressionProperties: { 'templateOptions.required': () => { return this.fieldRequired; } } }, ] } ]; constructor(public allService: AllService) { super(); } ngOnInit(): void { this.allService.getSubmitedStepAsObserVable().subscribe((submited) => { this.submited = submited; }); this.allService.organismeService.listBanks().toPromise().then(response => { if (response.body) { this.listBanksFromApi = response.body; } }).catch((error) => { console.error(error); }); this.allService.getStepsSubjectAsObserVable().subscribe((data) => { if (data && data.step && data.step.fields && data.step.fields[0].fieldGroup[0].type === 'stepOrganisme') { if (!this.hasEmptyValue()) { this.resultSearchOrganisme = []; this.resultSearchOrganisme.push({ name: this.model['lender_name'], address: this.model['lender_address'], city: this.model['lender_city'] }); } else { this.allService.currentStepCustomError = true; } } }); } private hasEmptyValue() { return [ this.model['lender_name'], this.model['lender_address'], this.model['lender_zipcode'], this.model['lender_city'] ].some((value) => { return value === '' || value === null; }); } checkErrorCustomForm() { if (!this.bankNameSelected || !this.codePostal) { this.allService.currentStepCustomError = true; } } serchOrganism() { const data = this.createFromForm(); this.fieldRequired = false; setTimeout(() => { this.showCustomInformations = false; }, 500); this.bankSelected = null; this.allService.currentStepCustomError = true; this.model['lender_id'] = ''; this.model['lender_name'] = ''; this.model['lender_address'] = ''; this.model['lender_zipcode'] = ''; this.model['lender_city'] = ''; this.model['lender_mail'] = ''; this.model['lender_phone'] = ''; this.searchError = ''; this.allService.organismeService.searchBank(data).toPromise().then((response) => { if (response && response.body.banks) { this.resultSearchOrganisme = response.body.banks; //this.resultSearchOrganisme = JSON.parse('{"banks":[{"id":40657,"name":"BARCLAYS ROUEN","address":"15 R JEANNE D\'ARC","zipCode":"76000","city":"ROUEN","refMncap":"99943945"},{"id":40698,"name":"BARCLAYS ROUEN JUSTICE","address":"72 R JEANNE D ARC","zipCode":"76000","city":"ROUEN","refMncap":"99943986"}]}').banks; } }, (response) => { this.resultSearchOrganisme = []; //this.resultSearchOrganisme = JSON.parse('{"banks":[{"id":40657,"name":"BARCLAYS ROUEN","address":"15 R JEANNE D\'ARC","zipCode":"76000","city":"ROUEN","refMncap":"99943945"},{"id":40698,"name":"BARCLAYS ROUEN JUSTICE","address":"72 R JEANNE D ARC","zipCode":"76000","city":"ROUEN","refMncap":"99943986"}]}').banks; this.searchError = response.error.error; }); } private createFromForm(): any { return { 'zipCode': this.codePostal, 'name': this.bankNameSelected }; } setDataToModel(event, bank) { this.fieldRequired = false; if (this.bankSelected == bank.id) { event.preventDefault(); this.model['lender_id'] = bank.id; this.model['lender_name'] = bank.name; this.model['lender_address'] = bank.address; this.model['lender_zipcode'] = bank.zipCode; this.model['lender_city'] = bank.city; this.model['lender_mail'] = ''; this.model['lender_phone'] = ''; this.allService.currentStepCustomError = false; if (this.bankSelected === null) { this.resultSearchOrganisme = []; this.resultSearchOrganisme.push({ name: this.model['lender_name'], address: this.model['lender_address'], city: this.model['lender_city'] }); this.showCustomInformations = false; } if (this.showCustomInformations && this.bankSelected) { this.showAndSetCustomInformations(); } } else { this.allService.currentStepCustomError = true; this.model['lender_id'] = ''; this.model['lender_name'] = ''; this.model['lender_address'] = ''; this.model['lender_zipcode'] = ''; this.model['lender_city'] = ''; this.model['lender_mail'] = ''; this.model['lender_phone'] = ''; } } showAndSetCustomInformations(): void { this.fieldRequired = true; this.showCustomInformations = true; this.bankSelected = null; this.allService.currentStepCustomError = true; this.resultSearchOrganisme = []; setTimeout(() => { this.form.controls['name'].setValue(this.model['lender_name']); this.form.controls['address'].setValue(this.model['lender_address']); this.form.controls['zipCode'].setValue(this.model['lender_zipcode']); this.form.controls['city'].setValue(this.model['lender_city']); }, 100); } }