All files / components/Sections SectionTranslator.jsx

100% Statements 9/9
100% Branches 6/6
100% Functions 4/4
100% Lines 8/8

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                              10x   10x                 19x 10x   10x 2x     10x                                               21x                    
import { Form } from "react-bootstrap";
import { useBackend } from "main/utils/useBackend";
 
/**
 * Shows the translation (label) of a raw section value, along with a
 * dropdown that lets the user pick one of the course's known sections.
 * Selecting a section from the dropdown updates the raw section value
 * (via onChange) and, in turn, the translation shown.
 */
function SectionTranslator({
  courseId,
  section,
  onChange,
  testIdPrefix = "SectionTranslator",
}) {
  const sectionsQueryKey = `/api/courses/${courseId}/sections`;
 
  const { data: sections = [] } = useBackend(
    [sectionsQueryKey],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: sectionsQueryKey },
    [],
    true,
    { enabled: Boolean(courseId) },
  );
 
  const matchingSection = sections.find((s) => s.section === section);
  const translation = matchingSection ? matchingSection.label : "";
 
  const handleSelectChange = (event) => {
    onChange(event.target.value);
  };
 
  return (
    <Form.Group className="mb-3">
      <Form.Label htmlFor={`${testIdPrefix}-translation`}>
        Section Translation
      </Form.Label>
      <Form.Control
        data-testid={`${testIdPrefix}-translation`}
        id={`${testIdPrefix}-translation`}
        type="text"
        value={translation}
        disabled
        readOnly
      />
      <Form.Label htmlFor={`${testIdPrefix}-select`} className="mt-2">
        Select Section
      </Form.Label>
      <Form.Select
        data-testid={`${testIdPrefix}-select`}
        id={`${testIdPrefix}-select`}
        value={section ?? ""}
        onChange={handleSelectChange}
      >
        <option value="">-- Select a Section --</option>
        {sections.map((s) => (
          <option key={s.id} value={s.section}>
            {s.section} - {s.label}
          </option>
        ))}
      </Form.Select>
    </Form.Group>
  );
}
 
export default SectionTranslator;