import { Component, OnInit, EventEmitter, Input } from '@angular/core'; import { BsModalRef } from 'ngx-bootstrap'; import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms'; import { ToastrService } from 'ngx-toastr'; import { ItemCategoryService } from '../../../services/item.category.service'; import { FileUploadService } from '../../../services/fileupload.service'; import { NGXLogger } from 'ngx-logger'; @Component({ selector: 'edit-new-category-modal', templateUrl: './edit-category-modal.component.html', styleUrls: ['./edit-category-modal.component.css'] }) export class EditCategoryModalComponent implements OnInit { categoryForm: FormGroup; modalRef: BsModalRef; message: string; confirm: boolean; isCategoryExists: boolean; isValidDesktopImage = false; isValidDeviceImage = false; isValidAppImage = false; desktopImageFileContent: string; deviceImageFileContent: string; appImageFileContent: string; productCategories: any[]; @Input() title: string; public event: EventEmitter = new EventEmitter(); constructor(public bsModalRef: BsModalRef,private logger: NGXLogger, private toastr: ToastrService, private fb: FormBuilder, private itemCategoryService: ItemCategoryService, private fileUploadService: FileUploadService ) { // Binding categories this.itemCategoryService.getAllItem_Categories().subscribe( categories => { // tslint:disable-next-line: no-string-literal this.productCategories = categories['data'] as any[]; }); } ngOnInit(): void { this.categoryForm = this.fb.group({ desktopImage: [''], deviceImage: [''], appImage: [''], desktopImageFileContent: [''], deviceImageFileContent: [''], appImageFileContent: [''], product_category:['', Validators.required] }); } // Validate and get desktop categry image content validateAndGetDesktopImageContent(event) { const file = event.target.files[0]; if (file) { this.fileUploadService.isValidImageFile(file, 150, 150, 30720, (isValid) => { this.isValidDesktopImage = isValid; if (this.isValidDesktopImage) { this.fileUploadService.getImageBinaryString(file, (content) => { this.desktopImageFileContent = content; }) } }) } else this.isValidDesktopImage = false; } // Validate and get device categry image content validateAndGetDeviceImageContent(event) { const file = event.target.files[0]; if (file) { this.fileUploadService.isValidImageFile(file, 150, 150, 30720, (isValid) => { this.isValidDeviceImage = isValid; if (this.isValidDeviceImage) { this.fileUploadService.getImageBinaryString(file, (content) => { this.deviceImageFileContent = content; }) } }) } else this.isValidDeviceImage = false; } // Validate and get app categry image content validateAndGetAppImageContent(event) { const file = event.target.files[0]; if (file) { this.fileUploadService.isValidImageFile(file, 150, 150, 30720, (isValid) => { this.isValidAppImage = isValid; if (this.isValidAppImage) { this.fileUploadService.getImageBinaryString(file, (content) => { this.appImageFileContent = content; }) } }) } else this.isValidAppImage = false; } /* @usage: called when a form is submitted */ async onSubmit() { if(this.categoryForm.get('product_category').value === '' || this.categoryForm.get('desktopImageFileContent').value === '' || this.categoryForm.get('deviceImageFileContent').value === '' || this.categoryForm.get('appImageFileContent').value === '') { this.categoryForm.controls['product_category'].markAllAsTouched(); this.categoryForm.controls['desktopImage'].markAllAsTouched(); this.categoryForm.controls['deviceImage'].markAllAsTouched(); this.categoryForm.controls['appImage'].markAllAsTouched(); } if (this.categoryForm.valid && this.isValidDesktopImage && this.isValidDeviceImage && this.isValidAppImage) { this.categoryForm.get('desktopImageFileContent').setValue(this.desktopImageFileContent); this.categoryForm.get('deviceImageFileContent').setValue(this.deviceImageFileContent); this.categoryForm.get('appImageFileContent').setValue(this.appImageFileContent); this.categoryForm.get('product_category').setValue(this.product_category.value); this.itemCategoryService.updateCategoryImages(this.categoryForm.value).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.logger.debug('Category images has been updated'); this.toastr.success('Category images has been updated'); } }, (error) => { this.toastr.error('Some error occured'); this.logger.error(error); }, ); this.bsModalRef.hide(); } else { this.categoryForm.controls['desktopImage'].markAllAsTouched(); this.categoryForm.controls['deviceImage'].markAllAsTouched(); this.categoryForm.controls['appImage'].markAllAsTouched(); this.categoryForm.controls['product_category'].markAllAsTouched(); } } get desktopImage() { return this.categoryForm.get('desktopImage') } get deviceImage() { return this.categoryForm.get('deviceImage') } get appImage() { return this.categoryForm.get('appImage') } get product_category() { return this.categoryForm.get('product_category') } }