All files / components/Game FarmerActivityTable.jsx

100% Statements 17/17
100% Branches 13/13
100% Functions 5/5
100% Lines 17/17

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              3x                     47x       47x 47x     47x                 47x       47x                     47x         7x   16x 16x 2x   14x 14x               98x         98x               47x                                    
import React, { useState } from "react";
import { Link } from "react-router";
import OurTable from "main/components/OurTable";
import PageSizeSelector from "main/components/Utils/PageSizeSelector";
import { useBackend } from "main/utils/useBackend";
import { formatPacificTimestamp } from "main/utils/dateUtils";
 
const ACTIVITY_TYPE_LABELS = {
  0: "Viewed Play Page",
  1: "Bought Cows",
  2: "Sold Cows",
};
 
export default function FarmerActivityTable({
  gameId,
  userId,
  testid = "FarmerActivityTable",
}) {
  const [pageSize, setPageSize] = useState(10);
 
  // When userId isn't given, this table shows activity for the whole game
  // (see AdminGameActivityPage) rather than for a single farmer.
  const showFarmerName = userId === undefined;
  const url = showFarmerName
    ? "/api/farmeractivity/game"
    : "/api/farmeractivity/all";
  const params = showFarmerName
    ? { gameId: gameId }
    : { userId: userId, gameId: gameId };
 
  // The query key is only used internally by react-query for cache lookups
  // and isn't otherwise observable in the rendered output, so it's excluded
  // from mutation testing (see the `url`/`params` above, which drive the
  // actual request and are asserted on directly).
  // Stryker disable all
  const queryKey = showFarmerName
    ? `/api/farmeractivity/game?gameId=${gameId}`
    : `/api/farmeractivity/all?userId=${userId}&gameId=${gameId}`;
 
  const { data: activity } = useBackend(
    [queryKey],
    {
      method: "GET",
      url: url,
      params: params,
    },
    [],
  );
  // Stryker restore all
 
  const columns = [
    ...(showFarmerName
      ? [
          {
            Header: "Farmer Name",
            accessor: (row) => row.farmer?.username,
            Cell: ({ row }) => {
              const farmer = row.original.farmer;
              if (!farmer) {
                return "";
              }
              const farmerUrl = `/admin/farmeractivity/${farmer.gameId}/user/${farmer.userId}`;
              return <Link to={farmerUrl}>{farmer.username}</Link>;
            },
          },
        ]
      : []),
    {
      Header: "Timestamp",
      accessor: "timestamp",
      Cell: ({ value }) => formatPacificTimestamp(value),
    },
    {
      Header: "Activity",
      accessor: "activityType",
      Cell: ({ value }) => ACTIVITY_TYPE_LABELS[value] ?? value,
    },
    {
      Header: "Cows",
      accessor: "numCows",
    },
  ];
 
  return (
    <>
      <PageSizeSelector
        value={pageSize}
        onChange={setPageSize}
        options={[10, 50, 100]}
        testid={`${testid}-page-size-selector`}
      />
      <OurTable
        data={activity}
        columns={columns}
        testid={testid}
        centered={false}
        pageSize={pageSize}
      />
    </>
  );
}