All files / pages/MyReviews MyReviewsIndexPage.js

100% Statements 13/13
100% Branches 2/2
100% Functions 7/7
100% Lines 13/13

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                9x       7x   1x 1x     3x 3x           8x           8x           8x 1x                 1x         7x   7x                              
import React from "react";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { useNavigate } from "react-router";
import { toast } from "react-toastify";
import ReviewTable from "main/components/Review/ReviewTable";
 
export function extractReview(reviewOrCell) {
  return reviewOrCell?.row?.original ?? reviewOrCell;
}
 
export function useHandlers(navigate, deleteMutation) {
  return {
    handleEdit: (reviewOrCell) => {
      const review = extractReview(reviewOrCell);
      navigate(`/reviews/edit/${review.id}`);
    },
    handleDelete: (reviewOrCell) => {
      const review = extractReview(reviewOrCell);
      deleteMutation.mutate(review);
    },
  };
}
 
export default function MyReviewsIndexPage() {
  const navigate = useNavigate();
 
  const {
    data: reviews,
    error: _error,
    status: _status,
  } = useBackend(
    ["/api/reviews/userReviews"],
    { method: "GET", url: "/api/reviews/userReviews" },
    [],
  );
 
  const deleteMutation = useBackendMutation(
    (review) => ({
      url: `/api/reviews/reviewer`,
      method: "DELETE",
      params: {
        id: review.id,
      },
    }),
    {
      onSuccess: () => {
        toast("Review deleted");
      },
    },
  );
 
  const { handleEdit, handleDelete } = useHandlers(navigate, deleteMutation);
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h1>My Reviews</h1>
        <ReviewTable
          reviews={reviews}
          userOptions={true}
          moderatorOptions={false}
          onEdit={handleEdit}
          onDelete={handleDelete}
        />
      </div>
    </BasicLayout>
  );
}