All files / components/TabComponent SettingsTabComponent.jsx

100% Statements 20/20
100% Branches 7/7
100% Functions 8/8
100% Lines 18/18

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                            204x 204x   204x 1x     204x                   204x               204x 1x       204x             204x 2x 2x         204x 3x         204x                 204x 5x     204x                                          
import React from "react";
import CanvasCourseSettings from "main/components/Settings/CanvasCourseSettings";
import CourseOptionsForm from "main/components/Settings/CourseOptionsForm";
import SlackCourseSettings from "main/components/Settings/SlackCourseSettings";
import { useBackendMutation } from "main/utils/useBackend";
import { useCourseOptions } from "main/utils/courseOptionsUtils";
import { toast } from "react-toastify";
 
export default function SettingsTabComponent({
  courseId,
  testIdPrefix,
  canEditCourseOptions,
}) {
  // Shares its query (and cache) with CourseOptionsForm, so toggling an option there updates this
  const { data: optionsMap = {} } = useCourseOptions(courseId);
  const [slackErrorMessage, setSlackErrorMessage] = React.useState();
 
  const onSuccessCanvasCredentialsAdded = () => {
    toast("Canvas credentials successfully added.");
  };
 
  const objectToAxiosParamsCanvasToken = (formData) => ({
    url: `/api/courses/updateCourseCanvasToken`,
    method: "PUT",
    params: {
      courseId: courseId,
      canvasCourseId: formData.canvasCourseId,
      canvasApiToken: formData.canvasApiToken,
    },
  });
 
  const canvasMutation = useBackendMutation(
    objectToAxiosParamsCanvasToken,
    {
      onSuccess: onSuccessCanvasCredentialsAdded,
    },
    [`/api/courses/getCanvasInfo?courseId=${courseId}`],
  );
 
  const handleSubmit = (formData) => {
    canvasMutation.mutate(formData);
  };
 
  // The token is sent in the body (not the query string) so that it stays out of access logs.
  const objectToAxiosParamsSlackToken = (formData) => ({
    url: `/api/courses/slack/token`,
    method: "POST",
    params: { courseId: courseId },
    data: new URLSearchParams({ slackBotToken: formData.slackBotToken }),
  });
 
  const onSuccessSlackTokenSaved = (data) => {
    setSlackErrorMessage(undefined);
    toast(
      `Slack token verified and saved for workspace ${data.slackTeamName} (${data.slackTeamId}).`,
    );
  };
 
  const onErrorSlackToken = (error) => {
    setSlackErrorMessage(
      error.response?.data?.message ?? `Error saving Slack token: ${error}`,
    );
  };
 
  const slackMutation = useBackendMutation(
    objectToAxiosParamsSlackToken,
    {
      onSuccess: onSuccessSlackTokenSaved,
      onError: onErrorSlackToken,
    },
    [`/api/courses/slack/info?courseId=${courseId}`],
  );
 
  const handleSlackSubmit = (formData) => {
    slackMutation.mutate(formData);
  };
 
  return (
    <div>
      <CourseOptionsForm courseId={courseId} canEdit={canEditCourseOptions} />
      {optionsMap.ENABLE_CANVAS && (
        <CanvasCourseSettings
          courseId={courseId}
          submitAction={handleSubmit}
          testIdPrefix={testIdPrefix}
        />
      )}
      {optionsMap.SLACK_INTEGRATION && (
        <SlackCourseSettings
          courseId={courseId}
          submitAction={handleSlackSubmit}
          errorMessage={slackErrorMessage}
          testIdPrefix={testIdPrefix}
        />
      )}
    </div>
  );
}