import { ChangeDetectorRef, Component, OnInit, Input } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MatTableDataSource, MatTableModule } from '@angular/material/table'; import { Utils } from '@pega/angular-sdk-components'; interface PegaDxilMyCaseWidgetProps { label?: string; } @Component({ selector: 'app-pega-dxil-my-case-widget', templateUrl: './pega-dxil-my-case-widget.component.html', styleUrls: ['./pega-dxil-my-case-widget.component.scss'], imports: [CommonModule, MatTableModule] }) export class PegaDxilMyCaseWidgetComponent implements OnInit { @Input() pConn$: typeof PConnect; configProps$: PegaDxilMyCaseWidgetProps; repeatList$: MatTableDataSource; fields$: any[]; displayedColumns$ = Array(); waitingForData = false; constructor( private utils: Utils, private cdRef: ChangeDetectorRef ) {} ngOnInit(): void { this.configProps$ = this.pConn$.getConfigProps(); const caseID = this.pConn$.getValue(PCore.getConstants().CASE_INFO.CASE_INFO_ID); const dataViewName = 'D_pyWorkHistory'; const context = this.pConn$.getContextName(); this.waitingForData = true; const caseHistoryData = PCore.getDataApiUtils().getData( dataViewName, { dataViewParameters: [{ CaseInstanceKey: caseID }] } as any, context ) as Promise; caseHistoryData.then((historyJSON: any) => { this.fields$ = [ { label: this.pConn$.getLocalizedValue('Date', '', ''), type: 'DateTime', fieldName: 'pxTimeCreated' }, { label: this.pConn$.getLocalizedValue('Description', '', ''), type: 'TextInput', fieldName: 'pyMessageKey' }, { label: this.pConn$.getLocalizedValue('Performed by', '', ''), type: 'TextInput', fieldName: 'pyPerformer' } ]; const tableDataResults = this.updateData(historyJSON.data.data, this.fields$); this.displayedColumns$ = this.getDisplayColumns(this.fields$); this.repeatList$ = new MatTableDataSource(tableDataResults); this.waitingForData = false; this.cdRef.markForCheck(); }); } updateData(listData: any[], fieldData: any[]): any[] { const returnList: any[] = new Array(); listData.forEach(row => { // copy const rowData = JSON.parse(JSON.stringify(row)); for (const field of fieldData) { const fieldName = field.fieldName; let formattedDate; switch (field.type) { case 'Date': formattedDate = this.utils.generateDate(rowData[fieldName], 'Date-Short-YYYY'); rowData[fieldName] = formattedDate; break; case 'DateTime': formattedDate = this.utils.generateDateTime(rowData[fieldName], 'DateTime-Short-YYYY'); rowData[fieldName] = formattedDate; break; default: break; } } returnList.push(rowData); }); return returnList; } getDisplayColumns(fields: any[] = []) { return fields.map(field => { return field.fieldName; }); } }