Skip to content

Commit ed3e3f8

Browse files
refactor(user): display error message instead of toast message on password change form (#1537)
* refactor: display error message instead of toast on error * refactor: change old password field name to current password
1 parent e409841 commit ed3e3f8

8 files changed

Lines changed: 53 additions & 35 deletions

File tree

packages/i18n/src/locales/en/user.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
"newPassword": {
1111
"label": "New password"
1212
},
13-
"oldPassword": {
14-
"label": "Old password"
13+
"currentPassword": {
14+
"label": "Current password"
1515
}
1616
},
1717
"messages": {
1818
"success": "You have successfully changed your password.",
1919
"validation": {
2020
"confirmPassword": "Confirm password.",
21+
"currentPassword": "Current password is required.",
2122
"mustMatch": "Passwords must match.",
22-
"newPassword": "New password is required.",
23-
"oldPassword": "Old password is required."
23+
"newPassword": "New password is required."
2424
}
2525
},
2626
"title": "Change password"

packages/i18n/src/locales/fr/user.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
"confirmPassword": {
88
"label": "Confirm password (fr)"
99
},
10+
"currentPassword": {
11+
"label": "Current password (fr)"
12+
},
1013
"newPassword": {
1114
"label": "New password (fr)"
12-
},
13-
"oldPassword": {
14-
"label": "Old password (fr)"
1515
}
1616
},
1717
"messages": {
1818
"success": "You have successfully changed your password. (fr)",
1919
"validation": {
2020
"confirmPassword": "Confirm password is required. (fr)",
21+
"currentPassword": "Current password is required. (fr)",
2122
"mustMatch": "Passwords must match. (fr)",
22-
"newPassword": "New password is required. (fr)",
23-
"oldPassword": "Old password is required. (fr)"
23+
"newPassword": "New password is required. (fr)"
2424
}
2525
},
2626
"title": "Change password (fr)"

packages/user/src/components/ChangePasswordForm/ChangePasswordFormFields.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const ChangePasswordFormFields = ({ loading }: Properties) => {
2424
<>
2525
<Password
2626
autoComplete="current-password"
27-
label={t("changePassword.form.oldPassword.label")}
27+
label={t("changePassword.form.currentPassword.label")}
2828
name="oldPassword"
2929
register={register}
3030
getFieldState={getFieldState}

packages/user/src/components/ChangePasswordForm/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const ChangePasswordForm = ({ handleSubmit, loading }: Properties) => {
2323
.object({
2424
oldPassword: zod
2525
.string()
26-
.nonempty(t("changePassword.messages.validation.oldPassword")),
26+
.nonempty(t("changePassword.messages.validation.currentPassword")),
2727
...PasswordConfirmationSchema({
2828
passwordValidationMessage: t(
2929
"changePassword.messages.validation.mustContain",

packages/user/src/components/__test__/ChangePasswordForm.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test("validation error message is displayed when input field is empty", async ()
2424

2525
await waitFor(() => {
2626
expect(
27-
screen.getByText("changePassword.messages.validation.oldPassword"),
27+
screen.getByText("changePassword.messages.validation.currentPassword"),
2828
).toBeDefined();
2929
});
3030
await waitFor(() => {
@@ -45,8 +45,8 @@ test("validation error message is displayed for unmatched confirm password", asy
4545
const handleSubmit = vi.fn();
4646
const { user } = setup(<ChangePasswordForm handleSubmit={handleSubmit} />);
4747

48-
const oldPassword = screen.getByLabelText(
49-
"changePassword.form.oldPassword.label",
48+
const currentPassword = screen.getByLabelText(
49+
"changePassword.form.currentPassword.label",
5050
);
5151
const newPassword = screen.getByLabelText(
5252
"changePassword.form.newPassword.label",
@@ -57,7 +57,7 @@ test("validation error message is displayed for unmatched confirm password", asy
5757
const submitButton = screen.getByText("changePassword.form.actions.submit");
5858

5959
await act(async () => {
60-
await user.type(oldPassword, "Test@123");
60+
await user.type(currentPassword, "Test@123");
6161
await user.type(newPassword, "Test@12345");
6262
await user.type(confirmPassword, "Test@12");
6363
});
@@ -81,8 +81,8 @@ test("form is successfully submitted", async () => {
8181
const handleSubmit = vi.fn();
8282
const { user } = setup(<ChangePasswordForm handleSubmit={handleSubmit} />);
8383

84-
const oldPassword = screen.getByLabelText(
85-
"changePassword.form.oldPassword.label",
84+
const currentPassword = screen.getByLabelText(
85+
"changePassword.form.currentPassword.label",
8686
);
8787
const newPassword = screen.getByLabelText(
8888
"changePassword.form.newPassword.label",
@@ -93,7 +93,7 @@ test("form is successfully submitted", async () => {
9393
const submitButton = screen.getByText("changePassword.form.actions.submit");
9494

9595
await act(async () => {
96-
await user.type(oldPassword, "Test@123");
96+
await user.type(currentPassword, "Test@123");
9797
await user.type(newPassword, "Test@12345");
9898
await user.type(confirmPassword, "Test@12345");
9999

packages/user/src/components/__test__/__snapshots__/ChangePasswordForm.snapshot.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports[`Component matches snapshot 1`] = `
1212
<label
1313
for="oldPassword"
1414
>
15-
changePassword.form.oldPassword.label
15+
changePassword.form.currentPassword.label
1616
</label>
1717
<div
1818
class="input-field-password"
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import { toast } from "react-toastify";
2-
31
import client from "@/api/axios";
42

53
export const changePassword = async (
64
oldPassword: string,
75
newPassword: string,
86
apiBaseUrl: string,
9-
): Promise<boolean | undefined> => {
10-
let success = false;
11-
7+
) => {
128
try {
139
const response = await client(apiBaseUrl).post(
1410
"/change_password",
@@ -18,18 +14,17 @@ export const changePassword = async (
1814
},
1915
);
2016

21-
if (response.data.status === "OK") {
22-
success = true;
23-
} else {
24-
toast.error(response.data.message);
25-
}
17+
return response.data;
2618
} catch (err) {
2719
let errorMessage = "Oops! Something went wrong.";
20+
2821
if (err instanceof Error) {
2922
errorMessage = err.message;
3023
}
31-
toast.error(errorMessage);
32-
}
3324

34-
return success;
25+
return {
26+
status: "ERROR",
27+
message: errorMessage,
28+
};
29+
}
3530
};

packages/user/src/views/ChangePassword.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FormSubmitOptions } from "@prefabs.tech/react-form";
22
import { useTranslation } from "@prefabs.tech/react-i18n";
3-
import { AuthPage } from "@prefabs.tech/react-ui";
3+
import { AuthPage, Message } from "@prefabs.tech/react-ui";
44
import React, { useState } from "react";
55
import { toast } from "react-toastify";
66

@@ -18,35 +18,58 @@ export const ChangePassword = ({ centered = true }: { centered?: boolean }) => {
1818
const { t } = useTranslation("user");
1919
const config = useConfig();
2020
const [loading, setLoading] = useState<boolean>(false);
21+
const [error, setError] = useState<"incorrectPassword" | "other" | null>(
22+
null,
23+
);
2124

2225
const handleSubmit = async (
2326
data: ChangePasswordFormData,
2427
options?: FormSubmitOptions,
2528
) => {
2629
setLoading(true);
2730

28-
const success = await changePassword(
31+
const response = await changePassword(
2932
data.oldPassword,
3033
data.password,
3134
config.apiBaseUrl,
3235
);
3336

34-
if (success) {
37+
if (response.status === "OK") {
3538
toast.success(t("changePassword.messages.success"));
39+
3640
if (options && options.reset) {
3741
options.reset();
3842
}
43+
} else if (response.status === "INVALID_PASSWORD") {
44+
setError("incorrectPassword");
45+
} else {
46+
setError("other");
3947
}
4048

4149
setLoading(false);
4250
};
4351

52+
const errorMessage =
53+
error === "incorrectPassword"
54+
? t("errors.incorrectPassword", { ns: "errors" })
55+
: t("errors.otherErrors", { ns: "errors" });
56+
4457
return (
4558
<AuthPage
4659
className="change-password"
4760
title={t("changePassword.title")}
4861
centered={centered}
4962
>
63+
{error && (
64+
<Message
65+
enableClose={true}
66+
message={errorMessage}
67+
onClose={() => {
68+
setError(null);
69+
}}
70+
severity="danger"
71+
/>
72+
)}
5073
<ChangePasswordForm handleSubmit={handleSubmit} loading={loading} />
5174
</AuthPage>
5275
);

0 commit comments

Comments
 (0)