simple-item-modal.component.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Component, Input } from '@angular/core';
  2. import { ModalService } from 'src/services/modal/modal.service';
  3. @Component({
  4. selector: 'app-simple-item-modal',
  5. templateUrl: './simple-item-modal.component.html',
  6. styleUrl: './simple-item-modal.component.scss',
  7. })
  8. export class SimpleItemModalComponent {
  9. public constructor(private modalAccessor: ModalService) {}
  10. @Input() public item: any;
  11. @Input() public isUpdate: boolean = false;
  12. @Input() public isFood: boolean = false;
  13. public name: string = '';
  14. public description: string = '';
  15. public quantity: number = 0;
  16. public weight: number = 0;
  17. public value: number = 0;
  18. public isReady: boolean = false;
  19. public ngOnInit(): void {
  20. console.log('ngOnInit() in simple-item-modal.component.ts');
  21. console.log('isUpdate: ', this.isUpdate);
  22. console.log('isFood: ', this.isFood);
  23. console.log('item: ', this.item);
  24. if (this.isUpdate) {
  25. this.loadItem();
  26. }
  27. }
  28. public cancel(): void {
  29. this.modalAccessor.handleModalClosing('cancel', undefined);
  30. this.resetItem();
  31. }
  32. public add(): void {
  33. this.modalAccessor.handleModalClosing('add', this.createItem());
  34. this.resetItem();
  35. }
  36. public update(): void {
  37. this.modalAccessor.handleModalClosing('update', this.createItem());
  38. this.resetItem();
  39. }
  40. private loadItem(): void {
  41. this.name = this.item.name;
  42. this.description = this.item.description;
  43. this.quantity = this.item.quantity;
  44. this.weight = this.item.weight;
  45. this.value = this.item.value;
  46. this.isReady = this.item.isReady;
  47. }
  48. private createItem(): any {
  49. if (this.isFood) {
  50. return {
  51. name: this.name,
  52. isReady: this.isReady,
  53. description: this.description,
  54. quantity: this.quantity,
  55. weight: this.weight,
  56. };
  57. } else {
  58. return {
  59. name: this.name,
  60. description: this.description,
  61. quantity: this.quantity,
  62. weight: this.weight,
  63. value: this.value,
  64. };
  65. }
  66. }
  67. private resetItem(): void {
  68. this.name = '';
  69. this.description = '';
  70. this.quantity = 0;
  71. this.weight = 0;
  72. this.value = 0;
  73. this.isReady = false;
  74. }
  75. public ngOnDestroy(): void {
  76. console.log('ngOnDestroy() in simple-item-modal.component.ts');
  77. }
  78. }