api calls simplified

This commit is contained in:
dimitar committed 2025-11-23 17:12:14 +01:00
1 parent 6cbed4e8d1
commit 1b50bfd2ee
7 files changed
+136 -49

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import { apiClient } from "./client";
import { API_ENDPOINTS } from "../config/api";
export interface Attendance {
id: string;
checkInTime: string;
checkOutTime?: string;
type: string;
notes?: string;
}
export const attendanceApi = {
getHistory: async (token: string): Promise<Attendance[]> => {
try {
const response = await apiClient.get(API_ENDPOINTS.ATTENDANCE.HISTORY, {
headers: { Authorization: `Bearer ${token}` },
});
return response.data;
} catch (error) {
throw error;
}
},
checkIn: async (type: string, token: string): Promise<void> => {
try {
await apiClient.post(
API_ENDPOINTS.ATTENDANCE.CHECK_IN,
{ type },
{ headers: { Authorization: `Bearer ${token}` } },
);
} catch (error) {
throw error;
}
},
checkOut: async (token: string): Promise<void> => {
try {
await apiClient.post(
API_ENDPOINTS.ATTENDANCE.CHECK_OUT,
{},
{ headers: { Authorization: `Bearer ${token}` } },
);
} catch (error) {
throw error;
}
},
};
+18
View File
@@ -0,0 +1,18 @@
import axios from 'axios';
import { API_BASE_URL } from '../config/api';
export const apiClient = axios.create({
baseURL: API_BASE_URL,
headers: {
'Content-Type': 'application/json',
},
});
// Helper to set the auth token for a request
export const setAuthToken = (token: string) => {
if (token) {
apiClient.defaults.headers.common['Authorization'] = `Bearer ${token}`;
} else {
delete apiClient.defaults.headers.common['Authorization'];
}
};
+9 -9
View File
@@ -1,5 +1,5 @@
import axios from "axios";
import { API_BASE_URL } from "../config/api";
import { apiClient } from "./client";
import { API_ENDPOINTS } from "../config/api";
export interface FitnessProfile {
id?: string;
@@ -22,10 +22,10 @@ export const fitnessProfileApi = {
try {
console.log(
"Getting fitness profile with URL:",
`${API_BASE_URL}/api/users/${userId}/fitness-profile`,
`${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
);
const response = await axios.get(
`${API_BASE_URL}/api/users/${userId}/fitness-profile`,
const response = await apiClient.get(
`${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
{
headers: {
Authorization: `Bearer ${token}`,
@@ -49,8 +49,8 @@ export const fitnessProfileApi = {
token: string,
): Promise<FitnessProfile> => {
try {
const response = await axios.put(
`${API_BASE_URL}/api/users/${userId}/fitness-profile`,
const response = await apiClient.put(
`${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
data,
{
headers: {
@@ -74,8 +74,8 @@ export const fitnessProfileApi = {
token: string,
): Promise<FitnessProfile> => {
try {
const response = await axios.post(
`${API_BASE_URL}/api/users/${data.clientId}/fitness-profile`,
const response = await apiClient.post(
`${API_ENDPOINTS.USERS}/${data.clientId}/fitness-profile`,
data,
{
headers: {
+30
View File
@@ -0,0 +1,30 @@
import { apiClient } from "./client";
import { API_ENDPOINTS } from "../config/api";
export interface FitnessProfileData {
userId: string;
height: string;
weight: string;
age: string;
gender: "male" | "female" | "other";
activityLevel: "sedentary" | "light" | "moderate" | "active" | "very_active";
fitnessGoals: string[];
exerciseHabits: string;
dietHabits: string;
medicalConditions: string;
}
export const profileApi = {
createFitnessProfile: async (
data: FitnessProfileData,
token: string,
): Promise<void> => {
try {
await apiClient.post(API_ENDPOINTS.PROFILE.FITNESS, data, {
headers: { Authorization: `Bearer ${token}` },
});
} catch (error) {
throw error;
}
},
};