All files / src/components/common/BusinessProfileInputs PasswordInput.tsx

17.39% Statements 4/23
0% Branches 0/44
0% Functions 0/4
17.39% Lines 4/23

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                                      89x                                                                                                                                                                                               89x                                             89x                       89x                                        
import { palette } from "@palette";
import { useFormContext } from "react-hook-form";
// mui
import { Box, Grid, Stack, useTheme, useMediaQuery } from "@mui/material";
// components
import { Text } from "@common/Text";
import { RHFInput } from "@common/Input";
import { Tooltip } from "@common/Tooltip";
import RequirementBox from "@common/Security/RequirementBox";
// assets
import { InfoIcon } from "@assets/icons";
// localization
import { useTranslation } from "react-i18next";
import { namespaces } from "localization/resources/i18n.constants";
 
type Props = {
  isTeamMemberInput?: boolean;
};
 
const PasswordInput = ({ isTeamMemberInput }: Props) => {
  const {
    watch,
    getFieldState,
    // formState: { errors },
  } = useFormContext();
  const values = watch();
  const theme = useTheme();
  const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
  const { t } = useTranslation(namespaces.pages.enterprise.createMerchant);
  const passwordField =
    typeof values.security?.password === "string"
      ? values.security?.password
      : values.password;
  const passwordFieldName = isTeamMemberInput
    ? "password"
    : "security.password";
 
  return (
    <>
      <Grid container sx={{ my: 2 }}>
        <Grid item xs={12} md={isTeamMemberInput ? 10 : 6}>
          <RHFInput
            name={passwordFieldName}
            fullWidth
            type="password"
            placeholder={t("password_placeholder")}
            label={
              <Stack
                mb={1}
                gap={1}
                direction="row"
                justifyContent={isDesktop ? "flex-start" : "space-between"}
              >
                <Text
                  variant="body"
                  fontWeight="medium"
                  color={palette.neutral[800]}
                >
                  {t("password")}
                </Text>
                <Tooltip
                  title={`Passwords must be at least 8 characters long and contain one number. The stongest passwords have at least one upper case letter, at least one number and one of these special characters !@#$&*`}
                  titleSx={{ textAlign: "start", lineHeight: "18px" }}
                >
                  <Box>
                    <InfoIcon
                      width={isDesktop ? 14 : 19}
                      height={isDesktop ? 14 : 19}
                      path_fill={palette.neutral[400]}
                    />
                  </Box>
                </Tooltip>
              </Stack>
            }
            helper={
              passwordField.length >= 8 && /[a-z]/.test(passwordField) ? (
                <>
                  Password strength{" "}
                  <span
                    style={{
                      color: getPasswordStatus(passwordField).color,
                      fontWeight: "500",
                    }}
                  >
                    {getPasswordStatus(passwordField).text}
                  </span>
                </>
              ) : undefined
            }
          />
        </Grid>
      </Grid>
      <Grid container>
        {requirementsList.map((item, index) => {
          return (
            <Grid item xs={12} sm={6} key={index}>
              <RequirementBox
                requirement={item.text}
                color={
                  getFieldState(passwordFieldName).isDirty ||
                  getFieldState(passwordFieldName).isTouched ||
                  getFieldState(passwordFieldName).invalid ||
                  passwordField.length > 0
                    ? getErrorColor(item.type, passwordField)
                    : palette.neutral[400]
                }
              />
            </Grid>
          );
        })}
      </Grid>
    </>
  );
};
 
const requirementsList = [
  {
    type: "minCharacters",
    text: "8 Characters minimum",
  },
  {
    type: "lowercase",
    text: "One lowercase character",
  },
  {
    type: "uppercase",
    text: "One uppercase character",
  },
  {
    type: "number",
    text: "One number",
  },
  {
    type: "specialCharacter",
    text: "One special character",
  },
];
 
const getPasswordStatus = (pwd: string) => {
  if (
    /[a-z]/.test(pwd) &&
    /[A-Z]/.test(pwd) &&
    /[0-9]/.test(pwd) &&
    /[!-/:-@[-`{-~]/.test(pwd)
  )
    return { text: "STRONG", color: palette.success.main };
 
  return { text: "MODERATE", color: palette.warning.main };
};
 
const getErrorColor = (type: string, value: string) => {
  switch (type) {
    case "minCharacters":
      return value.length >= 8 ? palette.success.main : palette.error.main;
    case "lowercase":
      return /[a-z]/.test(value) ? palette.success.main : palette.error.main;
    case "uppercase":
      return /[A-Z]/.test(value) ? palette.success.main : palette.neutral[400];
    case "number":
      return /[0-9]/.test(value) ? palette.success.main : palette.neutral[400];
    case "specialCharacter":
      return /[!-/:-@[-`{-~]/.test(value)
        ? palette.success.main
        : palette.neutral[400];
    default:
      return palette.neutral[400];
  }
};
 
export default PasswordInput;