contructeur classes, type DTO dans TS, clean hard code
This commit is contained in:
@@ -1,15 +1,7 @@
|
|||||||
export type Groupe = "Entrainement" | "Competition" | "Loisir"| "";
|
import { ActiviteDTO, AdminDTO, AthleteDTO, CoachDTO, SessionDTO } from "./classesDTO";
|
||||||
export type Role = "admin" | "athlete" | "coach";
|
|
||||||
|
export type Groupe = "Entrainement" | "Competition" | "Loisir"| "";
|
||||||
|
|
||||||
export class User{
|
|
||||||
id!: number;
|
|
||||||
keycloakId!: String;
|
|
||||||
nom!: string;
|
|
||||||
prenom!:string;
|
|
||||||
sessions: Session[] = []; //nb: Admin liaison non symétrique /!\
|
|
||||||
email!: string;
|
|
||||||
role!: Role;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Ligne{
|
export class Ligne{
|
||||||
id!: number;
|
id!: number;
|
||||||
@@ -18,32 +10,103 @@ export class Ligne{
|
|||||||
tempsDeJeu!: number; // en minutes
|
tempsDeJeu!: number; // en minutes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class User{
|
||||||
|
id!: number;
|
||||||
|
keycloakId!: String;
|
||||||
|
nom!: string;
|
||||||
|
prenom!:string;
|
||||||
|
email!: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class Admin extends User{
|
export class Admin extends User{
|
||||||
role!: Role;
|
|
||||||
|
constructor(dto:AdminDTO);
|
||||||
|
constructor();
|
||||||
|
|
||||||
|
constructor(dto?:AdminDTO){
|
||||||
|
super();
|
||||||
|
this.id = dto?.id ?? 0;
|
||||||
|
this.keycloakId = dto?.id_keycloak ?? "";
|
||||||
|
this.nom = dto?.name ?? "";
|
||||||
|
this.prenom = dto?.prenom ?? "";
|
||||||
|
this.email = ""; //TODO
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Athlete extends User{
|
export class Athlete extends User{
|
||||||
nom!: string;
|
groupes!: Groupe[];
|
||||||
groupe!: Groupe;
|
sessionsID!: number[];
|
||||||
role!: Role;
|
sessions: Session[] = [];
|
||||||
|
|
||||||
|
constructor(dto:AthleteDTO);
|
||||||
|
constructor();
|
||||||
|
|
||||||
|
constructor(dto?:AthleteDTO){
|
||||||
|
super();
|
||||||
|
this.id = dto?.id ?? 0;
|
||||||
|
this.keycloakId = dto?.id_keycloak ?? "";
|
||||||
|
this.nom = dto?.name ?? "";
|
||||||
|
this.prenom = dto?.prenom ?? "" ;
|
||||||
|
this.email = ""; //TODO
|
||||||
|
this.groupes = dto?.groupes ?? [];
|
||||||
|
this.sessionsID = dto?.sessionIds ?? [];
|
||||||
|
this.sessions = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Coach extends User{
|
export class Coach extends User{
|
||||||
nom!: string;
|
nom!: string;
|
||||||
role!: Role;
|
sessionsID!: number[];
|
||||||
|
sessions: Session[] = [];
|
||||||
|
|
||||||
|
constructor(dto:CoachDTO);
|
||||||
|
constructor();
|
||||||
|
|
||||||
|
constructor(dto?:CoachDTO){
|
||||||
|
super();
|
||||||
|
this.id = dto?.id ?? 0;
|
||||||
|
this.keycloakId = dto?.id_keycloak ?? "";
|
||||||
|
this.nom = dto?.name ?? "";
|
||||||
|
this.prenom = dto?.prenom ?? "";
|
||||||
|
this.email = ""; //TODO
|
||||||
|
this.sessionsID = dto?.sessionIds ?? [];
|
||||||
|
this.sessions = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Session{
|
export class Session{
|
||||||
id!: number;
|
id!: number;
|
||||||
name!: string;
|
name!: string;
|
||||||
|
activitesID: number[] = [];
|
||||||
activites: Activite[] = [];
|
activites: Activite[] = [];
|
||||||
isRecurrent! : Boolean;
|
isRecurrent! : Boolean;
|
||||||
creneau!: Date;
|
creneau!: Date;
|
||||||
coach!: Coach;
|
coach!: Coach;
|
||||||
|
athletesID!: number[]
|
||||||
athletes!: Athlete[]
|
athletes!: Athlete[]
|
||||||
duree! : number;
|
duree! : number;
|
||||||
groupe! : Groupe;
|
groupe! : Groupe;
|
||||||
ligne! : Ligne[];
|
ligne! : Ligne[];
|
||||||
|
|
||||||
|
constructor(dto:SessionDTO);
|
||||||
|
constructor();
|
||||||
|
|
||||||
|
constructor(dto?:SessionDTO){
|
||||||
|
this.id = dto?.id?? 0;
|
||||||
|
this.name = dto?.name?? "";
|
||||||
|
this.activitesID = dto?.activiteIds?? [];
|
||||||
|
this.activites = [];
|
||||||
|
this.isRecurrent = dto?.isRecurrent?? false;
|
||||||
|
this.creneau = dto? new Date(dto.creneau) : new Date();
|
||||||
|
//this.coach = new Coach(); //dto.coachId; //TODO
|
||||||
|
this.athletesID = dto?.athleteIds ?? [];
|
||||||
|
this.athletes = [];
|
||||||
|
this.duree = dto?.duree ?? 0;
|
||||||
|
this.groupe = dto?.groupe ?? "";
|
||||||
|
this.ligne = []; //TODO
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Activite{
|
export class Activite{
|
||||||
@@ -54,10 +117,23 @@ export class Activite{
|
|||||||
data!: Map<string,string>;
|
data!: Map<string,string>;
|
||||||
duree!: number;
|
duree!: number;
|
||||||
|
|
||||||
|
constructor(dto:ActiviteDTO);
|
||||||
|
constructor();
|
||||||
|
|
||||||
|
constructor(dto?:ActiviteDTO){
|
||||||
|
this.id = dto?.id ?? 0;
|
||||||
|
this.nom = dto?.name ?? "";
|
||||||
|
//this.session = dto.sessionId; //TODO
|
||||||
|
this.theme = dto?.theme ?? "";
|
||||||
|
//this.data = //TODO
|
||||||
|
this.duree = dto?.duree ?? 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
export function getUserTest():User{
|
export function getUserTest():User{
|
||||||
const user = new User();
|
const user = new Athlete();
|
||||||
const s1 = new Session();
|
const s1 = new Session();
|
||||||
const s2 = new Session();
|
const s2 = new Session();
|
||||||
const s3 = new Session();
|
const s3 = new Session();
|
||||||
@@ -65,17 +141,17 @@ export function getUserTest():User{
|
|||||||
athlete1.id = 1;
|
athlete1.id = 1;
|
||||||
athlete1.nom = "Alice Dupont";
|
athlete1.nom = "Alice Dupont";
|
||||||
athlete1.email = "alice@nootnoot.yee";
|
athlete1.email = "alice@nootnoot.yee";
|
||||||
athlete1.groupe = "Entrainement";
|
athlete1.groupes = ["Entrainement"];
|
||||||
|
|
||||||
const athlete2 = new Athlete();
|
const athlete2 = new Athlete();
|
||||||
athlete2.id = 2;
|
athlete2.id = 2;
|
||||||
athlete2.nom = "Bob Martin";
|
athlete2.nom = "Bob Martin";
|
||||||
athlete2.groupe = "Competition";
|
athlete2.groupes = ["Competition"];
|
||||||
|
|
||||||
const athlete3 = new Athlete();
|
const athlete3 = new Athlete();
|
||||||
athlete3.id = 3;
|
athlete3.id = 3;
|
||||||
athlete3.nom = "Clara Lopez";
|
athlete3.nom = "Clara Lopez";
|
||||||
athlete3.groupe = "Loisir";
|
athlete3.groupes = ["Loisir"];
|
||||||
|
|
||||||
const ligne1 = new Ligne();
|
const ligne1 = new Ligne();
|
||||||
ligne1.id = 1;
|
ligne1.id = 1;
|
||||||
@@ -99,7 +175,6 @@ export function getUserTest():User{
|
|||||||
|
|
||||||
user.id = 0;
|
user.id = 0;
|
||||||
user.nom = "Emilien-Yee NootNoot";
|
user.nom = "Emilien-Yee NootNoot";
|
||||||
user.role = "coach"
|
|
||||||
user.email = "emilien@nootnoot.yee";
|
user.email = "emilien@nootnoot.yee";
|
||||||
s1.creneau = new Date();
|
s1.creneau = new Date();
|
||||||
s1.id = 1;
|
s1.id = 1;
|
||||||
@@ -187,9 +262,7 @@ export function getUserTest():User{
|
|||||||
user.sessions.push(s2);
|
user.sessions.push(s2);
|
||||||
user.sessions.push(s3);
|
user.sessions.push(s3);
|
||||||
|
|
||||||
athlete1.role = "athlete";
|
|
||||||
athlete2.role = "athlete";
|
|
||||||
athlete3.role = "athlete";
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
50
front_end/src/classesDTO.tsx
Normal file
50
front_end/src/classesDTO.tsx
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { Groupe } from "./classes";
|
||||||
|
|
||||||
|
export type ActiviteDTO = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
theme: string;
|
||||||
|
duree: number;
|
||||||
|
dataActivite: string[];
|
||||||
|
sessionId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AdminDTO = {
|
||||||
|
id: number;
|
||||||
|
id_keycloak: string;
|
||||||
|
name: string;
|
||||||
|
prenom: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AthleteDTO = {
|
||||||
|
id:number;
|
||||||
|
id_keycloak: string;
|
||||||
|
name: string;
|
||||||
|
prenom: string;
|
||||||
|
categorie: string;
|
||||||
|
niveau: string;
|
||||||
|
groupes: Groupe[];
|
||||||
|
sessionIds: number[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CoachDTO = {
|
||||||
|
id: number;
|
||||||
|
id_keycloak: string;
|
||||||
|
name: string;
|
||||||
|
prenom: string;
|
||||||
|
sessionIds: number[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SessionDTO = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
isRecurrent: boolean;
|
||||||
|
creneau: string; // LocalDateTime → ISO string
|
||||||
|
duree: number;
|
||||||
|
groupe: Groupe;
|
||||||
|
|
||||||
|
coachId: number;
|
||||||
|
athleteIds: number[];
|
||||||
|
activiteIds: number[];
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import { useKeycloak } from '@react-keycloak/web'
|
import { useKeycloak } from '@react-keycloak/web'
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { getUserTest, User } from '../classes';
|
import { Athlete, User } from '../classes';
|
||||||
import { useLocalData } from '../context/useLocalData';
|
import { useLocalData } from '../context/useLocalData';
|
||||||
|
import { postAthlete } from '../requetes';
|
||||||
|
|
||||||
export const Login =() =>{
|
export const Login =() =>{
|
||||||
const {user,setUser} = useLocalData()
|
const {user,setUser} = useLocalData()
|
||||||
@@ -9,17 +10,25 @@ export const Login =() =>{
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const syncAndLoadUser = async () => {
|
const syncAndLoadUser = async () => {
|
||||||
|
const newAthlete:Athlete = new Athlete()
|
||||||
|
|
||||||
|
const athlete:Athlete = await postAthlete(newAthlete)
|
||||||
|
|
||||||
|
setUser(athlete);
|
||||||
|
/*postAthlete
|
||||||
if (keycloak.authenticated && keycloak.token) {
|
if (keycloak.authenticated && keycloak.token) {
|
||||||
const tokenParsed = keycloak.tokenParsed;
|
const tokenParsed = keycloak.tokenParsed;
|
||||||
setUser({
|
setUser(
|
||||||
|
|
||||||
|
{
|
||||||
id: 0,
|
id: 0,
|
||||||
keycloakId: tokenParsed!.sub!,
|
keycloakId: tokenParsed!.sub!,
|
||||||
email: tokenParsed?.email || "",
|
email: tokenParsed?.email || "",
|
||||||
nom: tokenParsed?.family_name || "",
|
nom: tokenParsed?.family_name || "",
|
||||||
prenom: tokenParsed?.given_name || "",
|
prenom: tokenParsed?.given_name || "",
|
||||||
role: "athlete",
|
|
||||||
sessions: []
|
sessions: []
|
||||||
});
|
}
|
||||||
|
);
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://localhost:8081/api/users/sync", {
|
const response = await fetch("http://localhost:8081/api/users/sync", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -32,7 +41,7 @@ export const Login =() =>{
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Sync fetch failed:", error);
|
console.error("Sync fetch failed:", error);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
};
|
};
|
||||||
|
|
||||||
syncAndLoadUser();
|
syncAndLoadUser();
|
||||||
@@ -61,9 +70,6 @@ export const Login =() =>{
|
|||||||
<div>
|
<div>
|
||||||
User nom : { user.nom}
|
User nom : { user.nom}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
|
||||||
User role : { user.role}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,20 +101,18 @@ function ObjectUser({admin=null,athlete=null,coach=null}:Props){
|
|||||||
return(
|
return(
|
||||||
<div>
|
<div>
|
||||||
<div className="object" onClick={() => handleOpen()}>
|
<div className="object" onClick={() => handleOpen()}>
|
||||||
<div>{user2.nom} ({user2.role})</div>
|
<div>{user2.nom}</div>
|
||||||
|
|
||||||
{/* <div>{user2.role}</div> */}
|
{/* <div>{user2.role}</div> */}
|
||||||
</div>
|
</div>
|
||||||
{open &&
|
{open &&
|
||||||
<Modal isOpen={open} onClose={() => setOpen(false)}>
|
<Modal isOpen={open} onClose={() => setOpen(false)}>
|
||||||
<div className="object_modal">
|
<div className="object_modal">
|
||||||
<div>{user2.nom} ({user2.role})</div>
|
<div>{user2.nom}</div>
|
||||||
<div>{user2.email}</div>
|
<div>{user2.email}</div>
|
||||||
<div className='list_object'>
|
<div className='list_object'>
|
||||||
<div>Sessions :</div>
|
<div>Sessions :</div>
|
||||||
{user.sessions.map((session,index)=>(
|
{/* TODO */}
|
||||||
<ObjectSession session={session}/>
|
|
||||||
))}
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ function AthleteList({ athletes, sessions }: AthleteListProps) {
|
|||||||
return (
|
return (
|
||||||
<li key={a.id}>
|
<li key={a.id}>
|
||||||
<div><strong>Nom:</strong> {a.nom}</div>
|
<div><strong>Nom:</strong> {a.nom}</div>
|
||||||
<div><strong>Groupe:</strong> {a.groupe}</div>
|
<div><strong>Groupe:</strong> {a.groupes}</div>
|
||||||
<div><strong>Nombre de sessions:</strong> {stats.nbSessions}</div>
|
<div><strong>Nombre de sessions:</strong> {stats.nbSessions}</div>
|
||||||
<div><strong>Sessions/semaine:</strong> {stats.nbSessionsPerWeek.toFixed(2)}</div>
|
<div><strong>Sessions/semaine:</strong> {stats.nbSessionsPerWeek.toFixed(2)}</div>
|
||||||
<div><strong>Alerte:</strong> {alerte}</div>
|
<div><strong>Alerte:</strong> {alerte}</div>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useLocalData } from "../context/useLocalData";
|
import { useLocalData } from "../context/useLocalData";
|
||||||
import { Activite, Athlete, Coach , Session, Ligne, getUserTest } from "../classes";
|
import { Activite, Athlete, Coach , Session, Ligne, Admin } from "../classes";
|
||||||
import {calculTempsDeJeuParLigne} from "../utils/ligneUtils";
|
import {calculTempsDeJeuParLigne} from "../utils/ligneUtils";
|
||||||
import { keyboard } from "@testing-library/user-event/dist/keyboard";
|
import { keyboard } from "@testing-library/user-event/dist/keyboard";
|
||||||
import ObjectSession from "./object/session";
|
import ObjectSession from "./object/session";
|
||||||
@@ -10,14 +10,11 @@ import ObjectUser from "./object/user";
|
|||||||
export type keyWord = "athletes" | "activites" | "coachs" | "sessions"| "lignes";
|
export type keyWord = "athletes" | "activites" | "coachs" | "sessions"| "lignes";
|
||||||
|
|
||||||
export default function RessourcePanel() {
|
export default function RessourcePanel() {
|
||||||
//const { user } = useLocalData();
|
const { user } = useLocalData();
|
||||||
const user = getUserTest(); //TODO
|
//const user = getUserTest(); //TODO
|
||||||
const [value,setValue] = useState<keyWord>("athletes");
|
const [value,setValue] = useState<keyWord>("athletes");
|
||||||
console.log("Rôle utilisateur:", user.role);
|
|
||||||
console.log(user.nom);
|
if (user instanceof Athlete) return null;
|
||||||
console.log(user.prenom);
|
|
||||||
console.log(user.email);
|
|
||||||
if (user.role === "athlete") return null;
|
|
||||||
|
|
||||||
|
|
||||||
const allAthletes: Athlete[] = [];
|
const allAthletes: Athlete[] = [];
|
||||||
@@ -28,26 +25,8 @@ import ObjectUser from "./object/user";
|
|||||||
|
|
||||||
const allSessions: Session[] = [];
|
const allSessions: Session[] = [];
|
||||||
|
|
||||||
const ligneMap: Map<number, Ligne> = new Map();
|
const allLignes: Ligne[] = [];
|
||||||
user.sessions.forEach(session => {
|
|
||||||
if (session.ligne) {
|
|
||||||
|
|
||||||
session.ligne.forEach(ligne => {
|
|
||||||
ligneMap.set(ligne.id, ligne);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
const allLignes: Ligne[] = Array.from(ligneMap.values());
|
|
||||||
|
|
||||||
// Calculer le temps de jeu pour chaque ligne
|
|
||||||
const tempsDeJeuParLigne: Map<number, number> = new Map();
|
|
||||||
allLignes.forEach(ligne => {
|
|
||||||
const tempsTotal = calculTempsDeJeuParLigne(allSessions, ligne);
|
|
||||||
tempsDeJeuParLigne.set(ligne.id, tempsTotal);
|
|
||||||
ligne.tempsDeJeu = tempsTotal;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +42,7 @@ import ObjectUser from "./object/user";
|
|||||||
}}>
|
}}>
|
||||||
<option value="athletes">Athlètes</option>
|
<option value="athletes">Athlètes</option>
|
||||||
<option value="activites">Activités</option>
|
<option value="activites">Activités</option>
|
||||||
{user.role === "admin" && <option value="coachs"> Coachs</option>}
|
{user instanceof Admin && <option value="coachs"> Coachs</option>}
|
||||||
<option value="sessions"> Sessions</option>
|
<option value="sessions"> Sessions</option>
|
||||||
<option value="lignes"> Lignes</option>
|
<option value="lignes"> Lignes</option>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import api, { activiteService, sessionService } from "./api";
|
import api, { activiteService, sessionService } from "./api";
|
||||||
import { Activite, Admin, Athlete, Coach, Session, User } from "./classes";
|
import { Activite, Admin, Athlete, Coach, Session, User } from "./classes";
|
||||||
import Keycloak from 'keycloak-js'
|
import Keycloak from 'keycloak-js'
|
||||||
|
import { AdminDTO, AthleteDTO, CoachDTO } from "./classesDTO";
|
||||||
|
|
||||||
//debug:
|
//debug:
|
||||||
export function delay(ms: number): Promise<void> {
|
export function delay(ms: number): Promise<void> {
|
||||||
@@ -19,16 +20,19 @@ export async function getUser(keycloak:Keycloak): Promise<User|null>{
|
|||||||
const roles = keycloak.tokenParsed?.realm_access?.roles
|
const roles = keycloak.tokenParsed?.realm_access?.roles
|
||||||
if(roles!=null){
|
if(roles!=null){
|
||||||
if(roles.includes("admin")){
|
if(roles.includes("admin")){
|
||||||
const response = await api.get<any>(`/TODO`);
|
const response = await api.get<AdminDTO>(`/TODO`);
|
||||||
return response.data;
|
const admin = new Admin(response.data);
|
||||||
|
return admin;
|
||||||
}
|
}
|
||||||
else if(roles.includes("coach")){
|
else if(roles.includes("coach")){
|
||||||
const response = await api.get<any>(`/TODO`);
|
const response = await api.get<CoachDTO>(`/TODO`);
|
||||||
return response.data;
|
const coach = new Coach(response.data);
|
||||||
|
return coach;
|
||||||
}
|
}
|
||||||
else if(roles.includes("athletes")){
|
else if(roles.includes("athletes")){
|
||||||
const response = await api.get<any>(`/TODO`);
|
const response = await api.get<AthleteDTO>(`/TODO`);
|
||||||
return response.data;
|
const athlete = new Athlete(response.data);
|
||||||
|
return athlete;
|
||||||
}
|
}
|
||||||
console.error("Error roles inconnu");
|
console.error("Error roles inconnu");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user