import { Component, Input } from '@angular/core'; import { ModalService } from 'src/services/modal/modal.service'; @Component({ selector: 'app-simple-item-modal', templateUrl: './simple-item-modal.component.html', styleUrl: './simple-item-modal.component.scss', }) export class SimpleItemModalComponent { public constructor(private modalAccessor: ModalService) {} @Input() public item: any; @Input() public isUpdate: boolean = false; @Input() public isFood: boolean = false; public name: string = ''; public description: string = ''; public quantity: number = 0; public weight: number = 0; public value: number = 0; public isReady: boolean = false; public ngOnInit(): void { console.log('ngOnInit() in simple-item-modal.component.ts'); console.log('isUpdate: ', this.isUpdate); console.log('isFood: ', this.isFood); console.log('item: ', this.item); if (this.isUpdate) { this.loadItem(); } } public cancel(): void { this.modalAccessor.handleModalClosing('cancel', undefined); this.resetItem(); } public add(): void { this.modalAccessor.handleModalClosing('add', this.createItem()); this.resetItem(); } public update(): void { this.modalAccessor.handleModalClosing('update', this.createItem()); this.resetItem(); } private loadItem(): void { this.name = this.item.name; this.description = this.item.description; this.quantity = this.item.quantity; this.weight = this.item.weight; this.value = this.item.value; this.isReady = this.item.isReady; } private createItem(): any { if (this.isFood) { return { name: this.name, isReady: this.isReady, description: this.description, quantity: this.quantity, weight: this.weight, }; } else { return { name: this.name, description: this.description, quantity: this.quantity, weight: this.weight, value: this.value, }; } } private resetItem(): void { this.name = ''; this.description = ''; this.quantity = 0; this.weight = 0; this.value = 0; this.isReady = false; } public ngOnDestroy(): void { console.log('ngOnDestroy() in simple-item-modal.component.ts'); } }