All files / components/MyReviews ReviewForm.jsx

100% Statements 3/3
100% Branches 1/1
100% Functions 2/2
100% Lines 3/3

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                          42x               42x                                                                                                                                             1x                    
import { Button, Form, Row, Col } from "react-bootstrap";
import { useForm } from "react-hook-form";
import { useNavigate } from "react-router";
 
export default function ReviewForm({
  initialItemName,
  submitAction,
  buttonLabel = "Submit Review",
}) {
  const {
    register,
    formState: { errors },
    handleSubmit,
  } = useForm({
    defaultValues: {
      reviewerComments: "",
      itemsStars: 5,
      dateItemServed: new Date().toISOString().slice(0, 16), // default to now, in YYYY-MM-DDTHH:mm format (UTC)
    },
  });
 
  const navigate = useNavigate();
 
  return (
    <Form onSubmit={handleSubmit(submitAction)}>
      <Form.Group className="mb-3">
        <Form.Label htmlFor="review-item-name">Item Name</Form.Label>
        <Form.Control
          data-testid="ReviewForm-review-item-name"
          id="review-item-name"
          type="text"
          value={initialItemName}
          disabled
        />
      </Form.Group>
 
      <Form.Group className="mb-3">
        <Form.Label htmlFor="review-comments">Comments</Form.Label>
        <Form.Control
          data-testid="ReviewForm-review-comments"
          id="review-comments"
          as="textarea"
          rows={3}
          {...register("reviewerComments")} // not required
        />
      </Form.Group>
 
      <Row>
        <Col>
          <Form.Group className="mb-3">
            <Form.Label htmlFor="review-stars">Stars (1 to 5)</Form.Label>
            <Form.Select
              data-testid="ReviewForm-review-stars"
              id="review-stars"
              {...register("itemsStars", {
                // default+unemptiable, so required unneeded for frontend
                valueAsNumber: true,
                min: 1,
                max: 5,
              })}
            >
              {[1, 2, 3, 4, 5].map((num) => (
                <option key={num} value={num}>
                  {num}
                </option>
              ))}
            </Form.Select>
          </Form.Group>
        </Col>
        <Col>
          <Form.Group className="mb-3">
            <Form.Label htmlFor="review-date">
              Date and Time Item was Served
            </Form.Label>
            <Form.Control
              data-testid="ReviewForm-review-date"
              id="review-date"
              type="datetime-local" // HTML native handling of invalid input, so required unneeded for frontend. default already step="60" as desired
              isInvalid={Boolean(errors.dateItemServed)}
              {...register("dateItemServed")}
            />
          </Form.Group>
        </Col>
      </Row>
 
      <Row>
        <Col>
          <Button type="submit" data-testid="ReviewForm-submit">
            {buttonLabel}
          </Button>
          <Button
            variant="secondary"
            onClick={() => navigate(-1)}
            data-testid="ReviewForm-cancel"
          >
            Cancel
          </Button>
        </Col>
      </Row>
    </Form>
  );
}