import React, { Component } from "react"; import deepFind from "../../utils/deepfind"; import Column from "./columns"; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faBook, faBookJournalWhills, faEdit, faTrash } from "@fortawesome/free-solid-svg-icons"; import { Link } from "react-router-dom"; import ConfirmButton from "./ConfirmButton"; import { Buffer } from 'buffer'; import Button, { ButtonType } from "./Button"; import { DateView } from "./DateView"; export interface AuditParams{ entityName : string, primaryKey : string } export interface TableBodyProps{ data : T[] | undefined; keyName : string; columns : Column[]; editPath? : string; selectedRow? : T; canEdit? : ( item : T ) => boolean; canDelete? : ( item : T ) => boolean; onDelete?: ( item? : T ) => void; onAuditParams?: ( item : T ) => AuditParams; onSelectRow? : ( item : T ) => void; showSecondaryAudit : boolean; } class TableBody extends Component> { resolvePath = ( path : string, args : string[]) => { let modifiedPath = path; let index : number = 0; while ( index < args.length ) { modifiedPath = modifiedPath.replace("{"+index+"}", args[index]) index++; } return modifiedPath; } renderCell = (item : T, column : Column) => { const {keyName} = this.props; if (column.content) return column.content(item); const foundItem = deepFind(item, column.path || column.key) let columnContent : JSX.Element; if (foundItem instanceof Date) { columnContent = } else if (typeof foundItem === "object"){ columnContent = <>; } else { columnContent = <> {foundItem} ; } const linkPath = column.link; if (linkPath !== undefined){ const resolvedlinkPath = this.resolvePath( linkPath, [ (item as any)[keyName] ] ); columnContent = {columnContent}; } return <> {columnContent} ; }; clickRow = ( value : T ) => { const { onSelectRow } = this.props; if (onSelectRow !== undefined) onSelectRow( value ); } createKey = (item : T, column : Column) => { const { keyName } = this.props; return (item as any)[keyName] + '_' + (column.path || column.key); }; handleAuditParams = ( item : T, primaryOnly : boolean ) => { const { onAuditParams } = this.props; if (onAuditParams !== undefined) { var auditParams = onAuditParams(item); let json = JSON.stringify(auditParams); var params = Buffer.from(json).toString('base64') ; var queryString = ""; if (primaryOnly===false) queryString += "?primaryOnly=" + primaryOnly; return "/audit/" + params + queryString; } return ""; } render() { const { data, keyName, selectedRow, columns, editPath, canEdit, canDelete, onDelete, onAuditParams, showSecondaryAudit } = this.props; const showDelete:boolean = onDelete != null; const showEdit:boolean = (editPath != null) && (editPath !== ""); const showAudit:boolean = onAuditParams !== undefined; return ( {data?.map((item) => { let classNames = ""; if (selectedRow === item) { classNames+="table-primary"; } return ( {columns.map((column) => ( this.clickRow(item)}>{this.renderCell(item, column)} ))} {showEdit && {(canEdit === undefined || canEdit(item)) && }} {showDelete && {(canDelete === undefined || canDelete(item)) && }} {showAudit && } {showAudit && showSecondaryAudit && } ) })} ); } } export default TableBody;