All files / components/Leaderboard LeaderboardTable.jsx

100% Statements 20/20
100% Branches 8/8
100% Functions 9/9
100% Lines 20/20

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            3x 3x 3x                           163x           163x         163x       65x 65x               2x 1x               1x 1x                                     86x                 86x             86x                         86x             86x             86x         163x   163x                                  
import OurTable from "main/components/OurTable";
import PageSizeSelector from "main/components/Utils/PageSizeSelector";
import usePageSize from "main/utils/usePageSize";
import { Link } from "react-router";
import { Button } from "react-bootstrap";
 
const PAGE_SIZE_OPTIONS = [20, 50, 100, 200, 500];
const DEFAULT_PAGE_SIZE = 20;
const PAGE_SIZE_STORAGE_KEY = "leaderboard-page-size";
 
// should take in a players list from a game. isAdminView (true only for an
// admin NOT viewing the leaderboard in "Student View" - see DashboardPage)
// and isCourseLinkedGame (activity is never tracked for a game with no
// course) together control whether the admin-only Activity column is shown
// at all; within that column, a farmer who isn't a student on the course's
// roster (row.student === false) gets "Not a student" instead of a button,
// since no activity is tracked for them either. See issue #291.
export default function LeaderboardTable({
  leaderboardUsers,
  isAdminView,
  isCourseLinkedGame,
}) {
  const [pageSize, handlePageSizeChange] = usePageSize({
    storageKey: PAGE_SIZE_STORAGE_KEY,
    options: PAGE_SIZE_OPTIONS,
    defaultPageSize: DEFAULT_PAGE_SIZE,
  });
 
  const USD = new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
  });
 
  const columns = [
    {
      Header: "Farmer",
      accessor: (row, _rowIndex) => {
        const url = `/admin/play/${row.gameId}/user/${row.userId}`;
        return <Link to={url}>{row.username}</Link>;
      },
    },
    ...(isAdminView && isCourseLinkedGame
      ? [
          {
            Header: "Activity",
            accessor: (row, _rowIndex) => {
              if (!row.student) {
                return (
                  <span
                    data-testid={`LeaderboardTable-cell-row-${_rowIndex}-col-Activity-not-a-student`}
                  >
                    Not a student
                  </span>
                );
              }
              const url = `/admin/farmeractivity/${row.gameId}/user/${row.userId}`;
              return (
                <Button
                  as={Link}
                  to={url}
                  variant="outline-secondary"
                  size="sm"
                  data-testid={`LeaderboardTable-cell-row-${_rowIndex}-col-Activity-button`}
                >
                  Activity
                </Button>
              );
            },
          },
        ]
      : []),
    {
      Header: "Total Wealth",
      accessor: "totalWealth",
      Cell: (props) => {
        return (
          <div style={{ textAlign: "right" }}>{USD.format(props.value)}</div>
        );
      },
    },
    {
      Header: "Cows Owned",
      accessor: "numOfCows",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
    {
      Header: "Cow Health",
      accessor: "cowHealth",
      Cell: (props) => {
        return (
          <div style={{ textAlign: "right" }}>
            {typeof props.value === "number"
              ? props.value.toFixed(2)
              : props.value}
          </div>
        );
      },
    },
    {
      Header: "Cows Bought",
      accessor: "cowsBought",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
    {
      Header: "Cows Sold",
      accessor: "cowsSold",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
    {
      Header: "Cow Deaths",
      accessor: "cowDeaths",
      Cell: (props) => {
        return <div style={{ textAlign: "right" }}>{props.value}</div>;
      },
    },
  ];
 
  const testid = "LeaderboardTable";
 
  return (
    <>
      <PageSizeSelector
        value={pageSize}
        onChange={handlePageSizeChange}
        options={PAGE_SIZE_OPTIONS}
        testid={`${testid}-page-size-selector`}
      />
      <OurTable
        data={leaderboardUsers}
        columns={columns}
        testid={testid}
        pageSize={pageSize}
      />
    </>
  );
}