All files / components/Onboarding OnboardingSelectSchoolComponent.jsx

100% Statements 20/20
100% Branches 12/12
100% Functions 9/9
100% Lines 19/19

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                      23x 23x   23x 2x             23x 5x             23x 2x               23x 2x         23x 2x   23x   23x 9x   3x                                             6x                                                 23x                                     10x                      
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { Typeahead } from "react-bootstrap-typeahead";
import { schoolToProvider } from "main/utils/loginProviderSchools";
import React, { useState } from "react";
import { Row, Button, Col, ProgressBar } from "react-bootstrap";
import { useSystemInfo } from "main/utils/systemInfo";
import { FaMicrosoft } from "react-icons/fa";
import { FcGoogle } from "react-icons/fc";
import SignInCard from "main/components/Auth/SignInCard";
 
export default function OnboardingSelectSchoolComponent({ onSignIn }) {
  const [selectedProvider, setSelectedProvider] = useState(null);
  const { data: systemInfo } = useSystemInfo();
 
  const microsoftIcon = () => {
    return (
      <span data-testid={"SignInOptions-microsoftIcon"}>
        <FaMicrosoft size={"10em"} role={"img"} />
      </span>
    );
  };
 
  const googleIcon = () => {
    return (
      <span data-testid={"SignInOptions-googleIcon"}>
        <FcGoogle size={"10em"} role={"img"} />
      </span>
    );
  };
 
  const notSetUp = () => {
    return (
      <p>
        We&#39;re sorry, your login provider isn&#39;t available at this time.
        Please contact your instructor.
      </p>
    );
  };
 
  const setRedirect = () => {
    sessionStorage.setItem("redirect", "/onboarding");
  };
 
  // When a parent supplies onSignIn (for example the instructor preview), the
  // sign in card becomes a plain button that calls it, so no real OAuth starts.
  const handleSignIn = onSignIn
    ? () => onSignIn(selectedProvider)
    : setRedirect;
  const signInUrl = (url) => (onSignIn ? undefined : url);
 
  const renderProviderSignIn = () => {
    switch (selectedProvider) {
      case "microsoft": {
        return (
          <>
            {systemInfo.activeDirectoryUrl ? (
              <SignInCard
                Icon={microsoftIcon}
                title={"Sign in with Microsoft"}
                description={
                  <>
                    Please sign in with your university-associated account via
                    Microsoft.
                  </>
                }
                url={signInUrl(systemInfo.activeDirectoryUrl)}
                testid={"microsoft"}
                onClick={handleSignIn}
              />
            ) : (
              notSetUp()
            )}
          </>
        );
      }
      case "google": {
        return (
          <>
            {systemInfo.oauthLogin ? (
              <SignInCard
                Icon={googleIcon}
                title={"Sign in with Google"}
                description={
                  <>
                    Please sign in with your university-associated account via
                    Google.
                  </>
                }
                url={signInUrl(systemInfo.oauthLogin)}
                testid={"google"}
                onClick={handleSignIn}
              />
            ) : (
              notSetUp()
            )}
          </>
        );
      }
    }
  };
 
  return (
    <>
      <Row>
        <Col>
          <h1>Welcome to Frontiers!</h1>
          <p>Let&#39;s get started.</p>
          <p>
            We need a bit of information from you. Please select the school you
            have credentials with:
          </p>
          <Typeahead
            options={schoolToProvider}
            placeholder="Start typing to select a school..."
            labelKey="schoolName"
            inputProps={{
              "aria-label": "Choose a school",
              "data-testid": "SelectSchool-typeahead",
            }}
            onChange={(selected) => {
              setSelectedProvider(selected[0]?.provider);
            }}
          />
        </Col>
        <Col className="text-center justify-content-center align-items-center d-flex">
          <>{selectedProvider && renderProviderSignIn()}</>
        </Col>
      </Row>
    </>
  );
}