import { Component, OnInit, Input, ViewChild, ComponentFactoryResolver } from "@angular/core"; import { Router } from "@angular/router"; import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service"; import { Instrument } from "@app/models/instrument"; import { InvestmentProposalWorkflowService } from "@app/services/investment-proposal-workflow.service"; import { InstrumentDirective } from "../instrument/instrument.directive"; import { InstrumentComponent } from "../instrument/instrument.component"; import { InvestmentProposalForm } from "@app/models/investment-proposal-form"; @Component({ selector: "mt-wizard-work", templateUrl: "./work.component.html" /*template: `

TEST

`*/ }) export class WorkComponent implements OnInit { title = "Formulario propuesta de inversión"; workType: string; form: any; @Input() ads: Instrument[]; currentAdIndex = -1; @ViewChild(InstrumentDirective, { static: true }) adHost: InstrumentDirective; interval: any; @Input() formData: InvestmentProposalForm; indexDynamicComponent: number; constructor( private router: Router, private formDataService: FormInvestmentProposalService, private componentFactoryResolver: ComponentFactoryResolver, private instrumentService: InvestmentProposalWorkflowService ) {} ngOnInit() { this.formData = this.formDataService.getFormData(); console.log("form data:"); console.log(this.formData); this.ads = this.instrumentService.getInstruments(); this.formData = this.formDataService.getFormData(); this.formData.instrumentos; console.log("instrumentos: "); console.log(this.formData.instrumentos); this.indexDynamicComponent = this.ads.findIndex( x => x.component.name == this.formData.instrumentos ); console.log(this.indexDynamicComponent); if (this.indexDynamicComponent >= 0) { this.loadComponent(); } else { console.log("No existe el componente"); } //this.workType = this.formDataService.getWork(); } loadComponent() { this.currentAdIndex = this.indexDynamicComponent % this.ads.length; const adItem = this.ads[this.currentAdIndex]; const componentFactory = this.componentFactoryResolver.resolveComponentFactory( adItem.component ); console.log(adItem.component); const viewContainerRef = this.adHost.viewContainerRef; viewContainerRef.clear(); const componentRef = viewContainerRef.createComponent(componentFactory); (componentRef.instance).data = adItem.data; } save(form: any): boolean { if (!form.valid) { return false; } this.formDataService.setWork(this.workType); return true; } goToPrevious(form: any) { if (this.save(form)) { // Navigate to the personal page this.router.navigate(["/investment-proposals/general-info"]); } } goToNext(form: any) { if (this.save(form)) { // Navigate to the address page this.router.navigate(["/address"]); } } }