All files / src/components/common/Table helpers.ts

14.89% Statements 7/47
5% Branches 2/40
8.33% Functions 1/12
13.95% Lines 6/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240                                                                                                                                                                                                                                                                                                                                264x                                                                                   264x                               264x                                 3719x 3098x 3098x    
import { TListWrapperSection } from "@containers/types";
import { TableProps as MuiTableProps, TableRowProps } from "@mui/material";
import React, { Dispatch, SetStateAction } from "react";
 
export type ColumnBaseType =
  | "text"
  | "half-text"
  | "product-title"
  | "merchant-first"
  | "merchant-last"
  | "assignment"
  | "phase"
  | "in-out"
  | "processing-merchant"
  | "status"
  | "status-round"
  | "member"
  | "actions"
  | "empty"
  | "apiKey-mobile"
  | "risk-level"
  | "transaction-first"
  | "invisible"
  | "avatar-name";
 
export type TableColumnType = {
  /**
   * Title of the column head
   */
  title?: string | any;
  /**
   * If table can be sorted with the column values
   */
  sortable?: boolean;
 
  rightSort?: boolean;
  hideColumn?: boolean;
  /**
   * Keyname of row item to access the value
   */
  key: string | any;
  /**
   * Custom component to show the column value
   */
  renderColumn?: (item: any, open?: boolean) => React.ReactNode;
  /**
   * If is to be shown expand button
   */
  hasExpandButton?: boolean;
 
  pinkBorder?: boolean;
 
  iconButton?: boolean;
 
  columnWidth?: number | string;
 
  sx?: TableRowProps["sx"];
 
  headerPosition?: "left" | "right" | "center";
 
  columnType?: ColumnBaseType;
};
 
export type RenderExpandedRow = (rowData: any) => React.ReactNode;
 
export interface TableData {
  id?: string | number;
 
  [key: string]: any;
}
 
export interface TableColumnTypeCustom extends TableColumnType {
  hideColumn?: boolean;
}
 
export type TableProps = MuiTableProps & {
  lastItemRef?: (node?: Element | null | undefined) => void;
  small?: boolean;
  page?: number;
  rowsPerPage?: number | string;
  maxHeight?: string | number;
  rowHeight?: string | number;
  expandable?: boolean | ((row: any) => boolean);
  renderExpandedRow?: RenderExpandedRow;
  shouldRenderTable?: boolean;
  data: TableData[];
  columns: TableColumnTypeCustom[];
  totalRecords?: number;
  withPagination?: boolean;
  hasPaginationWhenEmpty?: boolean;
  emptyTableMessage?: string;
  iconButton?: boolean;
  noTrailing?: boolean;
  order?: any;
  orderBy?: any;
  onRequestSort?: any;
  pinkBorder?: boolean;
  pinkRowBorder?: boolean;
  count?: number;
  sortOptionOne?: "Sort A-Z" | "Ascending Date" | "Ascending Amount";
  sortOptionTwo?: "Sort Z-A" | "Descending Date" | "Descending Amount";
  total?: boolean;
  disabled?: boolean;
  selectable?: boolean;
  currentRowsPerPage?: number;
  isLoading?: boolean;
  sorting?: string;
  setSorting?: Dispatch<SetStateAction<string>>;
  handlePageChange?: (event: React.ChangeEvent<unknown>, value: number) => void;
  tableTitle?: string;
  searchBar?: React.ReactNode;
  downloadButton?: React.ReactNode;
  withBackground?: boolean;
  filters?: React.ReactNode;
  tabs?: React.ReactNode;
  initialSortingProperty?: string;
  rowRenderKey?: (row: any) => any;
  onOpenSidePanel?: any;
  openedModal?: string;
  numberOfPages?: number;
  BodyRowStyle?: React.CSSProperties;
  queryKey?: string;
  itemsPerPage?: number;
  isFetching?: boolean;
  handleRowClick?: (itemRow: any, idx: number) => void;
  alwaysShowDownloadBtn?: boolean;
  emptyBox?: boolean;
  emptyState?: TListWrapperSection;
  hideTableView?: boolean;
  totalExportRecords?: ExportRecordsTotal;
  showExportFilterBtns?: boolean;
  leftSideSearchBar?: boolean;
  defaultSort?: { key: string; isDesc?: boolean };
  hideLeftSideFilter?: boolean;
  canBeSelected?: boolean;
};
 
export type ExportRecordsTotal = {
  portfolio: number;
  underwriting: number;
  risk: number;
};
 
export type TableOrder = "asc" | "desc";
 
export function stableSort<T>(
  array: readonly T[],
  comparator: (a: T, b: T) => number,
) {
  const stabilizedThis = array?.map((el, index) => [el, index] as [T, number]);
  stabilizedThis?.sort((a, b) => {
    const order = comparator(a[0], b[0]);
    if (order !== 0) {
      return order;
    }
    return a[1] - b[1];
  });
  return stabilizedThis?.map((el) => el[0]);
}
 
const getProperty = <T>(object: T, path: string[] | (keyof T)[]) => {
  let result: any = object;
  for (let i = 0; i < path.length; i++) {
    result = result[path[i]];
    if (result === undefined) {
      break;
    }
  }
  return result;
};
 
export function descendingComparator<T>(a: T, b: T, orderBy: keyof T) {
  const path = typeof orderBy === "string" ? orderBy.split(".") : [orderBy];
  let firstOperator: any = getProperty(a, path);
  let secondOperator: any = getProperty(b, path);
 
  if (typeof firstOperator === "string" && typeof secondOperator === "string") {
    firstOperator = firstOperator.toLowerCase();
    secondOperator = secondOperator.toLowerCase();
  }
 
  if (firstOperator < secondOperator) {
    return -1;
  }
  if (firstOperator > secondOperator) {
    return 1;
  }
  return 0;
}
 
export function getComparator<Key extends keyof any>(
  order: TableOrder,
  orderBy: Key,
): (
  a: { [key in Key]: number | string },
  b: { [key in Key]: number | string },
) => number {
  return order === "asc"
    ? (a, b) => descendingComparator(a, b, orderBy)
    : (a, b) => -descendingComparator(a, b, orderBy);
}
 
export const SortLabelAsc = (label: string | undefined) => {
  if (
    label === "date" ||
    label === "time" ||
    label === "createdAt" ||
    label === "updatedAt" ||
    label === "joined"
  ) {
    return "Ascending Date";
  } else if (label === "amount" || label === "total") {
    return "Ascending Amount";
  } else {
    return "Sort A-Z";
  }
};
 
export const SortLabelDsc = (label: string | undefined) => {
  if (
    label === "date" ||
    label === "time" ||
    label === "createdAt" ||
    label === "updatedAt" ||
    label === "joined"
  ) {
    return "Descending Date";
  } else if (label === "amount" || label === "total") {
    return "Descending Amount";
  } else {
    return "Sort Z-A";
  }
};
 
export function capitalizeFirstLetter(str: string) {
  if (!str) return "";
  const lower = str.toLowerCase();
  return lower[0].toUpperCase() + lower.slice(1);
}