All files / components/CATME CATMEAuditCard.jsx

100% Statements 23/23
100% Branches 16/16
100% Functions 3/3
100% Lines 22/22

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            52x 52x 52x 52x   52x 13x 13x 13x     52x 15x   15x   12x 12x 12x   12x 12x   12x 12x     5x   5x         10x       52x                                                                                                                                    
import React, { useState } from "react";
import axios from "axios";
import { Alert, Button, Card, Form, Spinner } from "react-bootstrap";
import CATMEAuditResults from "./CATMEAuditResults";
 
export default function CATMEAuditCard({ courseId }) {
  const [file, setFile] = useState(null);
  const [result, setResult] = useState(null);
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
 
  const handleFileChange = (event) => {
    setFile(event.target.files[0] || null);
    setResult(null);
    setError("");
  };
 
  const handleAudit = async (event) => {
    event.preventDefault();
 
    if (!file || loading) return;
 
    setLoading(true);
    setError("");
    setResult(null);
 
    const formData = new FormData();
    formData.append("file", file);
 
    try {
      const response = await axios.post("/api/catme/audit", formData, {
        params: { courseId },
      });
      setResult(response.data);
    } catch (err) {
      setError(
        err.response?.data?.message ||
          "Unable to audit the CSV file. Please check the file format and try again.",
      );
    } finally {
      setLoading(false);
    }
  };
 
  return (
    <Card data-testid="catme-audit-card">
      <Card.Body>
        <Card.Title>Audit CATME Teammaker CSV Download</Card.Title>
 
        <Card.Text>
          When using CATME Teammaker, this option will allow you to ensure that
          the roster of students in CATME is up-to-date with the course roster
          in Frontiers. It treats Frontiers as the &quot;source of truth&quot;
          and lets you know what students need to be added to CATME, removed
          from CATME, or have their information in CATME (name, section)
          updated.
        </Card.Text>
 
        <p>To use it:</p>
        <ol>
          <li>
            Navigate to the page <code>/faculty/tm_display_results</code> in
            CATME and click the <strong>Export to CSV</strong> button.
          </li>
          <li>Upload that file here.</li>
          <li>Read the results.</li>
        </ol>
 
        <Form onSubmit={handleAudit}>
          <Form.Group className="mb-3">
            <Form.Label>CATME Teammaker CSV File</Form.Label>
            <Form.Control
              type="file"
              accept=".csv,text/csv"
              onChange={handleFileChange}
              data-testid="catme-audit-file"
            />
          </Form.Group>
 
          <Button
            type="submit"
            disabled={!file || loading}
            data-testid="catme-audit-submit"
          >
            {loading ? (
              <>
                <Spinner size="sm" className="me-2" />
                Auditing...
              </>
            ) : (
              "Audit CSV"
            )}
          </Button>
        </Form>
 
        {error && (
          <Alert
            variant="danger"
            className="mt-3"
            data-testid="catme-audit-error"
          >
            {error}
          </Alert>
        )}
 
        {result && <CATMEAuditResults result={result} />}
      </Card.Body>
    </Card>
  );
}