summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole3/frontend/src/app/pages/software-profile/software-profile.component.ts
blob: b34cfb7733227f27686742186736c61e748c9e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {Component, OnInit} from '@angular/core';

import { SoftwareProfileService } from 'src/app/api/software-profile.service';
import {ParamMap, ActivatedRoute, Router} from '@angular/router';
import {SoftwareProfile} from '../../model/software-profile';
import {ToasterService} from '../../service/toaster.service';
import {SoftwareComponentService} from '../../api/software-component.service';
import {SoftwareComponent} from '../../model/software-component';
import {SoftwareProfileFormType} from '../../form-type/software-profile.form-type';
import {OgCommonService} from '../../service/og-common.service';

@Component({
  selector: 'app-software-profile',
  templateUrl: './software-profile.component.html',
  styleUrls: [ './software-profile.component.scss' ]
})
export class SoftwareProfileComponent implements OnInit {
  public softwareProfile: SoftwareProfile = new SoftwareProfile();
  public softwareComponents: SoftwareComponent[];
  formType: any;
  // this tells the tabs component which Pages
  // should be each tab's root Page
  constructor(public ogCommonService: OgCommonService, public softwareProfileService: SoftwareProfileService,
              public softwareComponentsService: SoftwareComponentService, private routerCtrl: Router,
              private router: ActivatedRoute, private toaster: ToasterService) {
    this.formType = new SoftwareProfileFormType().getForm();
  }

  ngOnInit(): void {
    this.softwareComponentsService.list().subscribe(
        components => {
          this.softwareComponents = components;
          this.router.paramMap.subscribe(
              (params: ParamMap) => {
                if(params.get('id')) {
                  this.softwareProfileService.read(Number(params.get('id'))).subscribe(
                      data => {
                        this.softwareProfile = data;
                        const self = this;
                        // Seleccionar los componentes adecuados
                        this.softwareComponents.forEach(function(component, index, array) {
                          if ( self.softwareProfile.softwares.find((value) => value.id === component.id)) {
                            // @ts-ignore
                            component.$$selected = true;
                          }
                        });
                      },
                      error => {
                        this.toaster.pop({type: 'error', body: 'error_loading_software_profile', title: 'error'});
                      }
                  );
                } else {
                  this.softwareProfile = new SoftwareProfile();
                }
              }
          );
        },
        error => {
          this.toaster.pop({type: 'error', body: 'error_loading_software_components', title: 'error'});
        }
    );

  }

  save() {
    this.softwareProfile.softwares = [];
    const self = this;
    this.softwareComponents.forEach(function(component, index, array) {
      // @ts-ignore
      if (component.$$selected === true) {
        // @ts-ignore
        self.softwareProfile.softwares.push(component.id);
      }
    });
    // Actualizar o guardar
    let request;
    if(this.softwareProfile.id !== 0){
      request =  this.softwareProfileService.update(this.softwareProfile);
    }
    else{
      request =  this.softwareProfileService.create(this.softwareProfile);
    }
    request.subscribe(
        (response) => {
          this.toaster.pop({type: 'success', title: 'success', body: 'successfully_saved'});
          this.routerCtrl.navigate(['/app/software']).then(
              success => {
                console.log(success);
              },
              error => {
                console.log(error);
              }
          );
        },
        (error) => {
          this.toaster.pop({type: 'error', title: 'error', body: error});
        }
    );
  }
}