fix(frontend): improve password update form UX and add translation (#29544)
- disable update button until form is valid - add required field validation - add translation key for password complexity message Signed-off-by: Chayan Das <daschayan8837@gmail.com>
This commit is contained in:
@@ -122,6 +122,7 @@ const PasswordView = ({ user }: PasswordViewProps) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const formMethods = useForm<ChangePasswordSessionFormValues>({
|
const formMethods = useForm<ChangePasswordSessionFormValues>({
|
||||||
|
mode: "onChange",
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
oldPassword: "",
|
oldPassword: "",
|
||||||
newPassword: "",
|
newPassword: "",
|
||||||
@@ -131,22 +132,6 @@ const PasswordView = ({ user }: PasswordViewProps) => {
|
|||||||
const handleSubmit = (values: ChangePasswordSessionFormValues) => {
|
const handleSubmit = (values: ChangePasswordSessionFormValues) => {
|
||||||
const { oldPassword, newPassword } = values;
|
const { oldPassword, newPassword } = values;
|
||||||
|
|
||||||
if (!oldPassword.length) {
|
|
||||||
formMethods.setError(
|
|
||||||
"oldPassword",
|
|
||||||
{ type: "required", message: t("error_required_field") },
|
|
||||||
{ shouldFocus: true }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!newPassword.length) {
|
|
||||||
formMethods.setError(
|
|
||||||
"newPassword",
|
|
||||||
{ type: "required", message: t("error_required_field") },
|
|
||||||
{ shouldFocus: true }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldPassword && newPassword) {
|
if (oldPassword && newPassword) {
|
||||||
passwordMutation.mutate({ oldPassword, newPassword });
|
passwordMutation.mutate({ oldPassword, newPassword });
|
||||||
}
|
}
|
||||||
@@ -157,7 +142,7 @@ const PasswordView = ({ user }: PasswordViewProps) => {
|
|||||||
value: mins,
|
value: mins,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const isDisabled = formMethods.formState.isSubmitting || !formMethods.formState.isDirty;
|
const isDisabled = formMethods.formState.isSubmitting || !formMethods.formState.isDirty || !formMethods.formState.isValid;
|
||||||
|
|
||||||
const passwordMinLength = data?.user.role === "USER" ? 7 : 15;
|
const passwordMinLength = data?.user.role === "USER" ? 7 : 15;
|
||||||
const isUser = data?.user.role === "USER";
|
const isUser = data?.user.role === "USER";
|
||||||
@@ -196,17 +181,23 @@ const PasswordView = ({ user }: PasswordViewProps) => {
|
|||||||
)}
|
)}
|
||||||
<div className="w-full sm:grid sm:grid-cols-2 sm:gap-x-6">
|
<div className="w-full sm:grid sm:grid-cols-2 sm:gap-x-6">
|
||||||
<div>
|
<div>
|
||||||
<PasswordField {...formMethods.register("oldPassword")} label={t("old_password")} />
|
<PasswordField
|
||||||
|
{...formMethods.register("oldPassword", {
|
||||||
|
required: t("error_required_field"),
|
||||||
|
})}
|
||||||
|
label={t("old_password")}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<PasswordField
|
<PasswordField
|
||||||
{...formMethods.register("newPassword", {
|
{...formMethods.register("newPassword", {
|
||||||
|
required: t("error_required_field"),
|
||||||
minLength: {
|
minLength: {
|
||||||
message: t(isUser ? "password_hint_min" : "password_hint_admin_min"),
|
message: t(isUser ? "password_hint_min" : "password_hint_admin_min"),
|
||||||
value: passwordMinLength,
|
value: passwordMinLength,
|
||||||
},
|
},
|
||||||
pattern: {
|
pattern: {
|
||||||
message: "Should contain a number, uppercase and lowercase letters",
|
message: t("password_complexity_requirement"),
|
||||||
value: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).*$/gm,
|
value: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).*$/gm,
|
||||||
},
|
},
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -689,6 +689,7 @@
|
|||||||
"password_hint_num": "Contain at least 1 number",
|
"password_hint_num": "Contain at least 1 number",
|
||||||
"max_limit_allowed_hint": "Must be {{limit}} or fewer characters long",
|
"max_limit_allowed_hint": "Must be {{limit}} or fewer characters long",
|
||||||
"invalid_password_hint": "The password must be a minimum of {{passwordLength}} characters long containing at least one number and have a mixture of uppercase and lowercase letters",
|
"invalid_password_hint": "The password must be a minimum of {{passwordLength}} characters long containing at least one number and have a mixture of uppercase and lowercase letters",
|
||||||
|
"password_complexity_requirement": "Should contain a number, uppercase and lowercase letters",
|
||||||
"incorrect_password": "Password is incorrect.",
|
"incorrect_password": "Password is incorrect.",
|
||||||
"incorrect_email_password": "Email or password is incorrect.",
|
"incorrect_email_password": "Email or password is incorrect.",
|
||||||
"compliance": "Compliance",
|
"compliance": "Compliance",
|
||||||
|
|||||||
Reference in New Issue
Block a user