All files / src/components/Merchants/BulkInvite/utils index.ts

47.61% Statements 10/21
18.75% Branches 3/16
25% Functions 1/4
47.36% Lines 9/19

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        1x 1x 1x 3x   2x     2x     1x     1x                                 1x            
import { TInviteElement, TInvitesMap } from "../types";
import { COLUMN_NAMES } from "../constants";
import { SortingOrder } from "@redux/types/sort";
 
export const getSelectedKeys = (map: TInvitesMap, mode: "single" | "bulk") => {
  const bulkKeys: string[] = [];
  for (const [key, value] of Array.from(map.entries())) {
    if (!value.checked) continue;
 
    Iif (mode === "single") {
      return key;
    } else {
      bulkKeys.push(key);
    }
  }
  return bulkKeys;
};
 
const compare = (a: string, b: string, order: SortingOrder) => {
  if (a > b) {
    return order === "asc" ? 1 : -1;
  } else if (a < b) {
    return order === "asc" ? -1 : 1;
  } else {
    return 0;
  }
};
 
type Element = [string, TInviteElement];
 
type InvitesSorter = (
  attribute: (typeof COLUMN_NAMES)[number],
  order: SortingOrder,
) => (a: Element, b: Element) => 1 | -1 | 0;
 
export const invitesSorter: InvitesSorter = (attribute, order) => (a, b) => {
  const isEmail = attribute === COLUMN_NAMES[0];
  const first = isEmail ? a[1].pahEmail : a[1].merchantName;
  const second = isEmail ? b[1].pahEmail : b[1].merchantName;
  return compare(first.toLowerCase(), second.toLowerCase(), order);
};