All files / pages/Home HomePageLoggedIn.jsx

100% Statements 16/16
100% Branches 12/12
100% Functions 5/5
100% Lines 14/14

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 115 116 117 118 119                    14x 14x               14x                               14x               14x   14x                 14x 1x 1x     14x       14x 1x     14x   14x                                                                                                  
import { useState } from "react";
import ProjectTable from "main/components/Projects/ProjectTable";
import ProjectModal from "main/components/Projects/ProjectModal";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import { useCurrentUser, hasRole } from "main/utils/currentUser";
import Button from "react-bootstrap/Button";
 
export default function HomePageLoggedIn() {
  const currentUser = useCurrentUser();
  const isResearcher =
    hasRole(currentUser, "ROLE_RESEARCHER") ||
    hasRole(currentUser, "ROLE_ADMIN");
 
  const {
    data: ownedProjects,
    error: _ownedError,
    status: _ownedStatus,
  } = useBackend(
    ["/api/projects/list/owner"],
    // Stryker disable next-line StringLiteral : The default value for an empty ("") method is GET.
    { method: "GET", url: "/api/projects/list/owner" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
    false,
    {
      enabled: isResearcher,
    },
  );
 
  const {
    data: collaboratorProjects,
    error: _collaboratorError,
    status: _collaboratorStatus,
  } = useBackend(
    ["/api/projects/list/collaborator"],
    // Stryker disable next-line StringLiteral : The default value for an empty ("") method is GET.
    { method: "GET", url: "/api/projects/list/collaborator" },
    // Stryker disable next-line all : don't test default value of empty list
    [],
  );
 
  const [viewModal, setViewModal] = useState(false);
 
  const objectToAxiosParams = (project) => ({
    url: "/api/projects/post",
    method: "POST",
    params: {
      name: project.name,
      description: project.description,
    },
  });
 
  const onSuccess = (project) => {
    toast(`Project ${project.name} created`);
    setViewModal(false);
  };
 
  const mutation = useBackendMutation(objectToAxiosParams, { onSuccess }, [
    "/api/projects/list/owner",
  ]);
 
  const onSubmit = async (data) => {
    mutation.mutate(data);
  };
 
  const createProject = () => setViewModal(true);
 
  return (
    <BasicLayout>
      <div className="pt-2">
        {isResearcher && (
          <>
            <ProjectModal
              showModal={viewModal}
              toggleShowModal={setViewModal}
              onSubmitAction={onSubmit}
            />
            <Button
              onClick={createProject}
              style={{ float: "right", marginBottom: 10 }}
              variant="primary"
            >
              Create Project
            </Button>
            <h1>Your Projects</h1>
            {ownedProjects.length === 0 && (
              <p data-testid="HomePageLoggedIn-no-owned-projects">
                No projects yet. Click the button above to create one.
              </p>
            )}
            {ownedProjects.length > 0 && (
              <ProjectTable
                projects={ownedProjects}
                currentUser={currentUser}
                testId={"OwnerProjectTable"}
              />
            )}
          </>
        )}
        <h1>Projects You Collaborate On</h1>
        {collaboratorProjects.length === 0 && (
          <p data-testid="HomePageLoggedIn-no-collaborator-projects">
            You are not a collaborator on any projects yet.
          </p>
        )}
        {collaboratorProjects.length > 0 && (
          <ProjectTable
            projects={collaboratorProjects}
            currentUser={currentUser}
            testId={"CollaboratorProjectTable"}
          />
        )}
      </div>
    </BasicLayout>
  );
}