import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; 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, FormArray, Validators } from '@angular/forms'; import { DataTableDirective } from 'angular-datatables'; import { Subject } from 'rxjs'; import { DatePipe } from '@angular/common'; @Component({ selector: 'app-system-notification-list', templateUrl: './system-notification-list.component.html', styleUrls: ['./system-notification-list.component.css'] }) export class SystemNotificationListComponent implements OnDestroy, OnInit { lstSystemNotification: any; modelSystemNotification = new SystemNotification(); APIResponse: any; systemNotificationForm: FormGroup; dtOptions: any = {} dtTrigger: Subject = new Subject(); @ViewChild(DataTableDirective) dtElement: DataTableDirective; formData: FormData = new FormData(); closeResult: string; previewContent: string = ''; fileContent: string = ''; fileType: string = ''; id: number = 0; showModal: boolean = false; deleteModal; previewModal; constructor( private service: SystemNotificationService, private common: CommonService, private formBuilder: FormBuilder, private datePipe: DatePipe, ) {} ngOnInit(): void { this.setFormBuilder(); this.dtOptions = this.common.dataTableOptionsForSN(true, false, false, false, true, false, true); this.fillGrid(); } setFormBuilder() { this.systemNotificationForm = this.formBuilder.group({ lstSystemNotification: this.formBuilder.array([]) }); } setEditForm(data): FormGroup { return this.formBuilder.group({ lstSystemNotification: this.setArrayGroup(data) }) } setArrayGroup(data: any[] | null): FormArray { var finalArrayGroup = this.formBuilder.array([]); data.map(x => { finalArrayGroup.push(this.formBuilder.group({ NotificationId: [x.NotificationId], NotificationName: [x.NotificationName == null ? "" : x.NotificationName], IsActive: [x.IsActive == true ? 1 : 0], Content: [x.Content == null ? "" : x.Content], StartDate: [x.StartDate == null ? "" : this.getDate(x.StartDate)], EndDate: [x.EndDate == null ? "" : this.getDate(x.EndDate)], CompanyId: [x.CompanyId], SortOrder: [x.SortOrder, [Validators.required, Validators.pattern("^[0-9]*$")]], IsSystemNotification: [x.IsSystemNotification == true ? 1 : 0], FeaturedImageName: [x.FeaturedImageName == null ? "" : x.FeaturedImageName], FeaturedImagePath: [x.FeaturedImagePath == null ? "" : x.FeaturedImagePath], ImageData: [x.ImageData == null ? "" : x.ImageData], Visible: false, format: [x.format == null ? "" : x.format], UserId: [x.UserId], IpAddress: [x.IpAddress == null ? "" : x.IpAddress] })) }) return finalArrayGroup; } fillGrid() { this.service.getList(data => { this.APIResponse = data; this.lstSystemNotification = this.APIResponse.objResponse.responseContent; if (this.lstSystemNotification != null || this.lstSystemNotification != undefined) { var lstFinalSystemNotification = []; this.lstSystemNotification.forEach(function (data) { if (data.ImageData != null) { if (data.ImageData.indexOf('/mp4') > -1) { data = { ...data, format: 'video' }; } else if (data.ImageData.indexOf('/png') > -1 || data.ImageData.indexOf('/jpg') > -1 || data.ImageData.indexOf('/jpeg')) { data = { ...data, format: 'image' }; } } lstFinalSystemNotification.push(data); }) this.sorting(lstFinalSystemNotification); this.systemNotificationForm = this.setEditForm(lstFinalSystemNotification); } //dtTrigger to rerender again this.dtTrigger.next(); }, (error) => { console.error('Error: ' + error); return error(error); }); } saveRecord(data) { if (this.systemNotificationForm.valid) { this.modelSystemNotification = data; if (this.CheckTostValidation(this.modelSystemNotification)) { this.formData = new FormData(); this.setModelBeforeSubmit(); this.service.saveSystemNotification(this.formData, data => { this.APIResponse = data; if (this.APIResponse.objResponse.statusCode == "200") { this.common.showSuccess("System Notification Changes Applied Successfully.", "Success") this.rerender(); } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } } } showDeleteModal(id) { this.showModal = true; this.deleteModal = { tableName: 'Notification', id: id }; this.previewModal = ""; } showPreviewModal(content, file, fileFormat) { this.showModal = true; this.previewModal = { content: content, file: file, fileFormat: fileFormat } this.deleteModal = ""; } getModalToggleValue(data) { this.showModal = data.toggle; if (data.isDeleteSuccess) { this.rerender(); } } setModelBeforeSubmit() { this.formData.append('NotificationId', String(this.modelSystemNotification.NotificationId)); 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('FeaturedImageName', String(this.modelSystemNotification.FeaturedImageName)); this.formData.append('FeaturedImagePath', String(this.modelSystemNotification.FeaturedImagePath)); this.formData.append('ImageData', String(this.modelSystemNotification.ImageData)); this.formData.append('UserId', String(this.modelSystemNotification.UserId)); this.formData.append('IpAddress', String(this.modelSystemNotification.IpAddress)); } 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 } ngOnDestroy(): void { // Do not forget to unsubscribe the event this.dtTrigger.unsubscribe(); } rerender(): void { this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => { // Destroy the table first dtInstance.destroy(); this.fillGrid(); }); } sorting(data) { data.sort((a, b) => a.SortOrder - b.SortOrder); } 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; } displayFieldCss(i: number, field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).valid, 'is-invalid': (this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).invalid && (this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).dirty || this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).touched)) }; } else { return { 'plain-valid': this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).valid, 'plain-invalid': (this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).invalid && (this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).dirty || this.systemNotificationForm.get('lstSystemNotification')['controls'][i].get(field).touched)) }; } } }