All files / components/Emails CategoricalCourseEmailsCard.jsx

100% Statements 20/20
100% Branches 5/5
100% Functions 6/6
100% Lines 20/20

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                        44x 44x 44x   44x                   44x   44x                       44x 6x 6x 1x 1x       44x 3x 3x 2x   1x       44x                           132x                                       3x       88x                                                                  
import React, { useState } from "react";
import { Accordion, Button, Form } from "react-bootstrap";
import { toast } from "react-toastify";
import { useBackend } from "main/utils/useBackend";
import TeamDropdown from "main/components/Teams/TeamDropdown";
import {
  EMAIL_FORMATS,
  EMAIL_TYPES,
  categoricalCourseEmailsQueryKey,
} from "main/utils/categoricalCourseEmailsUtils";
 
/**
 * A card, open by default, that shows the email addresses of the people of
 * the given type (STUDENTS, STAFF or ALL) in a course, in a read only text
 * area with a button to copy them.
 */
export default function CategoricalCourseEmailsCard({
  courseId,
  testIdPrefix = "CategoricalCourseEmailsCard",
}) {
  const [type, setType] = useState(EMAIL_TYPES[0].value);
  const [team, setTeam] = useState("");
  const [format, setFormat] = useState(EMAIL_FORMATS[0].value);
 
  const { data: teams } = useBackend(
    [`/api/teams/all?courseId=${courseId}`],
    {
      // Stryker disable next-line StringLiteral : GET and empty string are equivalent
      method: "GET",
      url: `/api/teams/all?courseId=${courseId}`,
    },
    [],
    true,
  );
  const teamOptions = teams ?? [];
 
  const { data: emails } = useBackend(
    [categoricalCourseEmailsQueryKey(courseId), type, team, format],
    {
      // Stryker disable next-line StringLiteral : GET and empty string are equivalent
      method: "GET",
      url: "/api/courses/emails",
      params: { courseId, type, team, format },
    },
    "",
    true,
  );
 
  const handleTypeChange = (event) => {
    setType(event.target.value);
    if (team !== "") {
      setTeam("");
      toast("Team is only available for students.");
    }
  };
 
  const copyEmails = async () => {
    try {
      await navigator.clipboard.writeText(emails);
      toast("Emails copied to the clipboard.");
    } catch {
      toast("Unable to copy the emails to the clipboard.");
    }
  };
 
  return (
    <div className="card mt-4" data-testid={`${testIdPrefix}-card`}>
      <Accordion flush defaultActiveKey="emails">
        <Accordion.Item eventKey="emails">
          <Accordion.Header>Emails</Accordion.Header>
          <Accordion.Body>
            <Form.Group className="mb-3" controlId={`${testIdPrefix}-type`}>
              <Form.Label>Type</Form.Label>
              <Form.Select
                value={type}
                onChange={handleTypeChange}
                data-testid={`${testIdPrefix}-type`}
              >
                {EMAIL_TYPES.map((f) => (
                  <option key={f.value} value={f.value}>
                    {f.label}
                  </option>
                ))}
              </Form.Select>
            </Form.Group>
            <Form.Group className="mb-3" controlId={`${testIdPrefix}-team`}>
              <Form.Label>Team</Form.Label>
              <TeamDropdown
                teams={teamOptions}
                value={team}
                onChange={setTeam}
                disabled={type !== "STUDENTS"}
                testId={`${testIdPrefix}-team`}
              />
            </Form.Group>
            <Form.Group className="mb-3" controlId={`${testIdPrefix}-format`}>
              <Form.Label>Format</Form.Label>
              <Form.Select
                value={format}
                onChange={(e) => setFormat(e.target.value)}
                data-testid={`${testIdPrefix}-format`}
              >
                {EMAIL_FORMATS.map((f) => (
                  <option key={f.value} value={f.value}>
                    {f.label}
                  </option>
                ))}
              </Form.Select>
            </Form.Group>
            <Form.Control
              as="textarea"
              rows={8}
              readOnly
              value={emails}
              aria-label="Emails"
              data-testid={`${testIdPrefix}-emails`}
              style={{
                fontFamily: "monospace",
                backgroundColor: "black",
                color: "white",
              }}
            />
            <Button
              className="mt-3"
              onClick={copyEmails}
              disabled={emails === ""}
              data-testid={`${testIdPrefix}-copy`}
            >
              Copy
            </Button>
          </Accordion.Body>
        </Accordion.Item>
      </Accordion>
    </div>
  );
}