All files / pages/RecommendationRequests RecommendationRequestsEditPage.js

90% Statements 18/20
76.92% Branches 10/13
100% Functions 5/5
100% Lines 18/18

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                4x           4x                         4x     1x   2x     2x   2x 2x     1x                                           4x 1x         4x             4x   4x 1x     4x 1x     3x                              
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useParams } from "react-router-dom";
import RecommendationRequestForm from "main/components/RecommendationRequests/RecommendationRequestForm";
import { Navigate } from "react-router-dom";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
 
export default function RecommendationRequestsEditPage({ storybook = false }) {
  let { id } = useParams();
 
  const {
    data: recommendationrequests,
    _error,
    _status,
  } = useBackend(
    // Stryker disable next-line all : don't test internal caching of React Query
    [`/api/recommendationrequests?id=${id}`],
    {
      // Stryker disable next-line all : GET is the default, so mutating this to "" doesn't introduce a bug
      method: "GET",
      url: `/api/recommendationrequests`,
      params: {
        id,
      },
    },
  );
 
  const objectToAxiosPutParams = (recommendationrequests) => {
    // Ensure dates are properly formatted for the backend
    // Stryker disable next-line all
    const formatDate = (dateString) => {
      // Stryker disable next-line all
      Iif (!dateString) return dateString;
      // If the date is already in ISO format, return it
      // Stryker disable next-line all
      Iif (dateString.includes("T")) return dateString;
      // Otherwise, convert YYYY-MM-DD to ISO format with time
      const date = new Date(dateString);
      return date.toISOString();
    };
 
    return {
      url: "/api/recommendationrequests",
      method: "PUT",
      params: {
        id: recommendationrequests.id,
      },
      data: {
        requesterEmail: recommendationrequests.requesterEmail,
        professorEmail: recommendationrequests.professorEmail,
        explanation: recommendationrequests.explanation,
        dateRequested: formatDate(recommendationrequests.dateRequested),
        dateNeeded: formatDate(recommendationrequests.dateNeeded),
        // Stryker disable next-line all
        done:
          // Stryker disable next-line all
          recommendationrequests.done === "true" ||
          // Stryker disable next-line all
          recommendationrequests.done === true,
      },
    };
  };
 
  const onSuccess = (recommendationrequests) => {
    toast(
      `Recommendation Request Updated - id: ${recommendationrequests.id} requesterEmail: ${recommendationrequests.requesterEmail}`,
    );
  };
 
  const mutation = useBackendMutation(
    objectToAxiosPutParams,
    { onSuccess },
    // Stryker disable next-line all : hard to set up test for caching
    [`/api/recommendationrequests?id=${id}`],
  );
 
  const { isSuccess } = mutation;
 
  const onSubmit = async (data) => {
    mutation.mutate(data);
  };
 
  if (isSuccess && !storybook) {
    return <Navigate to="/recommendationrequests" />;
  }
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h1>Edit Recommendation Request</h1>
        {recommendationrequests && (
          <RecommendationRequestForm
            submitAction={onSubmit}
            buttonLabel={"Update"}
            initialContents={recommendationrequests}
          />
        )}
      </div>
    </BasicLayout>
  );
}