All files / components/Slack SlackSectionChannelsCard.jsx

100% Statements 9/9
100% Branches 3/3
100% Functions 5/5
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                  14x           14x 2x         14x 3x             14x           14x                                       5x                      
import React from "react";
import { Accordion, Button } from "react-bootstrap";
import { toast } from "react-toastify";
import { useBackendMutation } from "main/utils/useBackend";
 
export default function SlackSectionChannelsCard({
  courseId,
  testIdPrefix = "SlackSectionChannelsCard",
}) {
  const objectToAxiosParams = () => ({
    url: "/api/courses/slack/sectionChannels",
    method: "POST",
    params: { courseId },
  });
 
  const onSuccess = (job) => {
    toast(
      `Job ${job.id} to set up section Slack channels started; see the Jobs tab for its log.`,
    );
  };
 
  const onError = (error) => {
    toast(
      error.response?.data?.message ??
        `Error starting job to set up section Slack channels: ${error}`,
    );
  };
 
  // The job appears on the Jobs tab, so that tab's list of jobs is refreshed
  const mutation = useBackendMutation(
    objectToAxiosParams,
    { onSuccess, onError },
    ["/api/jobs/course"],
  );
 
  return (
    <div
      className="card mt-4"
      data-testid={`${testIdPrefix}-section-channels-card`}
    >
      <Accordion flush>
        <Accordion.Item eventKey="sectionChannels">
          <Accordion.Header>Slack Section Channels</Accordion.Header>
          <Accordion.Body>
            <p data-testid={`${testIdPrefix}-section-channels-description`}>
              For each section on the Sections tab that has a Slack channel
              name, this creates a public Slack channel with that name (unless
              it already exists), and adds the roster students of that section
              who have an account in the Slack workspace. It then{" "}
              <strong>removes</strong> from each of those channels everyone who
              is not a roster student of that section, other than the course
              staff, the instructor, and bots. What was done is logged on the
              Jobs tab.
            </p>
            <Button
              onClick={() => mutation.mutate()}
              data-testid={`${testIdPrefix}-section-channels-submit`}
            >
              Set Up Section Slack Channels
            </Button>
          </Accordion.Body>
        </Accordion.Item>
      </Accordion>
    </div>
  );
}