All files / pages HomePage.jsx

100% Statements 19/19
100% Branches 7/7
100% Functions 4/4
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                    41x 41x     41x           41x             41x   41x                 41x           41x 15x 14x                     41x 41x 41x       41x 41x 1x       41x             41x                                                                            
import { useState, useEffect } from "react";
import { Card, Container, Row, Col } from "react-bootstrap";
import { useNavigate } from "react-router";
 
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import GameList from "main/components/Game/GameList";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { useCurrentUser, hasRole } from "main/utils/currentUser";
import {
  filterGameJoinedAndNotHidden,
  filterGameNotJoinedAndNotHidden,
} from "main/utils/gameUtils";
import getBackgroundImage from "main/components/Utils/HomePageBackground";
 
import "./HomePage.css";
 
export default function HomePage({ hour = null }) {
  // Stryker disable next-line all: it is acceptable to exclude useState calls from mutation testing
  const [gameJoined, setGameJoined] = useState([]);
  const { data: currentUser } = useCurrentUser();
 
  // Stryker disable all : it is acceptable to exclude useBackend calls from mutation testing
  const { data: game } = useBackend(
    ["/api/game/all"],
    { url: "/api/game/all" },
    [],
  );
 
  const { data: myCourseIds } = useBackend(
    ["/api/game/mycourses"],
    { url: "/api/game/mycourses" },
    [],
  );
  // Stryker restore all
 
  const isAdmin = hasRole(currentUser, "ROLE_ADMIN");
 
  const objectToAxiosParams = (newGameId) => ({
    url: "/api/game/join",
    method: "POST",
    params: {
      gameId: newGameId,
    },
  });
 
  // Stryker disable all : it is acceptable to exclude useBackendMutation calls from mutation testing
  const mutation = useBackendMutation(objectToAxiosParams, {}, [
    "/api/currentUser",
  ]);
  // Stryker restore all
 
  // Stryker disable all : TODO: restructure this code to avoid the need for this disable
  useEffect(() => {
    if (currentUser?.root?.user?.game) {
      setGameJoined(
        filterGameJoinedAndNotHidden(
          game,
          currentUser.root.user.game,
          myCourseIds,
          isAdmin,
        ),
      );
    }
  }, [game, currentUser, myCourseIds, isAdmin]);
 
  const firstName = currentUser?.root?.user?.givenName || "";
  const time = hour === null ? new Date().getHours() : hour;
  const Background = getBackgroundImage(time);
 
  // Stryker restore all
 
  let navigate = useNavigate();
  const visitButtonClick = (id) => {
    navigate("/play/" + id);
  };
 
  //create a list of game that the user hasn't joined for use in the "Join A New Game" list.
  const gameNotJoinedList = filterGameNotJoinedAndNotHidden(
    game,
    gameJoined,
    myCourseIds,
    isAdmin,
  );
 
  return (
    <div
      data-testid={"HomePage-main-div"}
      style={{ backgroundSize: "cover", backgroundImage: `url(${Background})` }}
    >
      <BasicLayout>
        <Card data-testid={"HomePage-intro-card"} className="woodentitle">
          <div className="text-center border-0 my-3">
            <h1 data-testid="homePage-title" className="animate-charcter">
              Howdy Farmer {firstName}
            </h1>
          </div>
        </Card>
        <Container>
          <Row>
            <Col sm>
              <GameList
                commonList={gameNotJoinedList}
                title="Join A New Game"
                buttonText={"Join"}
                buttonLink={mutation.mutate}
              />
            </Col>
 
            <Col sm>
              <GameList
                commonList={gameJoined}
                title="Visit A Game"
                buttonText={"Visit"}
                buttonLink={visitButtonClick}
              />
            </Col>
          </Row>
        </Container>
      </BasicLayout>
    </div>
  );
}