work.component.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. Component,
  3. OnInit,
  4. Input,
  5. ViewChild,
  6. ComponentFactoryResolver
  7. } from "@angular/core";
  8. import { Router } from "@angular/router";
  9. import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
  10. import { Instrument } from "@app/models/instrument";
  11. import { InvestmentProposalWorkflowService } from "@app/services/investment-proposal-workflow.service";
  12. import { InstrumentDirective } from "../instrument/instrument.directive";
  13. import { InstrumentComponent } from "../instrument/instrument.component";
  14. import { InvestmentProposalForm } from "@app/models/investment-proposal-form";
  15. @Component({
  16. selector: "mt-wizard-work",
  17. templateUrl: "./work.component.html"
  18. /*template: `
  19. <div>
  20. <h2>TEST</h2>
  21. </div>
  22. `*/
  23. })
  24. export class WorkComponent implements OnInit {
  25. title = "Formulario propuesta de inversión";
  26. workType: string;
  27. form: any;
  28. @Input() ads: Instrument[];
  29. currentAdIndex = -1;
  30. @ViewChild(InstrumentDirective, { static: true })
  31. adHost: InstrumentDirective;
  32. interval: any;
  33. @Input() formData: InvestmentProposalForm;
  34. indexDynamicComponent: number;
  35. constructor(
  36. private router: Router,
  37. private formDataService: FormInvestmentProposalService,
  38. private componentFactoryResolver: ComponentFactoryResolver,
  39. private instrumentService: InvestmentProposalWorkflowService
  40. ) {}
  41. ngOnInit() {
  42. this.formData = this.formDataService.getFormData();
  43. console.log("form data:");
  44. console.log(this.formData);
  45. this.ads = this.instrumentService.getInstruments();
  46. this.formData = this.formDataService.getFormData();
  47. this.formData.instrumentos;
  48. console.log("instrumentos: ");
  49. console.log(this.formData.instrumentos);
  50. this.indexDynamicComponent = this.ads.findIndex(
  51. x => x.component.name == this.formData.instrumentos
  52. );
  53. console.log(this.indexDynamicComponent);
  54. if (this.indexDynamicComponent >= 0) {
  55. this.loadComponent();
  56. } else {
  57. console.log("No existe el componente");
  58. }
  59. //this.workType = this.formDataService.getWork();
  60. }
  61. loadComponent() {
  62. this.currentAdIndex = this.indexDynamicComponent % this.ads.length;
  63. const adItem = this.ads[this.currentAdIndex];
  64. const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
  65. adItem.component
  66. );
  67. console.log(adItem.component);
  68. const viewContainerRef = this.adHost.viewContainerRef;
  69. viewContainerRef.clear();
  70. const componentRef = viewContainerRef.createComponent(componentFactory);
  71. (<InstrumentComponent>componentRef.instance).data = adItem.data;
  72. }
  73. save(form: any): boolean {
  74. if (!form.valid) {
  75. return false;
  76. }
  77. this.formDataService.setWork(this.workType);
  78. return true;
  79. }
  80. goToPrevious(form: any) {
  81. if (this.save(form)) {
  82. // Navigate to the personal page
  83. this.router.navigate(["/investment-proposals/general-info"]);
  84. }
  85. }
  86. goToNext(form: any) {
  87. if (this.save(form)) {
  88. // Navigate to the address page
  89. this.router.navigate(["/address"]);
  90. }
  91. }
  92. }