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

98.68% Statements 75/76
81.81% Branches 27/33
100% Functions 7/7
98.64% Lines 73/74

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 2851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   15x                                                                                                   30x 29x 29x 29x 29x 6x                                                 23x 8x               15x 4x               11x 6x               5x 1x               4x 2x                     2x       1x 35x 34x 34x 34x 34x 34x 34x   34x 34x   34x   34x 17x     34x 19x     34x 18x 18x 2x 2x 2x       1x         1x     1x 1x   1x             34x                         34x                                                                                                                          
import { Divider, Typography } from "@material-ui/core";
import classNames from "classnames";
import Alert from "components/Alert";
import CircularProgress from "components/CircularProgress";
import HtmlMeta from "components/HtmlMeta";
import Redirect from "components/Redirect";
import StyledLink from "components/StyledLink";
import MobileAuthBg from "features/auth/resources/mobile-auth-bg.jpg";
import CommunityGuidelinesForm from "features/auth/signup/CommunityGuidelinesForm";
import { Trans, useTranslation } from "i18n";
import { AUTH, GLOBAL } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useIsNativeEmbed } from "platform/nativeLink";
import Sentry from "platform/sentry";
import { useEffect, useState } from "react";
import vercelLogo from "resources/vercel.svg";
import { dashboardRoute, loginRoute, signupRoute, tosRoute } from "routes";
import { service } from "service";
import isGrpcError from "service/utils/isGrpcError";
import makeStyles from "utils/makeStyles";
import stringOrFirstString from "utils/stringOrFirstString";
 
import { useAuthContext } from "../AuthProvider";
import useAuthStyles from "../useAuthStyles";
import AccountForm from "./AccountForm";
import BasicForm from "./BasicForm";
import FeedbackForm from "./FeedbackForm";
import ResendVerificationEmailForm from "./ResendVerificationEmailForm";
 
const useStyles = makeStyles((theme) => ({
  agreement: {
    textAlign: "center",
    [theme.breakpoints.up("md")]: {
      marginTop: theme.spacing(3),
      textAlign: "left",
    },
  },
  stickyPage: {
    position: "fixed",
    left: 0,
    right: 0,
    top: theme.shape.navPaddingXs,
    [theme.breakpoints.up("sm")]: {
      top: theme.shape.navPaddingSmUp,
    },
    bottom: 0,
    zIndex: 1,
    [theme.breakpoints.down("sm")]: {
      position: "absolute",
      background: `linear-gradient(rgba(255, 255, 255, 0) 50%, rgba(255, 255, 255, 1)), url("${MobileAuthBg.src}")`,
    },
  },
  scrollingContent: {
    position: "relative",
    zIndex: 2,
    justifyContent: "center",
    minHeight: `calc(100vh - ${theme.shape.navPaddingXs})`,
    [theme.breakpoints.up("sm")]: {
      minHeight: `calc(100vh - ${theme.shape.navPaddingSmUp})`,
    },
  },
  scrollingForm: {
    flexGrow: 1,
    alignSelf: "flex-start",
    marginTop: theme.spacing(10),
    marginBottom: theme.spacing(4),
    [theme.breakpoints.up("md")]: {
      alignSelf: "flex-end",
    },
    [theme.breakpoints.down("sm")]: {
      width: "100%",
    },
  },
  mobileEmbed: {
    margin: theme.spacing(3),
  },
}));
 
function CurrentForm() {
  const { t } = useTranslation([AUTH, GLOBAL]);
  const classes = useStyles();
  const { authState } = useAuthContext();
  const state = authState.flowState;
  if (!state || state.needBasic) {
    return (
      <>
        <Typography variant="h1" gutterBottom>
          {t("auth:basic_sign_up_form.header")}
        </Typography>
        {!state && (
          <Typography gutterBottom>
            <Trans i18nKey="auth:basic_sign_up_form.existing_user_prompt">
              Already have an account?{" "}
              <StyledLink href={loginRoute}>Log in</StyledLink>
            </Trans>
          </Typography>
        )}
        <BasicForm />
        <Typography variant="body1" className={classes.agreement}>
          <Trans i18nKey="auth:basic_sign_up_form.sign_up_agreement_explainer">
            By continuing, you agree to our{" "}
            <StyledLink href={tosRoute} target="_blank">
              Terms of Service
            </StyledLink>
            , including our cookie, email, and data handling policies.
          </Trans>
        </Typography>
      </>
    );
  } else if (state.needAccount) {
    return (
      <>
        <Typography variant="h1" gutterBottom>
          {t("auth:account_form.header")}
        </Typography>
        <AccountForm />
      </>
    );
  } else if (state.needAcceptCommunityGuidelines) {
    return (
      <>
        <Typography variant="h1" gutterBottom>
          {t("auth:community_guidelines_form.header")}
        </Typography>
        <CommunityGuidelinesForm />
      </>
    );
  } else if (state.needFeedback) {
    return (
      <>
        <Typography variant="h1" gutterBottom>
          {t("auth:feedback_form.header")}
        </Typography>
        <FeedbackForm />
      </>
    );
  } else if (state.needVerifyEmail) {
    return (
      <>
        <Typography variant="h1" gutterBottom>
          {t("auth:sign_up_need_verification_title")}
        </Typography>
        <ResendVerificationEmailForm />
      </>
    );
  } else if (state.authRes) {
    return (
      <>
        <Typography variant="h1" 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"));
  }
}
 
export default function Signup() {
  const { t } = useTranslation([AUTH, GLOBAL]);
  const { authState, authActions } = useAuthContext();
  const authenticated = authState.authenticated;
  const error = authState.error;
  const authClasses = useAuthStyles();
  const classes = useStyles();
  const [loading, setLoading] = useState(false);
 
  const router = useRouter();
  const urlToken = stringOrFirstString(router.query.token);
 
  const isNativeEmbed = useIsNativeEmbed();
 
  useEffect(() => {
    authActions.clearError();
  }, [authActions]);
 
  useEffect(() => {
    if (authState.error) window.scroll({ top: 0, behavior: "smooth" });
  }, [authState.error]);
 
  useEffect(() => {
    (async () => {
      if (urlToken) {
        setLoading(true);
        try {
          authActions.updateSignupState(
            await service.auth.signupFlowEmailToken(urlToken)
          );
        } catch (err) {
          Sentry.captureException(err, {
            tags: {
              component: "auth/signup/Signup",
            },
          });
          authActions.authError(
            isGrpcError(err) ? err.message : t("global:error.fatal_message")
          );
          router.push(signupRoute);
          return;
        }
        setLoading(false);
      }
    })();
    // next-router-mock router isn't memoized, so putting router in the dependencies
    // causes infinite looping in tests
  }, [urlToken, authActions, t]); // eslint-disable-line react-hooks/exhaustive-deps
 
  Iif (isNativeEmbed) {
    return (
      <div className={classes.mobileEmbed}>
        {error && (
          <Alert className={authClasses.errorMessage} severity="error">
            {error}
          </Alert>
        )}
        {loading ? <CircularProgress /> : <CurrentForm />}
      </div>
    );
  }
 
  return (
    <>
      {authenticated && <Redirect to={dashboardRoute} />}
      <HtmlMeta title={t("global:sign_up")} />
      <div
        className={classNames(
          authClasses.page,
          classes.stickyPage,
          authClasses.pageBackground
        )}
      >
        <div className={authClasses.content}>
          <div className={authClasses.introduction}>
            <Typography
              classes={{ root: authClasses.title }}
              variant="h1"
              component="span"
            >
              {t("auth:introduction_title")}
            </Typography>
            <Typography
              classes={{ root: authClasses.subtitle }}
              variant="h2"
              component="span"
            >
              {t("auth:introduction_subtitle")}
              <Divider className={authClasses.underline}></Divider>
            </Typography>
          </div>
          <div
            style={{
              //this div is to match the flex layout on the login page
              width: "45%",
            }}
          ></div>
        </div>
      </div>
      <div className={classNames(authClasses.page, classes.scrollingContent)}>
        <div
          className={classNames(authClasses.formWrapper, classes.scrollingForm)}
        >
          {error && (
            <Alert className={authClasses.errorMessage} severity="error">
              {error}
            </Alert>
          )}
          {loading ? <CircularProgress /> : <CurrentForm />}
        </div>
        {process.env.NEXT_PUBLIC_COUCHERS_ENV !== "prod" && (
          <a
            className={authClasses.vercelLink}
            rel="noopener noreferrer"
            href="https://vercel.com?utm_source=couchers-org&utm_campaign=oss"
          >
            <img alt={t("auth:vercel_logo_alt_text")} src={vercelLogo.src} />
          </a>
        )}
      </div>
    </>
  );
}