UNPKG

@open-tender/utils

Version:

A library of utils for use with Open Tender applications that utilize our cloud-based Order API.

45 lines (44 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usePasswordValidation = void 0; const react_1 = require("react"); const passwordValidation_1 = require("../utils/passwordValidation"); /** * Custom hook for password field validation and state management * Follows SRP by encapsulating all password-related state and logic */ const usePasswordValidation = () => { const [passwordTouched, setPasswordTouched] = (0, react_1.useState)(false); const handlePasswordChange = () => { if (!passwordTouched) { setPasswordTouched(true); } }; const isPasswordValid = (password) => { if (!password) return false; const validation = (0, passwordValidation_1.validatePassword)(password); return validation.isValid; }; const shouldShowRequirements = (password, hidePassword = false) => { return !hidePassword && !!password; }; const getPasswordErrors = (password, errors, fieldName = 'password') => { const passwordErrors = {}; if (errors[fieldName]) { // If password is valid according to frontend but still has error, it's from backend const isValidFrontend = password && (0, passwordValidation_1.validatePassword)(password).isValid; if (isValidFrontend || !password) { passwordErrors[fieldName] = errors[fieldName]; } } return passwordErrors; }; return { handlePasswordChange, isPasswordValid, shouldShowRequirements, getPasswordErrors }; }; exports.usePasswordValidation = usePasswordValidation;