aboutsummaryrefslogtreecommitdiff
path: root/src/app/identity-page/identity-page.component.ts
blob: 296fe4cb380ee72061fcb56aba80f40e88726179 (plain) (blame)
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
import { Component, OnInit } from '@angular/core';
import { Pipe } from "@angular/core";
import { ApiService } from '../api.service';
import { IdentityAPI } from '../identity-api';

@Component({
  selector: 'app-identity-page',
  templateUrl: './identity-page.component.html'
})
export class IdentityPageComponent implements OnInit {

  identities: IdentityAPI[];
  filteredItems: IdentityAPI[];
  rename: boolean = false;
  changeIdentity: IdentityAPI;
  json: any;

  constructor(private apiService: ApiService) { }

  getAPIs(): void {
    this.apiService.getIdentities().subscribe(data => {
      this.identities = data;
      this.assignCopy();
    });
  }

  ngOnInit() {
    this.getAPIs();
  }

  assignCopy(){
    this.filteredItems = Object.assign([], this.identities);
  }

  filterItem(value : string){
    this.onRenameCancel();
    if(!value) this.assignCopy(); //when nothing has typed
    this.filteredItems = Object.assign([], this.identities).filter(
    item => {
      return ((item.name.indexOf(value) > -1) ||
      (item.id.indexOf(value) > -1) ||
      (item.type.indexOf(value) > -1 ))
    });
  }

  onClickRename(identity: IdentityAPI){
    this.rename = true;
    this.changeIdentity = Object.assign({},identity);
  }

  onRename(identity: IdentityAPI){
    this.rename = false;
    this.filteredItems = [];
    this.json = {'newname':identity.name};
    this.apiService.changeIdentity(identity.id,this.json).subscribe(data => {
      this.getAPIs();
    });
  }

  onRenameCancel(){
    this.rename = false;
  }

  onClickDelete(id: string){
    this.onRenameCancel();
    this.filteredItems = [];
    this.apiService.deleteIdentity(id).subscribe(data => {
      this.getAPIs();
    });
  }

}