57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { isAxiosError } from "axios";
|
|
import { API_ENDPOINTS } from "../config/api";
|
|
import { apiClient, withAuth } from "./client";
|
|
|
|
export interface Gym {
|
|
id: string;
|
|
name: string;
|
|
location?: string;
|
|
}
|
|
|
|
export const gymsApi = {
|
|
getGyms: async (token: string | null): Promise<Gym[]> => {
|
|
try {
|
|
const response = await apiClient.get<Gym[] | { data?: Gym[] }>(
|
|
API_ENDPOINTS.GYMS,
|
|
withAuth(token),
|
|
);
|
|
|
|
const payload = response.data;
|
|
if (Array.isArray(payload)) {
|
|
return payload;
|
|
}
|
|
|
|
if (payload && Array.isArray(payload.data)) {
|
|
return payload.data;
|
|
}
|
|
|
|
return [];
|
|
} catch (error) {
|
|
if (isAxiosError(error) && error.response) {
|
|
throw new Error(`Failed to fetch gyms: ${error.response.status}`);
|
|
}
|
|
throw new Error("Failed to fetch gyms");
|
|
}
|
|
},
|
|
|
|
updateUserGym: async (
|
|
gymId: string | null,
|
|
token: string | null,
|
|
): Promise<void> => {
|
|
try {
|
|
await apiClient.patch(
|
|
API_ENDPOINTS.USERS.GYM,
|
|
{ gymId },
|
|
withAuth(token),
|
|
);
|
|
} catch (error) {
|
|
if (isAxiosError(error) && error.response) {
|
|
throw new Error(
|
|
`Failed to update gym selection: ${error.response.status}`,
|
|
);
|
|
}
|
|
throw new Error("Failed to update gym selection");
|
|
}
|
|
},
|
|
};
|