150 lines
4.0 KiB
TypeScript
150 lines
4.0 KiB
TypeScript
import api from "./api";
|
|
import { Activite, Athlete, Coach, Session, User } from "./classes";
|
|
//import { useKeycloak } from '@react-keycloak/web'
|
|
|
|
import { useKeycloak } from '@react-keycloak/web'
|
|
import { useAuthHeader } from "./hook/useAuthHeader";
|
|
/*
|
|
const useAuthHeader = () => {
|
|
return keycloak?.token
|
|
? { Authorization: `Bearer ${keycloak.token}` }
|
|
: {}
|
|
}*/
|
|
|
|
//debug:
|
|
export function delay(ms: number): Promise<void> {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
//UPDATE /////////////////////////////////////////////////////////
|
|
|
|
//COACH / ATHLETE
|
|
|
|
/*
|
|
retourne l'utilisateur lié à l'identifiant keyloack
|
|
*/
|
|
export async function getUser(id:number): Promise<User|null>{
|
|
try {
|
|
const response = await api.get<User>(`/users/${id}`);
|
|
return response.data;
|
|
}
|
|
catch (error) {
|
|
console.error("Error fetching user:", error);
|
|
return null;
|
|
}
|
|
}
|
|
/*
|
|
retourne toutes les Session dont l'user est inscrit
|
|
*/
|
|
export async function updateSessionsOfUserAPI(user:Coach|Athlete, min: Date|null, max: Date|null){
|
|
try {
|
|
const response = await api.get<Session[]>(`/users/${user.id}/sessions`, {
|
|
params: {
|
|
minDate: min ? min.toISOString() : undefined,
|
|
maxDate: max ? max.toISOString() : undefined
|
|
}
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("Error fetching sessions for user:", error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function updateActivitiesOfSessionAPI(session:Session){
|
|
try {
|
|
const response = await api.get<Activite[]>(`/sessions/${session.id}/activities`);
|
|
session.activites = response.data;
|
|
} catch (error) {
|
|
console.error("Error fetching activities for session:", error);
|
|
}
|
|
|
|
}
|
|
|
|
export async function subscribeSessionAPI(user:User, session:Session):Promise<boolean>{
|
|
try {
|
|
await api.post(`/sessions/${session.id}/subscribe`, { userId: user.id });
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Error subscribing to session:", error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function unsubscribeSessionAPI(user:User, session:Session):Promise<boolean>{
|
|
try {
|
|
await api.post(`/session/${session.id}/unsubscribe`, { userId: user.id });
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Error unsubscribing from session:", error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// ADMIN :
|
|
|
|
export async function updateAllSessionAPI(min: Date, max: Date){
|
|
//TODO
|
|
}
|
|
|
|
export async function updateAllUserAPI(){
|
|
|
|
}
|
|
|
|
// POST /////////////////////////////////////////////////////////
|
|
|
|
// COACH ADMIN
|
|
export async function createSessionAPI(sessionDTO: any): Promise<Session> {
|
|
try {
|
|
const response = await api.post<Session>("/session/create", sessionDTO);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("Error creating session:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function postActivityAPI(session: Session, activity: Activite){
|
|
try {
|
|
const response = await api.post<Activite>(`/session/${session.id}/activities`, activity);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("Error creating activity:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function postUser(user: User):Promise<boolean>{
|
|
return true;
|
|
}
|
|
|
|
// SET /////////////////////////////////////////////////////////
|
|
|
|
//ADMIN
|
|
export async function setUserNameAPI(user: User, name: string){
|
|
|
|
}
|
|
|
|
//COACH
|
|
export async function setSessionCreneauAPI(session: Session, date:Date){
|
|
|
|
}
|
|
|
|
export async function getSessionsAPI(): Promise<Session[]> {
|
|
try {
|
|
const response = await api.get<Session[]>("/session");
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("Error fetching sessions:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
export async function getUsersAPI(): Promise<User[]> {
|
|
try {
|
|
const response = await api.get<User[]>("/users");
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("Error fetching users:", error);
|
|
throw error;
|
|
}
|
|
} |