Skip to content

Commit b84fadd

Browse files
committed
wip: Add API response localization and update form submission handling in AddNewUser component
1 parent 7ad4ca0 commit b84fadd

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

client/src/locales/base/ar-SA.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"name": "اسم",
4747
"please-enter-a-oauth-id": "الرجاء إدخال معرف Oauth 2.0 (sub)",
4848
"oauth-id": "OAuth 2.0 ID (sub)",
49-
"enter-the-oauth-id": "أدخل معرف Oauth 2.0 (sub)"
49+
"enter-the-oauth-id": "أدخل معرف Oauth 2.0 (sub)",
50+
"api-response": "استجابة API"
5051
}

client/src/locales/base/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"name": "Name",
4747
"please-enter-a-oauth-id": "Please enter an OAuth 2.0 id (sub)",
4848
"oauth-id": "OAuth 2.0 id (sub)",
49-
"enter-the-oauth-id": "Enter the OAuth 2.0 id (sub)"
49+
"enter-the-oauth-id": "Enter the OAuth 2.0 id (sub)",
50+
"api-response": "API Response"
5051
}

client/src/locales/base/es-ES.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"name": "Nombre",
4747
"please-enter-a-oauth-id": "Ingrese una ID de OAuth 2.0 (sub)",
4848
"oauth-id": "OAuth 2.0 ID (sub)",
49-
"enter-the-oauth-id": "Ingrese la ID de OAuth 2.0 (sub)"
49+
"enter-the-oauth-id": "Ingrese la ID de OAuth 2.0 (sub)",
50+
"api-response": "Respuesta de la API"
5051
}

client/src/locales/base/fr-FR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"name": "Nom",
4747
"please-enter-a-oauth-id": "Veuillez saisir un ID OAuth 2.0 (sub)",
4848
"oauth-id": "OAuth 2.0 ID (sub)",
49-
"enter-the-oauth-id": "Entrez l'id OAuth 2.0 (sub)"
49+
"enter-the-oauth-id": "Entrez l'id OAuth 2.0 (sub)",
50+
"api-response": "Réponse de l'API"
5051
}

client/src/locales/base/he-IL.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"name": "שֵׁם",
4747
"please-enter-a-oauth-id": "אנא הזן מזהה OAuth 2.0 (sub)",
4848
"oauth-id": "OAuth 2.0 ID (sub)",
49-
"enter-the-oauth-id": "הזן את מזהה OAuth 2.0 (Sub)"
49+
"enter-the-oauth-id": "הזן את מזהה OAuth 2.0 (Sub)",
50+
"api-response": "תגובת API"
5051
}

client/src/locales/base/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
"name": "姓名",
4747
"please-enter-a-oauth-id": "请输入OAuth 2.0 ID(sub)",
4848
"oauth-id": "OAuth 2.0 ID(sub)",
49-
"enter-the-oauth-id": "输入OAuth 2.0 ID(sub)"
49+
"enter-the-oauth-id": "输入OAuth 2.0 ID(sub)",
50+
"api-response": "API响应"
5051
}

client/src/pages/add-new-user.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,33 @@ import { Input } from "@heroui/input";
77

88
import { title } from "@/components/primitives";
99
import DefaultLayout from "@/layouts/default";
10-
import { userHasPermission } from "@/components/auth0";
10+
import { postJsonToSecuredApi, userHasPermission } from "@/components/auth0";
1111

1212
export default function AddNewUser() {
1313
const { t } = useTranslation();
1414
const { isAuthenticated, getAccessTokenSilently } = useAuth0();
1515

1616
const [hasPermission, setHasPermission] = useState(false);
17-
const [submitted, setSubmitted] = useState(
17+
const [apiResponse, setApiResponse] = useState(
1818
null as {
1919
[k: string]: FormDataEntryValue;
2020
} | null,
2121
);
2222

23-
const onSubmit = (e: FormEvent<HTMLFormElement>) => {
23+
const onSubmit = async (e: FormEvent<HTMLFormElement>) => {
2424
e.preventDefault();
2525

2626
const data = Object.fromEntries(new FormData(e.currentTarget));
27+
const ids = [data["oauthId"] as string];
28+
const name = data["name"] as string;
2729

28-
setSubmitted(data);
30+
const apiResponse = await postJsonToSecuredApi(
31+
`${import.meta.env.API_BASE_URL}/tester`,
32+
{ name, ids },
33+
getAccessTokenSilently,
34+
);
35+
36+
setApiResponse(apiResponse);
2937
};
3038

3139
useEffect(() => {
@@ -38,7 +46,9 @@ export default function AddNewUser() {
3846
getAccessTokenSilently,
3947
),
4048
);
41-
console.log("You have permission to add a new user.");
49+
console.log(
50+
`You have ${hasPermission ? "" : "not "}permission to add a new user.`,
51+
);
4252
} catch (error) {
4353
console.log(error as Error);
4454
}
@@ -60,28 +70,29 @@ export default function AddNewUser() {
6070
<Form className="w-full max-w-xs" onSubmit={onSubmit}>
6171
<Input
6272
isRequired
63-
errorMessage={t('please-enter-a-name')}
64-
label={t('name')}
73+
errorMessage={t("please-enter-a-name")}
74+
label={t("name")}
6575
labelPlacement="outside"
6676
name="name"
67-
placeholder={t('enter-the-user-name')}
77+
placeholder={t("enter-the-user-name")}
6878
type="text"
6979
/>
7080
<Input
7181
isRequired
72-
errorMessage={t('please-enter-a-oauth-id')}
73-
label={t('oauth-id')}
82+
errorMessage={t("please-enter-a-oauth-id")}
83+
label={t("oauth-id")}
7484
labelPlacement="outside"
7585
name="oauthId"
76-
placeholder={t('enter-the-oauth-id')}
86+
pattern="^[a-zA-Z0-9]{4,30}\|[a-zA-Z0-9]{4,30}$"
87+
placeholder={t("enter-the-oauth-id")}
7788
type="text"
7889
/>
7990
<Button type="submit" variant="bordered">
80-
{t('submit')}
91+
{t("submit")}
8192
</Button>
82-
{submitted && (
93+
{apiResponse && (
8394
<div className="text-small text-default-500">
84-
You submitted: <code>{JSON.stringify(submitted)}</code>
95+
{t('api-response')}: <code>{JSON.stringify(apiResponse)}</code>
8596
</div>
8697
)}
8798
</Form>

0 commit comments

Comments
 (0)