Skip to content

Commit 6e409a7

Browse files
refactor: dynamic translation logic
1 parent a6de63e commit 6e409a7

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"errors": {
3-
"401": "The credentials you input are invalid.",
3+
"401": "Invalid credentials. Please check your email or password and try again.",
4+
"emailAlreadyExists": "This email already exists. Please sign in instead.",
45
"otherErrors": "Oops! Something went wrong."
56
}
67
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"errors": {
3-
"401": "The credentials you input are invalid (fr).",
3+
"401": "Invalid credentials. Please check your email or password and try again. (fr)",
4+
"emailAlreadyExists": "This email already exists. Please sign in instead. (fr)",
45
"otherErrors": "Oops! Something went wrong (fr)."
56
}
67
}

packages/user/src/components/Login/LoginWrapper.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export const LoginWrapper: FC<IProperties> = ({
3535
const { setUser } = useUser();
3636
const config = useConfig();
3737
const [loginLoading, setLoginLoading] = useState<boolean>(false);
38-
const [loginError, setLoginError] = useState<string | null>(null);
38+
const [loginError, setLoginError] = useState(false);
39+
const [loginErrorKey, setLoginErrorKey] = useState<string | null>(null);
3940

4041
const links: Array<LinkType> = [
4142
{
@@ -72,11 +73,10 @@ export const LoginWrapper: FC<IProperties> = ({
7273
}
7374
})
7475
.catch(async (error) => {
75-
const errorMessage = `errors.${error.message}`;
76-
7776
onLoginFailed && (await onLoginFailed(error));
7877

79-
setLoginError(t(errorMessage, { ns: "errors" }));
78+
setLoginError(true);
79+
setLoginErrorKey(`errors.${error.message}`);
8080
});
8181

8282
setLoginLoading(false);
@@ -85,11 +85,14 @@ export const LoginWrapper: FC<IProperties> = ({
8585

8686
return (
8787
<>
88-
{loginError && (
88+
{loginError && loginErrorKey && (
8989
<Message
9090
enableClose={true}
91-
message={loginError}
92-
onClose={() => setLoginError(null)}
91+
message={t(loginErrorKey, { ns: "errors" })}
92+
onClose={() => {
93+
setLoginError(false);
94+
setLoginErrorKey(null);
95+
}}
9396
severity="danger"
9497
/>
9598
)}

packages/user/src/components/Signup/SignupWrapper.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const SignupWrapper: React.FC<IProperties> = ({
3434
}) => {
3535
const { t } = useTranslation("user");
3636
const [signupLoading, setSignupLoading] = useState<boolean>(false);
37-
const [signupError, setSignupError] = useState<string | null>(null);
37+
const [signupError, setSignupError] = useState(false);
38+
const [signupErrorKey, setSignupErrorKey] = useState<string | null>(null);
3839

3940
const { setUser } = useUser();
4041
const config = useConfig();
@@ -89,13 +90,15 @@ export const SignupWrapper: React.FC<IProperties> = ({
8990
.catch(async (error) => {
9091
onSignupFailed && (await onSignupFailed(error));
9192

93+
setSignupError(true);
94+
9295
if (error.message.includes("email already exists")) {
93-
setSignupError(t("errors.emailAlreadyExists", { ns: "errors" }));
96+
setSignupErrorKey("errors.emailAlreadyExists");
9497

9598
return;
9699
}
97100

98-
setSignupError(t("errors.otherErrors", { ns: "errors" }));
101+
setSignupErrorKey("errors.otherErrors");
99102
})
100103
.finally(() => {
101104
setSignupLoading(false);
@@ -105,11 +108,14 @@ export const SignupWrapper: React.FC<IProperties> = ({
105108

106109
return (
107110
<>
108-
{signupError && (
111+
{signupError && signupErrorKey && (
109112
<Message
110113
enableClose={true}
111-
message={signupError}
112-
onClose={() => setSignupError(null)}
114+
message={t(signupErrorKey, { ns: "errors" })}
115+
onClose={() => {
116+
setSignupError(false);
117+
setSignupErrorKey(null);
118+
}}
113119
severity="danger"
114120
/>
115121
)}

0 commit comments

Comments
 (0)