All files / pages HomePage.js

100% Statements 18/18
100% Branches 2/2
100% Functions 5/5
100% Lines 15/15

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                        20x 20x       20x             20x               20x             20x   6x 3x         20x 20x     20x 20x 13x   48x     20x                            
import { useState, useEffect } from "react"
import { Container, Row, Col } from "react-bootstrap";
import { useNavigate } from "react-router-dom";
 
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import CommonsList from "main/components/Commons/CommonsList";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { useCurrentUser } from "main/utils/currentUser";
import Background from './../../assets/HomePageBackground.jpg';
 
export default function HomePage() {
  // Stryker disable next-line all
  const [commonsJoined, setCommonsJoined] = useState([]);
  const { data: currentUser } = useCurrentUser();
  // Stryker disable all 
 
  const { data: commons } =
    useBackend(
      ["/api/commons/all"],
      { url: "/api/commons/all" },
      []
    );
  // Stryker enable all 
 
  const objectToAxiosParams = (newCommonsId) => ({
    url: "/api/commons/join",
    method: "POST",
    params: {
      commonsId: newCommonsId
    }
  });
 
  const mutation = useBackendMutation(
    objectToAxiosParams,
    {},
    // Stryker disable next-line all : hard to set up test for caching
    ["/api/currentUser"]
  );
 
  useEffect(
    () => {
      if (currentUser?.root?.user?.commons) {
        setCommonsJoined(currentUser.root.user.commons);
      }
    }, [currentUser]
  );
 
  let navigate = useNavigate();
  const visitButtonClick = (id) => { navigate("/play/" + id) };
 
  //create a list of commons that the user hasn't joined for use in the "Join a New Commons" list.
  let joinedIdList = [];
  for (let commonJoined of commonsJoined) {
	joinedIdList.push(commonJoined.id)
  }
  let commonsNotJoined = commons.filter(f => !joinedIdList.includes(f.id));
 
 
  return (
    <div style={{ backgroundSize: 'cover', backgroundImage: `url(${Background})` }}>
      <BasicLayout>
        <h1 data-testid="homePage-title" style={{ fontSize: "75px", borderRadius: "7px", backgroundColor: "white", opacity: ".9" }} className="text-center border-0 my-3">Howdy Farmer</h1>
        <Container>
          <Row>
            <Col sm><CommonsList commonList={commonsJoined} title="Visit A Commons" buttonText={"Visit"} buttonLink={visitButtonClick} /></Col>
            <Col sm><CommonsList commonList={commonsNotJoined} title="Join A New Commons" buttonText={"Join"} buttonLink={mutation.mutate} /></Col>
          </Row>
        </Container>
      </BasicLayout>
    </div>
  )
}