All files / pages/MyReviews MyReviewsIndexPage.js

92.85% Statements 26/28
75% Branches 6/8
100% Functions 8/8
96.29% Lines 26/27

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                  33x 32x             32x 32x   32x 4x             3x 3x 3x     1x             32x 1x 1x     32x 5x 5x     32x   4x 4x     32x 1x 1x     32x 12x               20x               20x                                                                      
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import ReviewTable from "main/components/Reviews/ReviewTable";
import { useNavigate } from "react-router";
import { useState } from "react";
import { Modal, Button } from "react-bootstrap";
import { toast } from "react-toastify";
 
export default function MyReviewsIndexPage() {
  const navigate = useNavigate();
  const { data, isLoading, error } = useBackend(
    // Stryker disable next-line all: don't test internal caching of React Query
    ["/api/reviews/userReviews"],
    // Stryker disable next-line all: default method is get, so replacing with an empty string will do nothing
    { method: "GET", url: `/api/reviews/userReviews` },
  );
 
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [pendingDelete, setPendingDelete] = useState(null);
 
  const deleteMutation = useBackendMutation(
    (review) => ({
      url: "/api/reviews/reviewer",
      method: "DELETE",
      params: { id: review.id },
    }),
    {
      onSuccess: () => {
        toast.success("Review deleted");
        setShowDeleteModal(false);
        setPendingDelete(null);
      },
      onError: () => {
        toast.error("Error deleting review");
      },
    },
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/api/reviews/userReviews"],
  );
 
  const onEdit = (cell) => {
    const id = cell.row.original.id;
    navigate(`/myreviews/edit/${id}`);
  };
 
  const onDelete = (cell) => {
    setPendingDelete(cell.row.original);
    setShowDeleteModal(true);
  };
 
  const handleDeleteConfirm = () => {
    // Stryker disable next-line all : don't test guard clause of pendingDelete
    Iif (!pendingDelete) return;
    deleteMutation.mutate(pendingDelete);
  };
 
  const handleDeleteCancel = () => {
    setShowDeleteModal(false);
    setPendingDelete(null);
  };
 
  if (isLoading) {
    return (
      <BasicLayout>
        <p>Loading...</p>
      </BasicLayout>
    );
  }
 
  // Stryker disable next-line all : Don't mutate error block
  Iif (error) {
    return (
      <BasicLayout>
        <p>Error loading reviews.</p>
      </BasicLayout>
    );
  }
 
  return (
    <BasicLayout>
      <h1>My Reviews</h1>
      <ReviewTable
        data={data}
        userOptions={true}
        moderatorOptions={false}
        onEdit={onEdit}
        onDelete={onDelete}
      />
      <Modal
        show={showDeleteModal}
        onHide={handleDeleteCancel}
        data-testid="delete-modal"
      >
        <Modal.Header closeButton>
          <Modal.Title>Confirm Delete</Modal.Title>
        </Modal.Header>
        <Modal.Body>Are you sure you want to delete this review?</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleDeleteCancel}>
            Cancel
          </Button>
          <Button
            variant="danger"
            onClick={handleDeleteConfirm}
            disabled={deleteMutation.isLoading}
          >
            {deleteMutation.isLoading ? "Deleting..." : "Delete"}
          </Button>
        </Modal.Footer>
      </Modal>
    </BasicLayout>
  );
}