All files / app/features/auth/signup SignupFormContent.tsx

94.59% Statements 35/37
80.95% Branches 17/21
100% Functions 3/3
94.59% Lines 35/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 1531x 1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x 1x   30x         38x 38x 38x 38x     38x   38x 20x 20x 20x       20x     20x 20x           20x     38x                                                                                                     28x                 15x                 8x                 4x                 3x                       2x      
import { Typography } from "@mui/material";
import StyledLink from "components/StyledLink";
import AntibotNote from "features/antibot/AntibotNote";
import { Trans, useTranslation } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { GetSignupPageInfoRes } from "proto/public_pb";
import { useEffect, useState } from "react";
import { missionRoute, tosRoute } from "routes";
 
import { useIsNativeEmbed } from "../../../utils/nativeLink";
import { useAuthContext } from "../AuthProvider";
import AccountForm from "./AccountForm";
import BasicForm from "./BasicForm";
import CommunityGuidelinesForm from "./CommunityGuidelinesForm";
import MotivationsForm from "./MotivationsForm";
import ResendVerificationEmailForm from "./ResendVerificationEmailForm";
 
export default function SignupFormContent({
  inviteCode,
}: {
  inviteCode?: string;
}) {
  const { t } = useTranslation([AUTH, GLOBAL]);
  const { authState } = useAuthContext();
  const state = authState.flowState;
  const isNativeEmbed = useIsNativeEmbed();
 
  const [signupInfo, setSignupInfo] =
    useState<GetSignupPageInfoRes.AsObject | null>(null);
 
  useEffect(() => {
    const fetchSignupInfo = async () => {
      try {
        const response = await fetch(
          "https://couchers.org/api/public/signup-page-info",
        );
 
        Iif (!response.ok) {
          throw new Error("Failed to fetch signup info");
        }
        const data = await response.json();
        setSignupInfo(data);
      } catch (error) {
        console.error("Error fetching signup info:", error);
      }
    };
 
    fetchSignupInfo();
  }, []);
 
  if (!state || state.needBasic) {
    return (
      <>
        <Typography
          gutterBottom
          sx={{ fontSize: "1.4rem", fontWeight: "bold" }}
        >
          {t("landing:signup_header")}
        </Typography>
        <Typography gutterBottom sx={{ marginBottom: 2 }}>
          <Trans
            i18nKey={
              isNativeEmbed
                ? "landing:signup_description_no_link"
                : "landing:signup_description"
            }
            values={{
              user_count: signupInfo?.userCount
                ? Number(signupInfo.userCount).toLocaleString()
                : "65k+",
            }}
            components={
              isNativeEmbed
                ? {}
                : {
                    2: <StyledLink href={missionRoute} />,
                  }
            }
          />
        </Typography>
        <BasicForm
          inviteCode={inviteCode}
          submitText={t("global:create_account")}
        />
        <Typography variant="caption">
          <Trans i18nKey="auth:basic_sign_up_form.sign_up_agreement_explainer">
            By continuing, you agree to our{" "}
            <StyledLink
              href={tosRoute}
              target="_blank"
              variant="caption"
              sx={{ fontWeight: 700 }}
            >
              Terms of Service
            </StyledLink>
            , including our cookie, email, and data handling policies.
          </Trans>{" "}
          <AntibotNote />
        </Typography>
      </>
    );
  } else if (state.needAccount) {
    return (
      <>
        <Typography variant="h2" gutterBottom>
          {t("auth:account_form.header")}
        </Typography>
        <AccountForm />
      </>
    );
  } else if (state.needAcceptCommunityGuidelines) {
    return (
      <>
        <Typography variant="h2" gutterBottom>
          {t("auth:community_guidelines_form.header")}
        </Typography>
        <CommunityGuidelinesForm />
      </>
    );
  } else if (state.needMotivations) {
    return (
      <>
        <Typography variant="h2" gutterBottom>
          {t("auth:motivations_form.header")}
        </Typography>
        <MotivationsForm />
      </>
    );
  } else if (state.needVerifyEmail) {
    return (
      <>
        <Typography variant="h2" gutterBottom>
          {t("auth:sign_up_need_verification_title")}
        </Typography>
        <ResendVerificationEmailForm />
      </>
    );
  } else if (state.authRes) {
    return (
      <>
        <Typography variant="h2" gutterBottom>
          {t("auth:sign_up_completed_title")}
        </Typography>
        <Typography variant="body1">
          {t("auth:sign_up_confirmed_prompt")}
        </Typography>
      </>
    );
  } else {
    throw Error(t("auth:unhandled_sign_up_state"));
  }
}