All files / components/TabComponent SectionsTabComponent.jsx

100% Statements 18/18
100% Branches 6/6
100% Functions 6/6
100% Lines 17/17

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                        48x   48x   48x                     48x 48x   48x                 48x   48x                       48x 2x 2x     48x           48x 3x     48x       1x                                 5x                                      
import React, { useState } from "react";
import { Button, Col, Row } from "react-bootstrap";
import Modal from "react-bootstrap/Modal";
import { toast } from "react-toastify";
 
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import SectionsForm from "main/components/Sections/SectionsForm";
import SectionsTable from "main/components/Sections/SectionsTable";
import { onSectionMutationError } from "main/utils/sectionsUtils";
import { useCourseOptions } from "main/utils/courseOptionsUtils";
 
export default function SectionsTabComponent({ courseId, testIdPrefix }) {
  const [showCreateModal, setShowCreateModal] = useState(false);
 
  const sectionsQueryKey = `/api/courses/${courseId}/sections`;
 
  const { data: sections } = useBackend(
    [sectionsQueryKey],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: sectionsQueryKey },
    [],
    true,
  );
 
  // The Slack Channel Name field is only shown when the SLACK_INTEGRATION
  // course option is enabled and the course has an active Slack token
  // (i.e. it has connected to a Slack workspace).
  const { data: optionsMap = {} } = useCourseOptions(courseId);
  const slackInfoQueryKey = `/api/courses/slack/info?courseId=${courseId}`;
  // Stryker disable all : hard to test query caching/toast-suppression behavior
  const { data: slackInfo = {} } = useBackend(
    [slackInfoQueryKey],
    { method: "GET", url: slackInfoQueryKey },
    {},
    true,
    { enabled: optionsMap.SLACK_INTEGRATION === true },
  );
  // Stryker restore all
  const showSlackChannel =
    optionsMap.SLACK_INTEGRATION === true && Boolean(slackInfo.slackTeamId);
 
  const objectToAxiosParamsPost = (section) => ({
    url: sectionsQueryKey,
    method: "POST",
    params: {
      section: section.section,
      label: section.label,
      ...(showSlackChannel && {
        slackChannelName: section.slackChannelName,
      }),
    },
  });
 
  const onCreateSuccess = () => {
    toast("Section successfully created.");
    setShowCreateModal(false);
  };
 
  const createMutation = useBackendMutation(
    objectToAxiosParamsPost,
    { onSuccess: onCreateSuccess, onError: onSectionMutationError },
    [sectionsQueryKey],
  );
 
  const handleCreateSubmit = (section) => {
    createMutation.mutate(section);
  };
 
  return (
    <div data-testid={`${testIdPrefix}-sections-tab-component`}>
      <Modal
        show={showCreateModal}
        onHide={() => setShowCreateModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-create-section-modal`}
      >
        <Modal.Header closeButton>
          <Modal.Title>Create Section</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <SectionsForm
            submitAction={handleCreateSubmit}
            showSlackChannel={showSlackChannel}
          />
        </Modal.Body>
      </Modal>
      <Row sm={4} className="p-2 g-3">
        <Col>
          <Button
            onClick={() => setShowCreateModal(true)}
            data-testid={`${testIdPrefix}-create-section-button`}
            className="w-100"
          >
            Create Section
          </Button>
        </Col>
      </Row>
      <Row>
        <SectionsTable
          sections={sections}
          courseId={courseId}
          testIdPrefix={`${testIdPrefix}-sections-table`}
          showSlackChannel={showSlackChannel}
        />
      </Row>
    </div>
  );
}