All files / components/Jobs InstructorReportSpecificGameForm.jsx

100% Statements 17/17
100% Branches 6/6
100% Functions 3/3
100% Lines 17/17

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              61x   61x           61x 61x         61x   61x 1x 1x     61x 2x 2x     61x 36x     25x 9x 9x     25x                                            
import { Button, Form } from "react-bootstrap";
import { useForm } from "react-hook-form";
import { useState } from "react";
import { useBackend } from "main/utils/useBackend";
import GameSelect from "main/components/Game/GameSelect";
 
function InstructorReportSpecificGameForm({ submitAction }) {
  const testid = "InstructorReportSpecificGameForm";
 
  const { data: game } = useBackend(
    ["/api/game/all"],
    { url: "/api/game/all" },
    [],
  );
 
  const [selectedGame, setSelectedGame] = useState(null);
  const [selectedGameName, setSelectedGameName] = useState(null);
 
  const {
    handleSubmit,
    formState: { _errors },
  } = useForm();
 
  const handleGameSelection = (id, name) => {
    setSelectedGame(id);
    setSelectedGameName(name);
  };
 
  const onSubmit = () => {
    const params = { selectedGame, selectedGameName };
    submitAction(params);
  };
 
  if (!game || game.length === 0) {
    return <div>There are no games on which to run this job.</div>;
  }
 
  if (selectedGame === null) {
    setSelectedGame(game[0].id);
    setSelectedGameName(game[0].name);
  }
 
  return (
    <Form onSubmit={handleSubmit(onSubmit)}>
      <GameSelect
        game={game}
        selectedGame={selectedGame}
        handleGameSelection={handleGameSelection}
        testid={testid}
      />
      <p>
        Click this button to generate an instructor report for the selected
        game.
      </p>
      <Button
        type="submit"
        data-testid="InstructorReportSpecificGameForm-Submit-Button"
      >
        Generate
      </Button>
    </Form>
  );
}
export default InstructorReportSpecificGameForm;