Skip to content

Commit df75da1

Browse files
refactor: display error message instead of toast on error
1 parent be5c007 commit df75da1

4 files changed

Lines changed: 36 additions & 16 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"errors": {
33
"401": "The credentials you input are invalid.",
4+
"emailAlreadyExists": "This email already exists. Please sign in instead.",
5+
"invalidPassword": "Invalid password.",
46
"otherErrors": "Oops! Something went wrong."
57
}
68
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"errors": {
33
"401": "The credentials you input are invalid (fr).",
4+
"emailAlreadyExists": "This email already exists. Please sign in instead. (fr)",
5+
"invalidPassword": "Invalid password. (fr)",
46
"otherErrors": "Oops! Something went wrong (fr)."
57
}
68
}
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: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FormSubmitOptions } from "@dzangolab/react-form";
22
import { useTranslation } from "@dzangolab/react-i18n";
3-
import { AuthPage } from "@dzangolab/react-ui";
3+
import { AuthPage, Message } from "@dzangolab/react-ui";
44
import React, { useState } from "react";
55
import { toast } from "react-toastify";
66

@@ -18,35 +18,56 @@ 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<"invalidPassword" | "other" | null>(null);
2122

2223
const handleSubmit = async (
2324
data: ChangePasswordFormData,
2425
options?: FormSubmitOptions,
2526
) => {
2627
setLoading(true);
2728

28-
const success = await changePassword(
29+
const response = await changePassword(
2930
data.oldPassword,
3031
data.password,
3132
config.apiBaseUrl,
3233
);
3334

34-
if (success) {
35+
if (response.status === "OK") {
3536
toast.success(t("changePassword.messages.success"));
37+
3638
if (options && options.reset) {
3739
options.reset();
3840
}
41+
} else if (response.status === "INVALID_PASSWORD") {
42+
setError("invalidPassword");
43+
} else {
44+
setError("other");
3945
}
4046

4147
setLoading(false);
4248
};
4349

50+
const errorMessage =
51+
error === "invalidPassword"
52+
? t("errors.invalidPassword", { ns: "errors" })
53+
: t("errors.otherErrors", { ns: "errors" });
54+
4455
return (
4556
<AuthPage
4657
className="change-password"
4758
title={t("changePassword.title")}
4859
centered={centered}
4960
>
61+
{error && (
62+
<Message
63+
enableClose={true}
64+
message={errorMessage}
65+
onClose={() => {
66+
setError(null);
67+
}}
68+
severity="danger"
69+
/>
70+
)}
5071
<ChangePasswordForm handleSubmit={handleSubmit} loading={loading} />
5172
</AuthPage>
5273
);

0 commit comments

Comments
 (0)