Advertisement
Guest User

employee-details.ts

a guest
May 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. import { Component, Input, Output, OnChanges, SimpleChange, EventEmitter } from '@angular/core';
  2. import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
  3. import { ToastrService } from 'ngx-toastr';
  4. import { FormControl, FormsModule } from "@angular/forms";
  5.  
  6. import { EmployeeInterface } from 'src/app/shared/models/interfaces/employee';
  7.  
  8. import { EmployeeModel } from 'src/app/shared/models/employee.model';
  9. import { ApiService } from 'src/app/shared/services/api.service';
  10. import { StorageService } from 'src/app/shared/services/storage.service';
  11.  
  12. @Component({
  13. selector: 'app-employee-details',
  14. templateUrl: './employee-details.component.html',
  15. styleUrls: ['./employee-details.component.scss']
  16. })
  17. export class EmployeeDetailsComponent implements OnChanges {
  18. @Input()
  19. employeeId: string;
  20.  
  21. @Input()
  22. insertEmployee: boolean;
  23.  
  24. @Input()
  25. getReports: boolean;
  26.  
  27. @Output()
  28. onSaveSuccess: EventEmitter<boolean> = new EventEmitter();
  29.  
  30. usernameForm = new FormControl();
  31. passwordForm = new FormControl();
  32. firstNameForm = new FormControl();
  33. lastNameForm = new FormControl();
  34. typeForm = new FormControl();
  35. addressForm = new FormControl();
  36. ssnForm = new FormControl();
  37. salaryForm = new FormControl();
  38. emailForm = new FormControl();
  39. hireDateForm = new FormControl();
  40.  
  41. /** employee data */
  42. private employeeData: EmployeeInterface;
  43. /** Copy of the initial employee data */
  44. private _employeeData: EmployeeInterface;
  45. /** Flag for letting know the user that save is in progress */
  46. private isSaving: boolean;
  47. /** Flag for letting know the user that save is in progress */
  48. private isRemoving: boolean;
  49.  
  50. private isInserting: boolean;
  51.  
  52. constructor(public employeeModel: EmployeeModel, private apiService: ApiService, private toastr: ToastrService, private storageService: StorageService) { }
  53.  
  54. /**
  55. * This is an Angular method
  56. * (one of the lifcecycle component hooks: https://angular.io/guide/lifecycle-hooks)
  57. * Will detect changes on the @Input data, e.g when user select a diferent employee from the list
  58. */
  59. ngOnChanges(changes: { employeeId: SimpleChange, insertEmployee: SimpleChange }): void {
  60. const { employeeId, insertEmployee } = changes;
  61.  
  62. if (!employeeId || !employeeId.currentValue) this.clearComponentData();
  63.  
  64. if (employeeId && employeeId.currentValue && employeeId.currentValue !== employeeId.previousValue) {
  65. /** If employeeId changes on selection, get the selected employee details */
  66. this.clearForm();
  67. this.getEmployeeDetailsData(employeeId.currentValue);
  68. }
  69. if (insertEmployee && insertEmployee.currentValue !== insertEmployee.previousValue) {
  70. console.log("insert employee changed value");
  71. }
  72. }
  73.  
  74. /**
  75. * Requesting employee details data through the API Service
  76. * @param employeeId {string}
  77. */
  78. getEmployeeDetailsData(employeeId: string): void {
  79. this.apiService.getEmployeeDetails(employeeId).subscribe(
  80. (data: EmployeeInterface) => {
  81. /** Saving the obtained employee data into a variable */
  82. this.employeeData = data;
  83. /** Making copy of the initial employee data (for comparing purpose only) */
  84. this._employeeData = Object.assign({}, this.employeeData);
  85. },
  86. (error: HttpErrorResponse) => console.error(error)
  87. );
  88. }
  89.  
  90. /**
  91. * Will submit the employee data if has been changed
  92. * This method is it bind to the `Save employee` button
  93. */
  94. onEmployeeSave(): void {
  95. /** Initialize the isSaving flag */
  96. this.isSaving = true;
  97. /** Trigger the saving method from the API Service passing the employeeData*/
  98. this.apiService.saveEmployeeDetails(this.employeeData).subscribe(
  99. /** On Success */
  100. (data: EmployeeInterface) => {
  101. /** Update the copy of the initial employee data */
  102. this._employeeData = data;
  103. /** Notify the parent component to refresf the employee list */
  104. this.onSaveSuccess.emit(true);
  105. /** Notify the user with a successful message */
  106. this.toastr.success('Employee details updated!');
  107. },
  108. /** On Error */
  109. (error: HttpErrorResponse) => {
  110. /** Notify the user about the error */
  111. this.toastr.error(error.message);
  112. /** End the isSaving flag */
  113. this.isSaving = false;
  114. },
  115. /** End the isSaving flag */
  116. () => (this.isSaving = false)
  117. );
  118. }
  119.  
  120. onEmployeeInsert(): void {
  121. console.log("INSERT EMPLOYEE FUNCTION");
  122. if (this.fieldsNotNull()) {
  123. this.isInserting = true;
  124. this.employeeData = {
  125. username: this.usernameForm.value,
  126. password: this.passwordForm.value,
  127. firstname: this.firstNameForm.value,
  128. lastname: this.lastNameForm.value,
  129. employeeType: this.typeForm.value,
  130. address: this.addressForm.value,
  131. ssn: this.ssnForm.value,
  132. salary: this.salaryForm.value,
  133. email: this.emailForm.value,
  134. employeeId: this.ssnForm.value,
  135. hire_date: this.hireDateForm.value
  136. };
  137. this.apiService.insertEmployee(this.employeeData).subscribe(
  138. (data: EmployeeInterface) => {
  139. this._employeeData = data;
  140. this.onSaveSuccess.emit(true);
  141. this.toastr.success("Employee added!");
  142. this.clearForm();
  143. this.insertEmployee = false;
  144. },
  145.  
  146. (error: HttpErrorResponse) => {
  147. this.toastr.error(error.message);
  148. this.employeeData = undefined;
  149. this.isInserting = false;
  150. },
  151. () => (this.isInserting = false)
  152. );
  153. } else {
  154. this.isInserting = false;
  155. this.toastr.error("Some fields were empty.");
  156. }
  157. }
  158.  
  159. fieldsNull(): boolean {
  160. return this.usernameForm.value == null && this.passwordForm.value == null && this.addressForm.value == null && this.emailForm.value == null &&
  161. this.typeForm.value == null && this.firstNameForm.value == null && this.lastNameForm.value == null && this.passwordForm == null &&
  162. this.salaryForm.value == null && this.ssnForm.value == null;
  163. }
  164. fieldsNotNull(): boolean {
  165. return this.usernameForm.value != null && this.passwordForm.value != null && this.addressForm.value != null && this.emailForm.value != null &&
  166. this.typeForm.value != null && this.firstNameForm.value != null && this.lastNameForm.value != null && this.passwordForm != null &&
  167. this.salaryForm.value != null && this.ssnForm.value != null;
  168. }
  169. clearForm(): void {
  170. this.usernameForm.setValue(null);
  171. this.passwordForm.setValue(null);
  172. this.firstNameForm.setValue(null);
  173. this.lastNameForm.setValue(null);
  174. this.typeForm.setValue(null);
  175. this.addressForm.setValue(null);
  176. this.ssnForm.setValue(null);
  177. this.salaryForm.setValue(null);
  178. this.emailForm.setValue(null);
  179. this.ssnForm.setValue(null);
  180. this.hireDateForm.setValue(null);
  181. }
  182. /**
  183. * Will remove the employee data through the API service
  184. * This method is it bind to the `Delete` button
  185. */
  186. onEmployeeRemove(): void {
  187. /** Initialize the isRemoving flag */
  188. this.isRemoving = true;
  189. /** Trigger the saving method from the API Service passing the employeeId*/
  190. this.apiService.deleteEmployee(this.employeeData.employeeId).subscribe(
  191. () => {
  192. /** End the isRemoving flag */
  193. this.isRemoving = false;
  194. /** Clear the component data */
  195. this.clearComponentData();
  196. /** Notify the parent component to refresf the employee list */
  197. this.onSaveSuccess.emit(true);
  198. /** Notify the user with a successful message */
  199. this.toastr.success('Employee was removed!');
  200. },
  201. (error: HttpErrorResponse) => {
  202. /** Notify the user about the error */
  203. this.toastr.error(error.message);
  204. /** End the isRemoving flag */
  205. this.isRemoving = false;
  206. }
  207. );
  208. }
  209.  
  210. /**
  211. * Will determinate if the data has been changed or not
  212. * Buttons will remaing disabled if not change has happened
  213. */
  214. hasEmployeeDataChanged(): boolean {
  215. return JSON.stringify(this.employeeData) !== JSON.stringify(this._employeeData);
  216. }
  217.  
  218. /** This method will clear the `employeeData` value and `_employeeData` copy value */
  219. clearComponentData(): void {
  220. this.employeeData = undefined;
  221. this._employeeData = undefined;
  222. }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement