All files / components/Dogs DogTable.js

100% Statements 20/20
100% Branches 7/7
100% Functions 6/6
100% Lines 19/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 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                            31x 1x 1x     31x               31x         31x 1x 1x     31x   31x 1x     31x 1x     31x                             31x 24x 24x 10x             31x        
import React from "react";
import OurTable, { ButtonColumn } from "main/components/OurTable";
import { useNavigate } from "react-router-dom";
import { useBackendMutation } from "../../utils/useBackend";
import { toast } from "react-toastify";
import { hasRole } from "main/utils/currentUser";
 
export default function DogTable({
  dogs,
  showButtons = true,
  testIdPrefix = "DogTable",
  currentUser = null,
}) {
 
  const handleDeleteSuccess = (message) => {
    console.log(message);
    toast(message);
  };
 
  const createAxiosParams = (cell) => ({
    url: "/api/dogs",
    method: "DELETE",
    params: {
      id: cell.row.values.id,
    },
  });
  // Stryker disable all : don't test internal caching of React Query
  const deleteMutation = useBackendMutation(createAxiosParams, { onSuccess: handleDeleteSuccess }, [
    "/api/dogs/all",
  ]);
  // Stryker enable all 
 
  const handleDelete = async (cell) => {
    deleteMutation.mutate(cell);
    window.location.reload();
  };
 
  const navigate = useNavigate();
 
  const handleEdit = (cell) => {
    navigate(`/dogs/edit/${cell.row.values.id}`);
  };
 
  const handleDetails = (cell) => {
    navigate(`/dogs/details/${cell.row.values.id}`);
  };
 
  const columns = [
    {
      Header: "Id",
      accessor: "id",
    },
    {
      Header: "Name",
      accessor: "name",
    },
    {
      Header: "Breed",
      accessor: "breed",
    },
  ];
 
  if (showButtons) {
    columns.push(ButtonColumn("Details", "primary", handleDetails, testIdPrefix));
    if (hasRole(currentUser, "ROLE_ADMIN")) {
      columns.push(
        ButtonColumn("Edit", "primary", handleEdit, testIdPrefix),
        ButtonColumn("Delete", "danger", handleDelete, testIdPrefix)
      );
    }
  }
 
  return (
    <OurTable data={dogs} columns={columns} testid={testIdPrefix} />
  );
}