All files / components/Courses CourseWarningBanner.jsx

100% Statements 19/19
100% Branches 17/17
100% Functions 7/7
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 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158              3x               274x                                             274x   274x 5x           4x             274x         274x               274x   274x         274x 1x 1x     274x             274x       274x                                                           5x                                                     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",
    },
  );
 
  // Keep the immediate dismissal scoped to the course, including when the
  // user navigates to another course before a request finishes.
  const [
    basePermissionWarningDismissedFor,
    setBasePermissionWarningDismissedFor,
  ] = useState(null);
 
  const hideBasePermissionWarningMutation = useBackendMutation(
    (id) => ({
      url: `/api/courses/warnings/hideBasePermissionWarning/${id}`,
      method: "POST",
    }),
    {
      onSuccess: (_data, id) => {
        setBasePermissionWarningDismissedFor(id);
      },
    },
    [`/api/courses/${courseId}`],
  );
 
  const showDefaultBasePermissionWarning =
    warnings?.showDefaultBasePermissions &&
    orgName &&
    !hideBasePermissionWarning &&
    basePermissionWarningDismissedFor !== courseId;
 
  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>
          .
          <Button
            size="sm"
            variant="outline-secondary"
            className="ms-2"
            disabled={hideBasePermissionWarningMutation.isPending}
            onClick={() => hideBasePermissionWarningMutation.mutate(courseId)}
            data-testid="CourseWarningBanner-defaultBasePermission-dismiss"
          >
            Dismiss
          </Button>
        </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>
      )}
    </>
  );
}