All files / components/Emails CourseEmailsCard.jsx

100% Statements 13/13
100% Branches 4/4
100% Functions 6/6
100% Lines 13/13

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                                          523x 523x   523x                       523x 3x 3x 2x   1x       523x                     4x       702x                     6x       1046x                                                                  
import React, { useState } from "react";
import { Accordion, Button, Form } from "react-bootstrap";
import { toast } from "react-toastify";
import { useBackend } from "main/utils/useBackend";
import {
  EMAIL_FORMATS,
  EMAIL_TYPES,
  courseEmailsQueryKey,
} from "main/utils/courseEmailsUtils";
 
/**
 * 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 CourseEmailsCard({
  courseId,
  type,
  showTypeDropdown = false,
  testIdPrefix = "CourseEmailsCard",
}) {
  const [selectedType, setSelectedType] = useState(type);
  const [format, setFormat] = useState(EMAIL_FORMATS[0].value);
 
  const { data: emails } = useBackend(
    [courseEmailsQueryKey(courseId), selectedType, format],
    {
      // Stryker disable next-line StringLiteral : GET and empty string are equivalent
      method: "GET",
      url: "/api/courses/emails",
      params: { courseId, type: selectedType, format },
    },
    "",
    true,
  );
 
  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>
            {showTypeDropdown && (
              <Form.Group className="mb-3" controlId={`${testIdPrefix}-type`}>
                <Form.Label>Type</Form.Label>
                <Form.Select
                  value={selectedType}
                  onChange={(e) => setSelectedType(e.target.value)}
                  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}-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>
  );
}