All files / app/features/auth/verification PhoneVerificationSection.tsx

55.17% Statements 32/58
76.92% Branches 20/26
6.25% Functions 1/16
57.14% Lines 32/56

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 2512x     2x 2x 2x 2x 2x 2x 2x     2x 2x 2x   2x 2x 2x 2x 2x 2x   2x                 2x           2x                                   17x 34x 34x       34x   34x           34x   34x             34x                               34x                             34x                     34x 34x                                                                                                                                                                                                                                                                
import "react-phone-number-input/style.css";
 
import { CheckCircle, SmartphoneOutlined } from "@mui/icons-material";
import { Box, Typography } from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import StyledLink from "components/StyledLink";
import TextField from "components/TextField";
import { accountInfoQueryKey } from "features/queryKeys";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { Trans, useTranslation } from "i18n";
import { AUTH } from "i18n/namespaces";
import luhn from "luhn";
import { GetAccountInfoRes } from "proto/account_pb";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { formatPhoneNumberIntl, isValidPhoneNumber } from "react-phone-number-input";
import PhoneInputWithCountry from "react-phone-number-input/react-hook-form";
import { donationsRoute } from "routes";
import { service } from "service";
 
import { ActionRow, SectionBlock, SectionRoot, SuccessBanner, VerificationSectionHeader } from "./VerificationSection";
 
type PhoneVerificationSectionProps = {
  accountInfo: GetAccountInfoRes.AsObject;
};
 
type ChangePhoneFormData = { phone: string };
type VerifyPhoneFormData = { code: string };
 
const validatePhoneCode = (code: string) => code.length === 6 && luhn.validate(code);
 
/**
 * The phone number widget ships its own CSS; this pulls its input in line with
 * the rest of the page's fields.
 */
const phoneInputSx = {
  flexGrow: 1,
  width: "100%",
  "& .PhoneInputInput": {
    padding: "12px 14px",
    fontSize: "1rem",
    fontFamily: "inherit",
    color: "var(--mui-palette-text-primary)",
    backgroundColor: "var(--mui-palette-background-paper)",
    border: "1px solid var(--mui-palette-grey-300)",
    borderRadius: "4px",
    "&:focus": {
      outline: "2px solid var(--mui-palette-primary-main)",
      outlineOffset: "-1px",
    },
  },
};
 
export default function PhoneVerificationSection({ accountInfo }: PhoneVerificationSectionProps) {
  const { t } = useTranslation(AUTH);
  const queryClient = useQueryClient();
 
  // In the verified and code-sent states the change form is opt-in, so the
  // section leads with the state the user is actually in.
  const [isChangingNumber, setIsChangingNumber] = useState(false);
 
  const { handleSubmit: changeHandleSubmit, reset: resetChangeForm, control } = useForm<ChangePhoneFormData>();
  const {
    handleSubmit: verifyHandleSubmit,
    register: verifyRegister,
    reset: resetVerifyForm,
    formState: { errors: verifyFormErrors },
  } = useForm<VerifyPhoneFormData>({ mode: "onBlur" });
 
  const invalidateAccountInfo = () => queryClient.invalidateQueries({ queryKey: [accountInfoQueryKey] });
 
  const {
    error: changeError,
    isPending: isChangePending,
    mutate: changePhone,
    reset: resetChange,
  } = useMutation<Empty, RpcError, ChangePhoneFormData>({
    mutationFn: ({ phone }) => service.account.changePhone(phone),
    onSuccess: () => {
      invalidateAccountInfo();
      resetChangeForm();
      resetVerify();
      resetRemove();
      setIsChangingNumber(false);
    },
  });
 
  const {
    error: verifyError,
    isPending: isVerifyPending,
    mutate: verifyPhone,
    reset: resetVerify,
  } = useMutation<Empty, RpcError, VerifyPhoneFormData>({
    mutationFn: ({ code }) => service.account.verifyPhone(code),
    onSuccess: () => {
      invalidateAccountInfo();
      resetVerifyForm();
      resetChange();
      resetRemove();
    },
  });
 
  const {
    error: removeError,
    isPending: isRemovePending,
    mutate: removePhone,
    reset: resetRemove,
  } = useMutation<Empty, RpcError>({
    mutationFn: () => service.account.removePhone(),
    onSuccess: () => {
      invalidateAccountInfo();
      resetChangeForm();
      resetVerifyForm();
      resetChange();
      resetVerify();
    },
  });
 
  const status = accountInfo.phoneVerified ? "verified" : accountInfo.phone ? "code_sent" : "not_verified";
  const formattedPhone = accountInfo.phone ? formatPhoneNumberIntl(accountInfo.phone) : "";
 
  const changeNumberForm = (
    <Box
      component="form"
      onSubmit={changeHandleSubmit(({ phone }) => changePhone({ phone }))}
      sx={{ display: "flex", flexDirection: "column", gap: 1.5, maxWidth: 560 }}
    >
      <Typography component="label" variant="body2" htmlFor="phone" sx={{ color: "var(--mui-palette-text-secondary)" }}>
        {t("change_phone.phone_label")}
      </Typography>
      {/* Button sits beside the number on desktop and drops under it, centred,
          once there isn't room for both. */}
      <Box
        sx={{
          display: "flex",
          gap: 1.5,
          flexDirection: { xs: "column", sm: "row" },
          alignItems: "center",
        }}
      >
        <Box sx={phoneInputSx}>
          <PhoneInputWithCountry
            name="phone"
            control={control}
            rules={{ validate: (value: string) => isValidPhoneNumber(value) }}
            international
            placeholder={t("change_phone.phone_label")}
            id="phone"
          />
        </Box>
        <Button type="submit" loading={isChangePending} sx={{ flexShrink: 0 }}>
          {accountInfo.phone ? t("change_phone.change_button_text") : t("change_phone.add_button_text")}
        </Button>
      </Box>
    </Box>
  );
 
  return (
    <SectionRoot id="phone-verification">
      <VerificationSectionHeader
        icon={<SmartphoneOutlined />}
        iconTint="secondary"
        title={t("verification_page.phone.header")}
        description={t("verification_page.phone.description")}
        status={status}
        badgeId="phone_verified"
      />
 
      {changeError && <Alert severity="error">{changeError.message}</Alert>}
      {verifyError && <Alert severity="error">{verifyError.message}</Alert>}
      {removeError && <Alert severity="error">{removeError.message}</Alert>}
 
      {!accountInfo.hasDonated ? (
        // ChangePhone is gated on having donated backend-side too, so showing the
        // form here would only earn a FAILED_PRECONDITION.
        <Typography variant="body1">
          <Trans
            t={t}
            i18nKey="change_phone.need_to_donate"
            components={{ donateLink: <StyledLink href={donationsRoute} /> }}
          />
        </Typography>
      ) : !accountInfo.phone ? (
        changeNumberForm
      ) : !accountInfo.phoneVerified ? (
        <SectionBlock sx={{ maxWidth: 420 }}>
          <Typography variant="body1">
            <Trans
              t={t}
              i18nKey="verification_page.phone.code_sent_description"
              values={{ phone_number: formattedPhone }}
            />
          </Typography>
          <Box component="form" onSubmit={verifyHandleSubmit(({ code }) => verifyPhone({ code }))}>
            <TextField
              id="code"
              {...verifyRegister("code", {
                required: true,
                validate: (code) => validatePhoneCode(code) || t("change_phone.wrong_code"),
              })}
              label={t("change_phone.code_label")}
              helperText={verifyFormErrors?.code?.message ?? " "}
              error={!!verifyFormErrors?.code?.message}
              fullWidth
              sx={{ "& input": { fontSize: "1.25rem", letterSpacing: "6px" } }}
            />
            <ActionRow>
              <Button type="submit" loading={isVerifyPending}>
                {t("change_phone.verify_button_text")}
              </Button>
              {!isChangingNumber && (
                <Button variant="text" onClick={() => setIsChangingNumber(true)}>
                  {t("verification_page.phone.use_another_number_button")}
                </Button>
              )}
            </ActionRow>
          </Box>
          {isChangingNumber && changeNumberForm}
        </SectionBlock>
      ) : (
        <SectionBlock>
          <SuccessBanner>
            <CheckCircle />
            <Typography variant="body1">
              <Trans
                t={t}
                i18nKey="verification_page.phone.verified_message"
                values={{ phone_number: formattedPhone }}
              />
            </Typography>
          </SuccessBanner>
          <ActionRow>
            <Button variant="outlined" loading={isRemovePending} onClick={() => removePhone()}>
              {t("change_phone.remove_button_text")}
            </Button>
            {!isChangingNumber && (
              <Button variant="text" onClick={() => setIsChangingNumber(true)}>
                {t("verification_page.phone.use_another_number_button")}
              </Button>
            )}
          </ActionRow>
          {isChangingNumber && changeNumberForm}
        </SectionBlock>
      )}
    </SectionRoot>
  );
}