VICE / Client / src / app / dev-work-request / dev-work-request.component.ts
dev-work-request.component.ts
Raw
import { Component, OnInit } from '@angular/core';
import { map } from 'rxjs/operators';
import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
import { ServerDataService } from '../server-data.service';
import { HttpClient, HttpClientModule, HttpHeaders } from '@angular/common/http';
import { RoleService, Roles } from '../role.service';
import { AuthenticationService } from '../authentication.service';
import { IssueService } from "../issue.service";
import { environment } from './../../environments/environment';


@Component({
  selector: 'app-dev-work-request',
  templateUrl: './dev-work-request.component.html',
  styleUrls: ['./dev-work-request.component.css']
})
export class DevWorkRequestComponent implements OnInit {

  public returnData: any;
  public vmList : any;
  public systemList : any;
  public fwList : any;
  public priority : number = 0;
  public success : boolean = false;
  currentRole: any;


  currentSystem: string = "";
  currentSystemData: any;
  sysNum: number = 0;

  public auth: boolean = true;
  public role: Roles = Roles.NA;
  public roles = Roles;

  cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
    map(({ matches }) => {
      if (matches) {
        return [

          { title: 'Make a Work Request', cols: 2, rows: 3 }

        ];
      }

      return [

        { title: 'Make a Work Request', cols: 2, rows: 2 }

      ];
    })
  );

  constructor( private breakpointObserver: BreakpointObserver,
               private serverDataService: ServerDataService,
               private roleService: RoleService,
               private http: HttpClient,
               private authenticationService: AuthenticationService) {
      this.printVMList();
      this.currentRole = this.roleService.roleSrc;
  }

  printVMList() {
    console.log(this.vmList);
  }

  setSystem( system: string, num: number ) {
    this.sysNum = num;
    this.currentSystem = system;
  }

  ngOnInit(){
    this.serverDataService.vmList.subscribe((data: any) => {
      this.vmList = data;
      console.log(this.vmList);
    });

    this.serverDataService.fwList.subscribe((data: any) => {
      this.fwList = data;
      console.log(this.fwList);
    });

    this.roleService.currentRole.subscribe( (result: Roles) => {
      this.role = result;
    });

    this.serverDataService.systemList.subscribe((data: any) => {
      this.systemList = data;
      this.setSystem(data[this.sysNum].id, this.sysNum);
      if(this.currentSystemData === "") {
        this.currentSystem = this.systemList[this.sysNum].id;
      }
    });

    this.roleService.currentRole.subscribe((data: any) => {
      this.currentRole = data;
      console.log("Current role:\t" + this.roleService.getRoleString(this.currentRole));
    });
  }

  async sendData(product:String, system:String, desc:String) {
    let auth = this.authenticationService.authHeader.get('Authorization') || "NA NA";
    //setting headers for request
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
        Authorization: auth
      })
    };
    //setting payload
    let payload = {id: product, system: system, desc: desc};
    //http post request add work order
    this.http.post(environment.ip + '/addWork', payload, httpOptions)
      .subscribe(data => {
        this.returnData = data;
        console.log(this.returnData);
        this.serverDataService.getDataFromServer();
        this.success = true;
        return this.returnData;
      });
  }
}