import { Component, OnInit, NgZone, ElementRef } from '@angular/core'; import { DatePipe } from '@angular/common'; import { ActivatedRoute, Router } from "@angular/router"; import { CommonService } from '../../../../../services/common.service' import { SystemNotification } from '../../../../../models/admin/system/system-notification/system-notification'; import { SystemNotificationService } from '../../../../../services/admin/system/system-notification/system-notification.service'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { CustomValidator } from '../../../../../core/custom-validator'; @Component({ selector: 'app-system-notification-add', templateUrl: './system-notification-add.component.html', styleUrls: ['./system-notification-add.component.css'] }) export class SystemNotificationAddComponent implements OnInit { modelSystemNotification = new SystemNotification(); systemNotificationForm: FormGroup; systemNotificationAPIResponse: any; tinyMCEInitOptions; featuredImage: File = null; _paramId: number = 0; previewContent: string = ''; fileContent: string = ''; formData: FormData = new FormData(); fileName: string = ''; format: string = ''; showModal: boolean = false; previewModal; constructor( private formBuilder: FormBuilder, private route: ActivatedRoute, private router: Router, private ngZone: NgZone, private service: SystemNotificationService, private common: CommonService, private datePipe: DatePipe, private elementRef: ElementRef ) { } ngOnInit(): void { this._paramId = Number(this.route.snapshot.params.id); this.setFormBuilder(); this.tinyMCEInitOptions = this.common.tinyMCEOptions(); if (this._paramId == 0) { this.setAddForm(); } else { this.service.getById(this._paramId, data => { this.systemNotificationAPIResponse = data; this.modelSystemNotification = this.systemNotificationAPIResponse.objResponse.responseContent; this.setEditForm(); if (this.modelSystemNotification.ImageData != "") { if (this.modelSystemNotification.ImageData.indexOf('/mp4') > -1) { this.format = 'video'; } else if (this.modelSystemNotification.ImageData.indexOf('/png') > -1 || this.modelSystemNotification.ImageData.indexOf('/jpg') > -1) { this.format = 'image'; } this.fileContent = this.modelSystemNotification.ImageData; this.fileName = this.modelSystemNotification.FeaturedImageName; } }, (error) => { console.error('Error: ' + error); return error(error); }); } } setFormBuilder() { this.systemNotificationForm = this.formBuilder.group({ NotificationId: [''], NotificationName: ['', [Validators.required,CustomValidator.noWhitespaceValidator]], IsActive: [''], Content: ['', Validators.required], StartDate: [''], EndDate: [''], CompanyId: [''], SortOrder: ['', [Validators.required, Validators.pattern("^[0-9]*$"),CustomValidator.noWhitespaceValidator]], IsSystemNotification: [''], FeaturedImage: [''], FeaturedImageName: [''], FeaturedImagePath: [''], fileContent: [''], UserId: [''], IpAddress: [''], }); } setAddForm() { this.systemNotificationForm.setValue({ NotificationId: 0, NotificationName: '', IsActive: 1, Content: '', StartDate: this.fromJsonDate(new Date()), EndDate: this.fromJsonDate(new Date()), CompanyId: 0, SortOrder: 0, IsSystemNotification: 1, FeaturedImage: '', FeaturedImageName: '', FeaturedImagePath: '', fileContent: '', UserId: 1, IpAddress: '10.10.10.1', }); } setEditForm() { this.systemNotificationForm.setValue({ NotificationId: this._paramId, NotificationName: this.modelSystemNotification.NotificationName == null ? "" : this.modelSystemNotification.NotificationName, IsActive: this.modelSystemNotification.IsActive == true ? 1 : 0, Content: this.modelSystemNotification.Content == null ? "" : this.modelSystemNotification.Content, StartDate: this.modelSystemNotification.StartDate == null ? "" : this.getDate(this.modelSystemNotification.StartDate), EndDate: this.modelSystemNotification.EndDate == null ? "" : this.getDate(this.modelSystemNotification.EndDate), CompanyId: this.modelSystemNotification.CompanyId, SortOrder: this.modelSystemNotification.SortOrder, IsSystemNotification: this.modelSystemNotification.IsSystemNotification == true ? 1 : 0, FeaturedImage: this.modelSystemNotification.FeaturedImage, FeaturedImageName: this.modelSystemNotification.FeaturedImageName == null ? "" : this.modelSystemNotification.FeaturedImageName, FeaturedImagePath: this.modelSystemNotification.FeaturedImagePath == null ? "" : this.modelSystemNotification.FeaturedImagePath, fileContent: this.modelSystemNotification.ImageData == null ? "" : this.modelSystemNotification.ImageData, UserId: this.modelSystemNotification.UserId, IpAddress: this.modelSystemNotification.IpAddress == null ? "" : this.modelSystemNotification.IpAddress, }); } showPreviewModal() { this.showModal = true; this.previewContent = this.systemNotificationForm.get("Content").value; this.previewModal = { content: this.previewContent, file: this.fileContent, fileFormat: this.format } } getModalToggleValue(data) { this.showModal = data.toggle; } submitform() { if (this.systemNotificationForm.valid) { this.modelSystemNotification = this.systemNotificationForm.value; if (this.CheckTostValidation(this.modelSystemNotification)) { this.setModelBeforeSubmit(); this.service.saveSystemNotification(this.formData, data => { this.systemNotificationAPIResponse = data; if (this.systemNotificationAPIResponse.objResponse.statusCode == "200") { this.common.showSuccess("System Notification Changes Applied Successfully.", "Success") this.ngZone.run(() => this.navigateTo('/admin/admin-system/system-notification-list')); } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } } else { this.common.validateAllFormFields(this.systemNotificationForm); this.common.validationfocus(this.systemNotificationForm, this.elementRef); if (this.systemNotificationForm.get("Content").value == "" && this.systemNotificationForm.get("NotificationName").value == "") { this.common.showError("Please enter required fields.", "Validation Alert!") } else if (this.systemNotificationForm.get("NotificationName").value == "") { this.common.showError("Please enter Title.", "Validation Alert!") } else if (this.systemNotificationForm.get("Content").value == "") { this.common.showError("Please enter Content.", "Validation Alert!") } } } setModelBeforeSubmit() { this.formData.append('NotificationId', String(this._paramId)); this.formData.append('NotificationName', String(this.modelSystemNotification.NotificationName)); this.formData.append('IsActive', String(Boolean(Number(this.modelSystemNotification.IsActive)))); this.formData.append('Content', String(this.modelSystemNotification.Content)); this.formData.append('StartDate', String(this.setDate(this.modelSystemNotification.StartDate))); this.formData.append('EndDate', String(this.setDate(this.modelSystemNotification.EndDate))); this.formData.append('CompanyId', String(this.modelSystemNotification.CompanyId)); this.formData.append('SortOrder', String(this.modelSystemNotification.SortOrder)); this.formData.append('IsSystemNotification', String(Boolean(Number(this.modelSystemNotification.IsSystemNotification)))); this.formData.append('FeaturedImage', this.featuredImage); this.formData.append('FeaturedImageName', String(this.modelSystemNotification.FeaturedImageName)); this.formData.append('FeaturedImagePath', String(this.modelSystemNotification.FeaturedImagePath)); this.formData.append('fileContent', String(this.modelSystemNotification.ImageData)); this.formData.append('UserId', String(this.modelSystemNotification.UserId)); this.formData.append('IpAddress', String(this.modelSystemNotification.IpAddress)); } displayFieldCss(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.systemNotificationForm.get(field).valid, 'is-invalid': (this.systemNotificationForm.get(field).invalid && (this.systemNotificationForm.get(field).dirty || this.systemNotificationForm.get(field).touched)) }; } else { return { 'plain-valid': this.systemNotificationForm.get(field).valid, 'plain-invalid': (this.systemNotificationForm.get(field).invalid && (this.systemNotificationForm.get(field).dirty || this.systemNotificationForm.get(field).touched)) }; } } onFeaturedImage(fileInput: any) { const reader = new FileReader(); if (fileInput.target.files[0].type == 'image/png' || fileInput.target.files[0].type == 'image/jpeg' || fileInput.target.files[0].type == 'video/mp4') { if (fileInput.target.files[0].type.indexOf('image') > -1) { this.format = 'image'; } else if (fileInput.target.files[0].type.indexOf('video') > -1) { this.format = 'video'; } this.featuredImage = fileInput.target.files[0]; this.fileName = fileInput.target.files[0].name; reader.readAsDataURL(this.featuredImage); reader.onload = () => { this.fileContent = reader.result as string; }; } else { this.common.showError("Please select valid file.", "Wrong File Format!") } } fromJsonDate(jDate): string { const bDate: Date = new Date(jDate); return bDate.toISOString().substring(0, 10); //Ignore time } CheckTostValidation(modelSystemNotification: SystemNotification) { let haserror = false; let error = ''; if (modelSystemNotification.EndDate != '' && modelSystemNotification.StartDate > modelSystemNotification.EndDate) { haserror = true; error += 'End Date should be greater than Start Date.' } if (haserror) { this.common.showError(error, "Validation Alert!") return false } return true } setDate(date) { let _pickYear = 0; let _pickMonth = 0; let _pickDay = 0; if (date != undefined && date != "" && date != null) { _pickYear = Number(this.datePipe.transform(date, 'yyyy')); _pickMonth = Number(this.datePipe.transform(date, 'MM')); _pickDay = Number(this.datePipe.transform(date, 'dd')); return _pickMonth + "-" + _pickDay + "-" + _pickYear; } else return null; } getDate(date) { let _pickYear = ""; let _pickMonth = ""; let _pickDay = ""; if (date != undefined && date != "" && date != null) { _pickYear = this.datePipe.transform(date, 'yyyy'); _pickMonth = this.datePipe.transform(date, 'MM'); _pickDay = this.datePipe.transform(date, 'dd'); return _pickYear + "-" + _pickMonth + "-" + _pickDay; } else return null; } navigateTo(url) { this.router.navigate([url]); } }