contructeur classes, type DTO dans TS, clean hard code

This commit is contained in:
trochas
2026-01-08 15:42:11 +01:00
parent 78b82fcfee
commit f2a0f8ca86
7 changed files with 207 additions and 97 deletions

View File

@@ -1,42 +1,51 @@
import { useKeycloak } from '@react-keycloak/web'
import { useEffect } from 'react';
import { getUserTest, User } from '../classes';
import { Athlete, User } from '../classes';
import { useLocalData } from '../context/useLocalData';
import { postAthlete } from '../requetes';
export const Login =() =>{
const {user,setUser} = useLocalData()
const { keycloak } = useKeycloak();
useEffect(() => {
const syncAndLoadUser = async () => {
if (keycloak.authenticated && keycloak.token) {
const tokenParsed = keycloak.tokenParsed;
setUser({
id: 0,
keycloakId: tokenParsed!.sub!,
email: tokenParsed?.email || "",
nom: tokenParsed?.family_name || "",
prenom: tokenParsed?.given_name || "",
role: "athlete",
sessions: []
});
try {
const response = await fetch("http://localhost:8081/api/users/sync", {
method: "POST",
headers: {
"Authorization": `Bearer ${keycloak.token}`,
"Content-Type": "application/json"
},
});
console.log("Sync status:", response.status);
} catch (error) {
console.error("Sync fetch failed:", error);
}
}
};
const syncAndLoadUser = async () => {
const newAthlete:Athlete = new Athlete()
syncAndLoadUser();
}, [keycloak.authenticated, keycloak.token, setUser]);
const athlete:Athlete = await postAthlete(newAthlete)
setUser(athlete);
/*postAthlete
if (keycloak.authenticated && keycloak.token) {
const tokenParsed = keycloak.tokenParsed;
setUser(
{
id: 0,
keycloakId: tokenParsed!.sub!,
email: tokenParsed?.email || "",
nom: tokenParsed?.family_name || "",
prenom: tokenParsed?.given_name || "",
sessions: []
}
);
try {
const response = await fetch("http://localhost:8081/api/users/sync", {
method: "POST",
headers: {
"Authorization": `Bearer ${keycloak.token}`,
"Content-Type": "application/json"
},
});
console.log("Sync status:", response.status);
} catch (error) {
console.error("Sync fetch failed:", error);
}
}*/
};
syncAndLoadUser();
}, [keycloak.authenticated, keycloak.token, setUser]);
function handleLogin(): void {
@@ -61,9 +70,6 @@ export const Login =() =>{
<div>
User nom : { user.nom}
</div>
<div>
User role : { user.role}
</div>
</div>
}