summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole3/frontend/src/app/pages/menu/menu.component.ts
blob: 2d803c866e63a4e69d3c1b1f040c08fab19b2f4c (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
101
102
103
104
105
import {Component, OnInit} from '@angular/core';

import { MenuService } from 'src/app/api/menu.service';
import { Menu } from 'src/app/model/menu';
import {PartitionInfo} from '../../model/image';
import {Ng2TableActionComponent} from '../common/table-action/ng2-table-action.component';
import {TranslateService} from '@ngx-translate/core';
import {Router} from '@angular/router';
import {OgSweetAlertService} from '../../service/og-sweet-alert.service';
import {ToasterService} from '../../service/toaster.service';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: [ './menu.component.scss' ]
})
export class MenuComponent implements OnInit {
  public menus: Menu[];
  private tableSettings: any;
  // this tells the tabs component which Pages
  // should be each tab's root Page
  constructor(public menuService: MenuService, private router: Router, private ogSweetAlert: OgSweetAlertService, private toaster: ToasterService, private translate: TranslateService) {
  }

  ngOnInit(): void {
    this.menuService.list().subscribe(
        data => {
          this.menus = data;
        },
        error => {

        }
    );
    const self = this;
    this.tableSettings = {
      columns: {
        title: {
          title: this.translate.instant('title')
        },
        description: {
          title: this.translate.instant('description')
        },
        comments: {
          title: this.translate.instant('comments'),
        },
        resolution: {
          title: this.translate.instant('resolution')
        },
        options: {
          title: 'Options',
          filter: false,
          sort: false,
          type: 'custom',
          renderComponent: Ng2TableActionComponent,
          onComponentInitFunction(instance) {
            instance.edit.subscribe(row => {
              self.router.navigate(['/app/menus/edit/', row.id]);
            });
            instance.delete.subscribe(row => {
              self.deleteMenu(row);
            });
          }
        },
      },
      actions: {
        position: 'right',
        add: false,
        edit: false,
        delete: false
      }
    };
  }

  deleteMenu(menu) {
    const self = this;
    this.ogSweetAlert.swal({
      title: this.translate.instant('sure_to_delete') + '?',
      message: this.translate.instant('action_cannot_be_undone'),
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3c8dbc',
      confirmButtonText: this.translate.instant('yes_delete'),
      closeOnConfirm: true
    }).then(
        function(result) {
          if (result.value === true) {

            self.menuService.delete(menu.id).subscribe(
                (response) => {
                  self.toaster.pop({type: 'success', title: 'success', body: self.translate.instant('successfully_deleted')});
                  // Buscar el elemento en el array y borrarlo
                  const index = self.menus.indexOf(menu);
                  if (index !== -1) {
                    self.menus.splice(menu, 1);
                  }
                },
                (error) => {
                  self.toaster.pop({type: 'error', title: 'error', body: error});
                }
            );
          }
        });
  }

}