All files / components/Courses CourseWarningBanner.jsx

100% Statements 14/14
100% Branches 16/16
100% Functions 4/4
100% Lines 13/13

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127              3x               252x                                     252x       252x               252x   252x         252x 1x 1x     252x             252x       252x                                                                                             2x                    
import { useState } from "react";
import { Alert, Button } from "react-bootstrap";
import { toast } from "react-toastify";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
 
/** Where verified educators can upgrade a GitHub organization at no cost. */
export const GITHUB_EDUCATION_TEACHER_URL =
  "https://education.github.com/globalcampus/teacher";
 
export function CourseWarningBanner({
  courseId,
  orgName,
  hideBasePermissionWarning = false,
  hideFreePlanWarning = false,
}) {
  const { data: warnings } = useBackend(
    [`/api/courses/warnings/${courseId}`],
    {
      method: "GET",
      url: `/api/courses/warnings/${courseId}`,
    },
    undefined,
    true,
    {
      placeholderData: {
        showOrganizationAgeWarning: false,
        showDefaultBasePermissions: false,
        showFreePlanWarning: false,
      },
      staleTime: "static",
    },
  );
 
  const showDefaultBasePermissionWarning =
    warnings?.showDefaultBasePermissions &&
    orgName &&
    !hideBasePermissionWarning;
 
  const memberPrivilegesUrl = orgName
    ? `https://github.com/organizations/${orgName}/settings/member_privileges`
    : null;
 
  // Dismissing the free plan warning is remembered on the course (so it stays
  // dismissed on reload); this local flag hides it immediately, without waiting
  // for the course to be refetched.
  const [freePlanWarningDismissed, setFreePlanWarningDismissed] =
    useState(false);
 
  const objectToAxiosParamsHideFreePlanWarning = () => ({
    url: `/api/courses/warnings/hideFreePlanWarning/${courseId}`,
    method: "POST",
  });
 
  const onSuccessHideFreePlanWarning = () => {
    setFreePlanWarningDismissed(true);
    toast("Free plan warning dismissed for this course");
  };
 
  const hideFreePlanWarningMutation = useBackendMutation(
    objectToAxiosParamsHideFreePlanWarning,
    { onSuccess: onSuccessHideFreePlanWarning },
    [`/api/courses/${courseId}`],
  );
 
  const showFreePlanWarning =
    warnings?.showFreePlanWarning &&
    !hideFreePlanWarning &&
    !freePlanWarningDismissed;
 
  return (
    <>
      {warnings?.showOrganizationAgeWarning && (
        <Alert variant="warning">
          Warning: This GitHub Organization is less than 30 days old. You will
          experience difficulties enrolling more than 50 students in a day.
        </Alert>
      )}
      {showDefaultBasePermissionWarning && (
        <Alert
          variant="warning"
          data-testid="CourseWarningBanner-defaultBasePermission"
        >
          Warning: the organization setting for Default Base Permission is not
          the recommended value of None. This means that students in the
          organization may be able to access other students&apos; private repos.{" "}
          <a
            href={memberPrivilegesUrl}
            target="_blank"
            rel="noopener noreferrer"
            data-testid="CourseWarningBanner-defaultBasePermission-link"
          >
            You can change that setting here
          </a>
          .
        </Alert>
      )}
      {showFreePlanWarning && (
        <Alert variant="warning" data-testid="CourseWarningBanner-freePlan">
          Warning: this GitHub organization is on the Free plan. Some features
          do not work on the Free plan; in particular, the &quot;Require Signed
          Commits&quot; option for assignments cannot be applied to private
          repositories. Verified educators can upgrade the organization at no
          cost through{" "}
          <a
            href={GITHUB_EDUCATION_TEACHER_URL}
            target="_blank"
            rel="noopener noreferrer"
            data-testid="CourseWarningBanner-freePlan-link"
          >
            GitHub Education for Teachers
          </a>
          .
          <Button
            size="sm"
            variant="outline-secondary"
            className="ms-2"
            onClick={() => hideFreePlanWarningMutation.mutate()}
            data-testid="CourseWarningBanner-freePlan-dismiss"
          >
            Dismiss
          </Button>
        </Alert>
      )}
    </>
  );
}