All files / pages/Onboarding OnboardingPreviewPage.jsx

100% Statements 38/38
100% Branches 18/18
100% Functions 13/13
100% Lines 37/37

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181                      2x               2x     58x 58x 58x 58x 58x 58x       58x                 58x 2x                       56x 19x                 37x     58x         58x                       58x 3x 3x 3x   3x     58x 37x   2x   1x       14x                           4x 2x 2x         3x   18x   1x           58x 76x     58x                     148x     11x                                                              
import { Fragment, useState } from "react";
import { Link, useParams } from "react-router";
import { Alert, Button, ButtonGroup, ProgressBar, Row } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { useBackend } from "main/utils/useBackend";
import { hasRole, useCurrentUser } from "main/utils/currentUser";
import OnboardingSelectSchoolComponent from "main/components/Onboarding/OnboardingSelectSchoolComponent";
import OnboardingGithubSignInComponent from "main/components/Onboarding/OnboardingGithubSignInComponent";
import OnboardingSelectCoursesComponent from "main/components/Onboarding/OnboardingSelectCoursesComponent";
import OnboardingCompleteComponent from "main/components/Onboarding/OnboardingCompleteComponent";
 
const previewStages = [
  { key: "school", label: "School sign in", progress: 25 },
  { key: "github", label: "GitHub sign in", progress: 50 },
  { key: "courses", label: "Join courses", progress: 75 },
  { key: "complete", label: "Complete", progress: 100 },
];
 
// Stryker disable next-line all : returning home is ignored on purpose so the preview stays open
const doNothing = () => {};
 
export default function OnboardingPreviewPage() {
  const currentUser = useCurrentUser();
  const { courseId } = useParams();
  const [stage, setStage] = useState("school");
  const [joined, setJoined] = useState(false);
  const [inviteViewed, setInviteViewed] = useState(false);
  const [resetCount, setResetCount] = useState(0);
 
  // The backend only returns a course to admins, its instructor and its staff,
  // so a failed request means this viewer may not see the preview.
  const { data: course, failureCount } = useBackend(
    // Stryker disable next-line all : the query key only affects caching
    [`/api/courses/${courseId}`],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: `/api/courses/${courseId}` },
    null,
    true,
  );
 
  if (failureCount > 0) {
    return (
      <BasicLayout>
        <h2 data-testid="OnboardingPreviewPage-denied">
          You do not have access to preview onboarding for this course.
        </h2>
        <Button as={Link} to="/">
          Return
        </Button>
      </BasicLayout>
    );
  }
 
  if (!course) {
    return (
      <BasicLayout>
        <p data-testid="OnboardingPreviewPage-loading">Loading...</p>
      </BasicLayout>
    );
  }
 
  // Stryker disable OptionalChaining : currentUser.root is null until the user loads
  const canSeeInstructorView =
    hasRole(currentUser, "ROLE_ADMIN") ||
    currentUser?.root?.user?.email === course.instructorEmail;
  // Stryker restore OptionalChaining
  const returnPath = `/${canSeeInstructorView ? "instructor" : "staff"}/courses/${courseId}`;
 
  // One sample row based on this course. Joining it only flips the in-memory
  // joined flag, which is the same JOINCOURSE to INVITED change a real student
  // sees after joining. Nothing is sent to the backend.
  const sampleCourses = [
    {
      id: course.id,
      rosterStudentId: 0,
      courseName: course.courseName,
      term: course.term,
      school: course.school,
      orgName: course.orgName,
      studentStatus: joined ? "INVITED" : "JOINCOURSE",
    },
  ];
 
  const resetPreview = () => {
    setStage("school");
    setJoined(false);
    setInviteViewed(false);
    // Stryker disable next-line all : any new key remounts the screens, which clears the school picker
    setResetCount((count) => count + 1);
  };
 
  const renderStage = () => {
    switch (stage) {
      case "github":
        return (
          <OnboardingGithubSignInComponent
            onSignIn={() => setStage("courses")}
          />
        );
      case "courses":
        return (
          <>
            {inviteViewed && (
              <Alert
                variant="secondary"
                data-testid="OnboardingPreviewPage-invite-notice"
              >
                In the real flow, View Invite opens the GitHub invitation for
                the {course.orgName} organization in a new tab. The preview does
                not open it, since no invitation was sent.
              </Alert>
            )}
            <OnboardingSelectCoursesComponent
              courses={sampleCourses}
              onJoinCourse={() => setJoined(true)}
              onViewInvite={() => setInviteViewed(true)}
              onContinue={() => setStage("complete")}
            />
          </>
        );
      case "complete":
        return <OnboardingCompleteComponent onReturnHome={doNothing} />;
      default:
        return (
          <OnboardingSelectSchoolComponent
            onSignIn={() => setStage("github")}
          />
        );
    }
  };
 
  const currentStage = previewStages.find(
    (stageInfo) => stageInfo.key === stage,
  );
 
  return (
    <BasicLayout enableBootstrap={true}>
      <Alert variant="info" data-testid="OnboardingPreviewPage-banner">
        <strong>Student onboarding preview</strong> for {course.courseName} (
        {course.term}). Click through it like a student would, or jump to a
        stage with the buttons below. Nothing here signs anyone in, joins a
        course, or changes any account.
      </Alert>
      <div className="d-flex flex-wrap align-items-center gap-2 mb-3">
        <ButtonGroup aria-label="Onboarding stage">
          {previewStages.map((stageInfo) => (
            <Button
              key={stageInfo.key}
              variant={stageInfo.key === stage ? "primary" : "outline-primary"}
              onClick={() => setStage(stageInfo.key)}
              data-testid={`OnboardingPreviewPage-stage-${stageInfo.key}`}
            >
              {stageInfo.label}
            </Button>
          ))}
        </ButtonGroup>
        <Button
          variant="outline-secondary"
          onClick={resetPreview}
          data-testid="OnboardingPreviewPage-reset"
        >
          Reset preview
        </Button>
        <Button
          as={Link}
          to={returnPath}
          variant="secondary"
          className="ms-auto"
          data-testid="OnboardingPreviewPage-return"
        >
          Return to course
        </Button>
      </div>
      <Fragment key={resetCount}>{renderStage()}</Fragment>
      <Row className="mt-auto pb-3">
        <ProgressBar now={currentStage.progress} />
      </Row>
    </BasicLayout>
  );
}