Advertisement
Hen_B_S

EmployeeComponent Angular-TS

Jan 24th, 2023 (edited)
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 3.63 KB | Source Code | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { Employee } from './employee';
  3. import { EmployeeService } from './employee.service';
  4. import { HttpErrorResponse } from '@angular/common/http';
  5. import { NgForm } from '@angular/forms';
  6.  
  7. @Component({
  8.   selector: 'app-root',
  9.   templateUrl: './app.component.html',
  10.   styleUrls: ['./app.component.css']
  11. })
  12.  
  13. export class AppComponent implements OnInit {
  14.   public employees!: Employee[];
  15.   public editEmployee!: Employee;
  16.   public deleteEmployee!: Employee;
  17.  
  18.   constructor(private employeeService: EmployeeService){}
  19.  
  20.   ngOnInit() {
  21.     this.getEmployees();
  22.   }
  23.  
  24.   public getEmployees(): void {
  25.     this.employeeService.getEmployees().subscribe(
  26.       (response: Employee[]) => {
  27.         this.employees = response;
  28.         console.log(this.employees);
  29.       },
  30.       (error: HttpErrorResponse) => {
  31.         alert(error.message);
  32.       }
  33.     );
  34.   }
  35.  
  36.   public onAddEmployee(addForm: NgForm): void {
  37.     document.getElementById('add-employee-form')?.click();
  38.     this.employeeService.addEmployee(addForm.value).subscribe(
  39.       (response: Employee) => {
  40.         console.log(response);
  41.         this.getEmployees();
  42.         addForm.reset();
  43.       },
  44.       (error: HttpErrorResponse) => {
  45.         alert(error.message);
  46.         addForm.reset();
  47.       }
  48.     );
  49.   }
  50.  
  51.   public onUpdateEmployee(employee: Employee): void {
  52.     this.employeeService.updateEmployee(employee).subscribe(
  53.       (response: Employee) => {
  54.         console.log(response);
  55.         this.getEmployees();
  56.       },
  57.       (error: HttpErrorResponse) => {
  58.         alert(error.message);
  59.       }
  60.     );
  61.   }
  62.  
  63.   public onDeleteEmployee(employeeId: number): void {
  64.     this.employeeService.deleteEmployee(employeeId).subscribe(
  65.       (response: void) => {
  66.         console.log(response);
  67.         this.getEmployees();
  68.       },
  69.       (error: HttpErrorResponse) => {
  70.         alert(error.message);
  71.       }
  72.     );
  73.   }
  74.  
  75.   public searchEmployees(key: string): void {
  76.     console.log(key);
  77.     const results: Employee[] = [];
  78.     for (const employee of this.employees) {
  79.       if (employee.name.toLowerCase().indexOf(key.toLowerCase()) !== -1
  80.       || employee.email.toLowerCase().indexOf(key.toLowerCase()) !== -1
  81.       || employee.phone.toLowerCase().indexOf(key.toLowerCase()) !== -1
  82.       || employee.jobTitle.toLowerCase().indexOf(key.toLowerCase()) !== -1) {
  83.         results.push(employee);
  84.       }
  85.     }
  86.     this.employees = results;
  87.     if (results.length === 0 || !key) {
  88.       this.getEmployees();
  89.     }
  90.   }
  91.  
  92.   public onOpenModalAdd(employee: null, mode: string): void {
  93.     const container = document.getElementById('main-container');
  94.     const button = document.createElement('button');
  95.     button.type = 'button';
  96.     button.style.display = 'none';
  97.     button.setAttribute('data-toggle', 'modal');
  98.  
  99.     if (mode === 'add') {
  100.       button.setAttribute('data-target', '#addEmployeeModal');
  101.     }
  102.     container?.appendChild(button);
  103.     button.click();
  104.   }
  105.  
  106.   public onOpenModal(employee: Employee , mode: string): void {
  107.     const container = document.getElementById('main-container');
  108.     const button = document.createElement('button');
  109.     button.type = 'button';
  110.     button.style.display = 'none';
  111.     button.setAttribute('data-toggle', 'modal');
  112.  
  113.     if (mode === 'edit') {
  114.       this.editEmployee = employee
  115.       button.setAttribute('data-target', '#updateEmployeeModal');
  116.     }
  117.     if (mode === 'delete') {
  118.       this.deleteEmployee = employee
  119.       button.setAttribute('data-target', '#deleteEmployeeModal');
  120.     }
  121.     container?.appendChild(button);
  122.     button.click();
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement