All files / components/Jobs DeleteEmptyRepoForm.jsx

100% Statements 16/16
100% Branches 6/6
100% Functions 6/6
100% Lines 15/15

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          2x 43x       43x                   43x       2x 2x     3x                 43x 6x 6x 1x 1x   5x     43x                     5x                                                          
import React, { useState } from "react";
import { Form, Button, Row, Col } from "react-bootstrap";
import { useBackendMutation } from "main/utils/useBackend"; // Adjust path if needed
import { toast } from "react-toastify";
 
const DeleteEmptyReposForm = ({ courseId }) => {
  const [prefix, setPrefix] = useState("");
 
  // Configure the Axios request for the DELETE endpoint
  // Configure the Axios request for the DELETE endpoint
  const objectToAxiosParams = () => ({
    url: "/api/repos",
    method: "DELETE",
    params: {
      courseId,
      prefix,
    },
  });
 
  // Setup the mutation using your team's standard hook
  const deleteMutation = useBackendMutation(
    objectToAxiosParams,
    {
      onSuccess: () => {
        toast(`Delete empty repos job launched for prefix: ${prefix}`);
        setPrefix(""); // Clear the input after launching
      },
      onError: (error) => {
        toast.error(
          `Error starting job: ${error.response?.data?.message || error.message}`,
        );
      },
    },
    // We don't necessarily need to invalidate a cache key here unless you display jobs on the same page
    ["/api/repos"],
  );
 
  const onSubmit = (event) => {
    event.preventDefault();
    if (!prefix) {
      toast.error("Please enter a prefix");
      return;
    }
    deleteMutation.mutate();
  };
 
  return (
    <Form onSubmit={onSubmit} className="p-3 border rounded bg-light mb-4">
      <h5>Delete Empty Repositories</h5>
      <Form.Group className="mb-3" controlId="prefixInput">
        <Form.Label>Repository Prefix</Form.Label>
        <Row>
          <Col sm={8} md={6}>
            <Form.Control
              type="text"
              placeholder="e.g., lab01"
              value={prefix}
              onChange={(e) => setPrefix(e.target.value)}
              data-testid="DeleteEmptyReposForm-prefix"
            />
          </Col>
          <Col sm={4} md={6}>
            <Button
              variant="danger"
              type="submit"
              data-testid="DeleteEmptyReposForm-submit"
              disabled={deleteMutation.isPending}
            >
              {deleteMutation.isPending
                ? "Launching..."
                : "Delete Empty Matching Repos"}
            </Button>
          </Col>
        </Row>
        <Form.Text className="text-muted text-danger mt-2 d-block">
          <strong>Warning:</strong> This will kick off a background job to
          delete all repositories in the organization that have names starting
          with this prefix <strong>and have no commits</strong>. Repositories
          with commits will be kept safe.
        </Form.Text>
      </Form.Group>
    </Form>
  );
};
 
export default DeleteEmptyReposForm;