All files / components/TabComponent DownloadsTabComponent.jsx

100% Statements 16/16
100% Branches 0/0
100% Functions 2/2
100% Lines 16/16

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          215x 4x 4x         2x 2x   2x 2x 2x 2x 2x 2x 2x   2x   2x 2x       215x                  
import axios from "axios";
import CourseDownloadsForm from "../Courses/CourseDownloadsForm";
import { toast } from "react-toastify";
 
export default function DownloadsTabComponent({ courseId, testIdPrefix }) {
  const handleSubmit = async () => {
    try {
      const response = await axios.get("/api/csv/formattedteams", {
        params: { courseId },
        responseType: "blob",
      });
 
      const blob = new Blob([response.data], { type: "text/csv" });
      const url = window.URL.createObjectURL(blob);
 
      const link = document.createElement("a");
      link.href = url;
      link.download = `formatted_teams_${courseId}.csv`;
      document.body.appendChild(link);
      link.click();
      link.remove();
      window.URL.revokeObjectURL(url);
 
      toast("Download successfully initiated.");
    } catch (error) {
      console.error("CSV download error:", error);
      toast.error("Failed to download the CSV. Please try again.");
    }
  };
 
  return (
    <div data-testid={`${testIdPrefix}-downloadsTab`}>
      <CourseDownloadsForm
        downloadAction={handleSubmit}
        testIdPrefix={testIdPrefix}
      />
    </div>
  );
}