Skip to content

Commit

Permalink
Fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Sep 26, 2024
1 parent 16efe96 commit b6d5638
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export const ChangePasswordForm = () => {

<FormControl isInvalid={!!errors.newPassword} marginY={6}>
<PasswordInput
checkPasswordStrength
data-testid="new-password"
inputName="newPassword"
isCheckPasswordStrengthEnabled
label="New Password"
placeholder="Enter new password"
required="New password is required"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export const EnterAndConfirmPassword = ({
<form onSubmit={handleSubmit(onSubmit)} style={{ width: "100%" }}>
<FormControl isInvalid={!!errors.password}>
<PasswordInput
checkPasswordStrength
data-testid="password"
inputName="password"
isCheckPasswordStrengthEnabled
placeholder="Enter master password"
/>
{errors.password && <FormErrorMessage>{errors.password.message}</FormErrorMessage>}
Expand Down
22 changes: 11 additions & 11 deletions apps/desktop/src/components/PasswordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type PasswordInputProps<T extends FieldValues, U extends Path<T>> = {
inputName: U;
label?: string;
placeholder?: string;
checkPasswordStrength?: boolean;
isCheckPasswordStrengthEnabled?: boolean;
required?: string | boolean;
minLength?: RegisterOptions<T, U>["minLength"];
validate?: (val: string) => string | boolean;
Expand All @@ -33,29 +33,29 @@ export const PasswordInput = <T extends FieldValues, U extends Path<T>>({
label = "Password",
placeholder = "Enter your password",
required = "Password is required",
checkPasswordStrength = false,
isCheckPasswordStrengthEnabled = false,
minLength = MIN_LENGTH,
validate,
...rest
}: PasswordInputProps<T, U>) => {
const { register } = useFormContext<T>();
const [showPassword, setShowPassword] = useState<boolean>(false);
const { validatePassword, PasswordStrengthBar } = usePasswordValidation({
colorScheme: colors.gray[500],
const { validatePasswordStrength, PasswordStrengthBar } = usePasswordValidation({
color: colors.gray[500],
inputName,
});

const handleValidate = (val: string) => {
if (validate) {
const validateResult = validate(val);
const validationResult = validate(val);

if (checkPasswordStrength && validateResult === true) {
return validatePassword(val);
if (isCheckPasswordStrengthEnabled && validationResult === true) {
return validatePasswordStrength(val);
}

return validateResult;
} else if (checkPasswordStrength) {
return validatePassword(val);
return validationResult;
} else if (isCheckPasswordStrengthEnabled) {
return validatePasswordStrength(val);
}
};

Expand Down Expand Up @@ -91,7 +91,7 @@ export const PasswordInput = <T extends FieldValues, U extends Path<T>>({
</Button>
</InputRightElement>
</InputGroup>
{checkPasswordStrength && PasswordStrengthBar}
{isCheckPasswordStrengthEnabled && PasswordStrengthBar}
</>
);
};
2 changes: 1 addition & 1 deletion apps/desktop/src/setupTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jest.mock("./env", () => ({ IS_DEV: false }));
jest.mock("@umami/components", () => ({
...jest.requireActual("@umami/components"),
usePasswordValidation: () => ({
validatePassword: () => true,
validatePasswordStrength: () => true,
PasswordStrengthBar: <div>PasswordStrengthBar</div>,
}),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export const ChangePasswordMenu = () => {
/>
<Divider />
<PasswordInput
checkPasswordStrength
data-testid="new-password"
inputName="newPassword"
isCheckPasswordStrengthEnabled
label="New Password"
placeholder="New password"
required="New password is required"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ export const SetupPassword = ({ mode }: SetupPasswordProps) => {
<ModalBody>
<Flex flexDirection="column" gap="24px">
<PasswordInput
checkPasswordStrength={!isPasswordSet}
inputName="password"
isCheckPasswordStrengthEnabled={!isPasswordSet}
label={isPasswordSet ? "Password" : "Set Password"}
required="Password is required"
/>
Expand Down
16 changes: 8 additions & 8 deletions apps/web/src/components/PasswordInput/PasswordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type PasswordInputProps<T extends FieldValues, U extends Path<T>> = {
label?: string;
placeholder?: string;
required?: string | boolean;
checkPasswordStrength?: boolean;
isCheckPasswordStrengthEnabled?: boolean;
minLength?: RegisterOptions<T, U>["minLength"];
validate?: (val: string) => string | boolean;
} & InputProps & {
Expand All @@ -37,7 +37,7 @@ export const PasswordInput = <T extends FieldValues, U extends Path<T>>({
placeholder = "Enter your password",
required = "Password is required",
minLength = MIN_LENGTH,
checkPasswordStrength = false,
isCheckPasswordStrengthEnabled = false,
validate,
...rest
}: PasswordInputProps<T, U>) => {
Expand All @@ -46,7 +46,7 @@ export const PasswordInput = <T extends FieldValues, U extends Path<T>>({
formState: { errors },
} = useFormContext<T>();
const [showPassword, setShowPassword] = useState<boolean>(false);
const { validatePassword, PasswordStrengthBar } = usePasswordValidation({
const { validatePasswordStrength, PasswordStrengthBar } = usePasswordValidation({
inputName,
});

Expand All @@ -59,13 +59,13 @@ export const PasswordInput = <T extends FieldValues, U extends Path<T>>({
if (validate) {
const validateResult = validate(val);

if (checkPasswordStrength && validateResult === true) {
return validatePassword(val);
if (isCheckPasswordStrengthEnabled && validateResult === true) {
return validatePasswordStrength(val);
}

return validateResult;
} else if (checkPasswordStrength) {
return validatePassword(val);
} else if (isCheckPasswordStrengthEnabled) {
return validatePasswordStrength(val);
}
};

Expand Down Expand Up @@ -108,7 +108,7 @@ export const PasswordInput = <T extends FieldValues, U extends Path<T>>({
/>
</InputRightElement>
</InputGroup>
{checkPasswordStrength && PasswordStrengthBar}
{isCheckPasswordStrengthEnabled && PasswordStrengthBar}
{error && (
<FormErrorMessage data-testid={`${rest["data-testid"]}-error`}>
{errorMessage}
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/hooks/usePasswordValidation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const password = {
};

const TestComponent = () => {
const { validatePassword, PasswordStrengthBar } = usePasswordValidation();
const { validatePasswordStrength, PasswordStrengthBar } = usePasswordValidation();
const { register } = useFormContext();

return (
Expand All @@ -21,7 +21,7 @@ const TestComponent = () => {
id="password"
type="text"
{...register("password", {
validate: validatePassword,
validate: validatePasswordStrength,
})}
/>
{PasswordStrengthBar}
Expand Down
26 changes: 13 additions & 13 deletions packages/components/src/hooks/usePasswordValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import { useFormContext } from "react-hook-form";
import zxcvbn from "zxcvbn";

const DEFAULT_SCORE = 0;
const DEFAULT_COLOR_SCHEME = "gray.100";
const DEFAULT_COLOR = "gray.100";

type PasswordStrengthBarProps = {
score: number;
colorScheme: string;
color: string;
inputName: string;
};

const PasswordStrengthBar = ({ score, colorScheme, inputName }: PasswordStrengthBarProps) => {
type UsePasswordValidationProps = {
color?: string;
inputName?: string;
};

const PasswordStrengthBar = ({ score, color, inputName }: PasswordStrengthBarProps) => {
const form = useFormContext();

const colors = [colorScheme, "red.500", "yellow.500", "green.500"];
const colors = [color, "red.500", "yellow.500", "green.500"];
const passwordError = form.formState.errors[inputName];

const getColor = (index: number) => {
Expand Down Expand Up @@ -75,13 +80,8 @@ const PasswordStrengthBar = ({ score, colorScheme, inputName }: PasswordStrength
);
};

type UsePasswordValidationProps = {
colorScheme?: string;
inputName?: string;
};

export const usePasswordValidation = ({
colorScheme = DEFAULT_COLOR_SCHEME,
color = DEFAULT_COLOR,
inputName = "password",
}: UsePasswordValidationProps = {}) => {
const [passwordScore, setPasswordScore] = useState(DEFAULT_SCORE);
Expand All @@ -95,7 +95,7 @@ export const usePasswordValidation = ({
}
}, [passwordError]);

const validatePassword = (value: string) => {
const validatePasswordStrength = (value: string) => {
const result = zxcvbn(value);

setPasswordScore(result.score);
Expand All @@ -107,9 +107,9 @@ export const usePasswordValidation = ({
};

return {
validatePassword,
validatePasswordStrength,
PasswordStrengthBar: (
<PasswordStrengthBar colorScheme={colorScheme} inputName={inputName} score={passwordScore} />
<PasswordStrengthBar color={color} inputName={inputName} score={passwordScore} />
),
};
};

0 comments on commit b6d5638

Please sign in to comment.