All files / components/Slack SlackPrivateChannelsCard.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 68 69 70 71 72 73 74 75 76                  28x           28x 2x         28x 3x             28x           28x                                                         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 SlackPrivateChannelsCard({
  courseId,
  testIdPrefix = "SlackPrivateChannelsCard",
}) {
  const objectToAxiosParams = () => ({
    url: "/api/courses/slack/privateChannels",
    method: "POST",
    params: { courseId },
  });
 
  const onSuccess = (job) => {
    toast(
      `Job ${job.id} to create private Slack channels started; see the Jobs tab for its log.`,
    );
  };
 
  const onError = (error) => {
    toast(
      error.response?.data?.message ??
        `Error starting job to create private 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}-private-channels-card`}
    >
      <Accordion flush>
        <Accordion.Item eventKey="privateChannels">
          <Accordion.Header>
            Slack Private Channels for Each Student plus Staff
          </Accordion.Header>
          <Accordion.Body>
            <p data-testid={`${testIdPrefix}-private-channels-description`}>
              For each student on the roster who has an account in the Slack
              workspace, this creates a private Slack channel named{" "}
              <code>private-first-last</code>, and adds the student, the
              instructor and all of the course staff to it. The first and last
              name are taken from Slack when the student has entered them there,
              and otherwise from the roster; only the first word of the first
              name is used. A student&apos;s channel is recognized by its
              members, not by its name, so running this again does not create
              duplicates: it creates channels for new students, adds new staff
              members to the existing channels, and renames a channel if the
              name of its student has changed. It then <strong>removes</strong>{" "}
              from each of those existing channels everyone other than the
              student, the instructor, the course staff, and bots; so someone
              who has been deleted from the Staff tab is removed from the
              channels. What was done is logged on the Jobs tab.
            </p>
            <Button
              onClick={() => mutation.mutate()}
              data-testid={`${testIdPrefix}-private-channels-submit`}
            >
              Create Private Channels for Each Student plus Staff
            </Button>
          </Accordion.Body>
        </Accordion.Item>
      </Accordion>
    </div>
  );
}