|
| 1 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { Button, ModalBody, ModalFooter, ModalHeader, Tile, PasswordInput, Form, Layer } from '@carbon/react'; |
| 4 | +import { navigate, showSnackbar } from '@openmrs/esm-framework'; |
| 5 | +import styles from './change-password-modal.scss'; |
| 6 | +import { performPasswordChange } from './change-password-model.resource'; |
| 7 | + |
| 8 | +interface ChangePasswordModalProps { |
| 9 | + close(): void; |
| 10 | +} |
| 11 | + |
| 12 | +export default function ChangePasswordModal({ close }: ChangePasswordModalProps) { |
| 13 | + const { t } = useTranslation(); |
| 14 | + const [isSavingPassword, setIsSavingPassword] = useState(false); |
| 15 | + const oldPasswordInputRef = useRef<HTMLInputElement>(null); |
| 16 | + const newPasswordInputRef = useRef<HTMLInputElement>(null); |
| 17 | + const confirmPasswordInputRef = useRef<HTMLInputElement>(null); |
| 18 | + const formRef = useRef<HTMLFormElement>(null); |
| 19 | + const [newPasswordError, setNewPasswordErr] = useState(''); |
| 20 | + const [oldPasswordError, setOldPasswordErr] = useState(''); |
| 21 | + const [confirmPasswordError, setConfirmPasswordError] = useState(''); |
| 22 | + const [isOldPasswordInvalid, setIsOldPasswordInvalid] = useState<boolean>(true); |
| 23 | + const [isNewPasswordInvalid, setIsNewPasswordInvalid] = useState<boolean>(true); |
| 24 | + const [isConfirmPasswordInvalid, setIsConfirmPasswordInvalid] = useState<boolean>(true); |
| 25 | + const [passwordInput, setPasswordInput] = useState({ |
| 26 | + oldPassword: '', |
| 27 | + newPassword: '', |
| 28 | + confirmPassword: '', |
| 29 | + }); |
| 30 | + |
| 31 | + const handleValidation = useCallback( |
| 32 | + (passwordInputValue, passwordInputFieldName) => { |
| 33 | + if (passwordInputFieldName === 'newPassword') { |
| 34 | + const uppercaseRegExp = /(?=.*?[A-Z])/; |
| 35 | + const lowercaseRegExp = /(?=.*?[a-z])/; |
| 36 | + const digitsRegExp = /(?=.*?[0-9])/; |
| 37 | + const minLengthRegExp = /.{8,}/; |
| 38 | + const passwordLength = passwordInputValue.length; |
| 39 | + const uppercasePassword = uppercaseRegExp.test(passwordInputValue); |
| 40 | + const lowercasePassword = lowercaseRegExp.test(passwordInputValue); |
| 41 | + const digitsPassword = digitsRegExp.test(passwordInputValue); |
| 42 | + const minLengthPassword = minLengthRegExp.test(passwordInputValue); |
| 43 | + let errMsg = ''; |
| 44 | + if (passwordLength === 0) { |
| 45 | + errMsg = t('passwordIsEmpty', 'Password is empty'); |
| 46 | + } else if (!uppercasePassword) { |
| 47 | + errMsg = t('atLeastOneUppercase', 'At least one Uppercase'); |
| 48 | + } else if (!lowercasePassword) { |
| 49 | + errMsg = t('atLeastOneLowercase', 'At least one Lowercase'); |
| 50 | + } else if (!digitsPassword) { |
| 51 | + errMsg = t('atLeastOneDigit', 'At least one digit'); |
| 52 | + } else if (!minLengthPassword) { |
| 53 | + errMsg = t('minimum8Characters', 'Minimum 8 characters'); |
| 54 | + } else if (passwordInput.oldPassword.length > 0 && passwordInput.newPassword === passwordInput.oldPassword) { |
| 55 | + errMsg = t('newPasswordMustNotBeTheSameAsOld', 'New password must not be the same as password'); |
| 56 | + } else { |
| 57 | + errMsg = ''; |
| 58 | + setIsNewPasswordInvalid(false); |
| 59 | + } |
| 60 | + setNewPasswordErr(errMsg); |
| 61 | + } else if ( |
| 62 | + passwordInputFieldName === 'confirmPassword' || |
| 63 | + (passwordInputFieldName === 'newPassword' && passwordInput.confirmPassword.length > 0) |
| 64 | + ) { |
| 65 | + if (passwordInput.confirmPassword !== passwordInput.newPassword) { |
| 66 | + setConfirmPasswordError(t('confirmPasswordMustBeTheSameAsNew', 'Confirm password must be the same as new')); |
| 67 | + } else { |
| 68 | + setConfirmPasswordError(''); |
| 69 | + setIsConfirmPasswordInvalid(false); |
| 70 | + } |
| 71 | + } else { |
| 72 | + if (passwordInput.newPassword.length > 0 && passwordInput.newPassword === passwordInput.oldPassword) { |
| 73 | + setOldPasswordErr(t('oldPasswordMustNotBeTheSameAsNew', 'Old password must not be the same as new')); |
| 74 | + } else { |
| 75 | + setOldPasswordErr(''); |
| 76 | + setIsOldPasswordInvalid(false); |
| 77 | + } |
| 78 | + } |
| 79 | + }, |
| 80 | + [passwordInput.confirmPassword, passwordInput.newPassword, passwordInput.oldPassword, t], |
| 81 | + ); |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + if (passwordInput.oldPassword !== '') { |
| 85 | + handleValidation(passwordInput.oldPassword, 'oldPassword'); |
| 86 | + } |
| 87 | + if (passwordInput.newPassword !== '') { |
| 88 | + handleValidation(passwordInput.newPassword, 'newPassword'); |
| 89 | + } |
| 90 | + if (passwordInput.confirmPassword !== '') { |
| 91 | + handleValidation(passwordInput.confirmPassword, 'confirmPassword'); |
| 92 | + } |
| 93 | + }, [handleValidation, passwordInput]); |
| 94 | + |
| 95 | + const handlePasswordChange = (event) => { |
| 96 | + const passwordInputValue = event.target.value.trim(); |
| 97 | + const passwordInputFieldName = event.target.name; |
| 98 | + const NewPasswordInput = { ...passwordInput, [passwordInputFieldName]: passwordInputValue }; |
| 99 | + setPasswordInput(NewPasswordInput); |
| 100 | + }; |
| 101 | + |
| 102 | + const handleSubmit = useCallback( |
| 103 | + async (evt: React.FormEvent<HTMLFormElement>) => { |
| 104 | + evt.preventDefault(); |
| 105 | + setIsSavingPassword(true); |
| 106 | + performPasswordChange(passwordInput.oldPassword, passwordInput.confirmPassword) |
| 107 | + .then(() => { |
| 108 | + close(); |
| 109 | + navigate({ to: `\${openmrsSpaBase}/logout` }); |
| 110 | + showSnackbar({ |
| 111 | + isLowContrast: true, |
| 112 | + kind: 'success', |
| 113 | + subtitle: t('userPasswordUpdated', 'User password updated successfully'), |
| 114 | + title: t('userPassword', 'User password'), |
| 115 | + }); |
| 116 | + }) |
| 117 | + .catch((error) => { |
| 118 | + setIsSavingPassword(false); |
| 119 | + showSnackbar({ |
| 120 | + title: t('invalidPasswordCredentials', 'Invalid password provided'), |
| 121 | + kind: 'error', |
| 122 | + isLowContrast: false, |
| 123 | + subtitle: error?.message, |
| 124 | + }); |
| 125 | + }); |
| 126 | + }, |
| 127 | + [close, passwordInput.confirmPassword, passwordInput.oldPassword, t], |
| 128 | + ); |
| 129 | + |
| 130 | + return ( |
| 131 | + <> |
| 132 | + <ModalHeader closeModal={close} title={t('changePassword', 'Change Password')} /> |
| 133 | + <ModalBody> |
| 134 | + <Tile> |
| 135 | + <Form onSubmit={handleSubmit} ref={formRef}> |
| 136 | + <div className={styles['input-group']}> |
| 137 | + <Layer> |
| 138 | + <PasswordInput |
| 139 | + id="oldPassword" |
| 140 | + invalid={oldPasswordError.length > 0} |
| 141 | + invalidText={oldPasswordError} |
| 142 | + labelText={t('oldPassword', 'Old Password')} |
| 143 | + name="oldPassword" |
| 144 | + value={passwordInput.oldPassword} |
| 145 | + onChange={handlePasswordChange} |
| 146 | + ref={oldPasswordInputRef} |
| 147 | + required |
| 148 | + showPasswordLabel="Show old password" |
| 149 | + /> |
| 150 | + </Layer> |
| 151 | + <Layer> |
| 152 | + <PasswordInput |
| 153 | + id="newPassword" |
| 154 | + invalid={newPasswordError.length > 0} |
| 155 | + invalidText={newPasswordError} |
| 156 | + labelText={t('newPassword', 'New Password')} |
| 157 | + name="newPassword" |
| 158 | + value={passwordInput.newPassword} |
| 159 | + onChange={handlePasswordChange} |
| 160 | + ref={newPasswordInputRef} |
| 161 | + required |
| 162 | + showPasswordLabel="Show new password" |
| 163 | + /> |
| 164 | + </Layer> |
| 165 | + <Layer> |
| 166 | + <PasswordInput |
| 167 | + id="confirmPassword" |
| 168 | + invalid={confirmPasswordError.length > 0} |
| 169 | + invalidText={confirmPasswordError} |
| 170 | + labelText={t('confirmPassword', 'Confirm Password')} |
| 171 | + name="confirmPassword" |
| 172 | + value={passwordInput.confirmPassword} |
| 173 | + onChange={handlePasswordChange} |
| 174 | + ref={confirmPasswordInputRef} |
| 175 | + required |
| 176 | + showPasswordLabel="Show confirm password" |
| 177 | + /> |
| 178 | + </Layer> |
| 179 | + </div> |
| 180 | + </Form> |
| 181 | + </Tile> |
| 182 | + </ModalBody> |
| 183 | + <ModalFooter> |
| 184 | + <Button kind="secondary" onClick={close}> |
| 185 | + {t('cancel', 'Cancel')} |
| 186 | + </Button> |
| 187 | + <Button |
| 188 | + type="submit" |
| 189 | + onClick={handleSubmit} |
| 190 | + disabled={isSavingPassword || isNewPasswordInvalid || isConfirmPasswordInvalid || isOldPasswordInvalid} |
| 191 | + > |
| 192 | + {t('updatePassword', 'Update Password')} |
| 193 | + </Button> |
| 194 | + </ModalFooter> |
| 195 | + </> |
| 196 | + ); |
| 197 | +} |
0 commit comments