api calls simplified

This commit is contained in:
echo 2025-11-23 17:12:14 +01:00
parent 6cbed4e8d1
commit 1b50bfd2ee
7 changed files with 136 additions and 49 deletions

Binary file not shown.

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;
}
},
};

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'];
}
};

View File

@ -1,5 +1,5 @@
import axios from "axios"; import { apiClient } from "./client";
import { API_BASE_URL } from "../config/api"; import { API_ENDPOINTS } from "../config/api";
export interface FitnessProfile { export interface FitnessProfile {
id?: string; id?: string;
@ -22,10 +22,10 @@ export const fitnessProfileApi = {
try { try {
console.log( console.log(
"Getting fitness profile with URL:", "Getting fitness profile with URL:",
`${API_BASE_URL}/api/users/${userId}/fitness-profile`, `${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
); );
const response = await axios.get( const response = await apiClient.get(
`${API_BASE_URL}/api/users/${userId}/fitness-profile`, `${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
{ {
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
@ -49,8 +49,8 @@ export const fitnessProfileApi = {
token: string, token: string,
): Promise<FitnessProfile> => { ): Promise<FitnessProfile> => {
try { try {
const response = await axios.put( const response = await apiClient.put(
`${API_BASE_URL}/api/users/${userId}/fitness-profile`, `${API_ENDPOINTS.USERS}/${userId}/fitness-profile`,
data, data,
{ {
headers: { headers: {
@ -74,8 +74,8 @@ export const fitnessProfileApi = {
token: string, token: string,
): Promise<FitnessProfile> => { ): Promise<FitnessProfile> => {
try { try {
const response = await axios.post( const response = await apiClient.post(
`${API_BASE_URL}/api/users/${data.clientId}/fitness-profile`, `${API_ENDPOINTS.USERS}/${data.clientId}/fitness-profile`,
data, data,
{ {
headers: { headers: {

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;
}
},
};

View File

@ -1,16 +1,9 @@
import { View, Text, StyleSheet, TouchableOpacity, ActivityIndicator, ScrollView, Alert } from 'react-native' import { View, Text, StyleSheet, TouchableOpacity, ActivityIndicator, ScrollView, Alert } from 'react-native'
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import { useAuth } from '@clerk/clerk-expo' import { useAuth } from '@clerk/clerk-expo'
import axios from 'axios' import { attendanceApi, Attendance } from '../../api/attendance'
import { API_BASE_URL, API_ENDPOINTS } from '../../config/api'
interface Attendance {
id: string
checkInTime: string
checkOutTime?: string
type: string
notes?: string
}
export default function AttendanceScreen() { export default function AttendanceScreen() {
const { getToken, userId } = useAuth() const { getToken, userId } = useAuth()
@ -22,13 +15,10 @@ export default function AttendanceScreen() {
try { try {
setLoading(true) setLoading(true)
const token = await getToken() const token = await getToken()
const url = `${API_BASE_URL}${API_ENDPOINTS.ATTENDANCE.HISTORY}` if (!token) return
console.log('Fetching attendance from:', url)
const response = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` }
})
const data: Attendance[] = response.data console.log('Fetching attendance history')
const data = await attendanceApi.getHistory(token)
setHistory(data) setHistory(data)
// Check if there's an active check-in (latest one has no checkOutTime) // Check if there's an active check-in (latest one has no checkOutTime)
@ -52,11 +42,9 @@ export default function AttendanceScreen() {
const handleCheckIn = async () => { const handleCheckIn = async () => {
try { try {
const token = await getToken() const token = await getToken()
await axios.post( if (!token) return
`${API_BASE_URL}${API_ENDPOINTS.ATTENDANCE.CHECK_IN}`,
{ type: 'gym' }, await attendanceApi.checkIn('gym', token)
{ headers: { Authorization: `Bearer ${token}` } }
)
fetchAttendance() fetchAttendance()
Alert.alert('Success', 'Checked in successfully!') Alert.alert('Success', 'Checked in successfully!')
} catch (error: any) { } catch (error: any) {
@ -68,11 +56,9 @@ export default function AttendanceScreen() {
const handleCheckOut = async () => { const handleCheckOut = async () => {
try { try {
const token = await getToken() const token = await getToken()
await axios.post( if (!token) return
`${API_BASE_URL}${API_ENDPOINTS.ATTENDANCE.CHECK_OUT}`,
{}, await attendanceApi.checkOut(token)
{ headers: { Authorization: `Bearer ${token}` } }
)
fetchAttendance() fetchAttendance()
Alert.alert('Success', 'Checked out successfully!') Alert.alert('Success', 'Checked out successfully!')
} catch (error: any) { } catch (error: any) {

View File

@ -1,9 +1,9 @@
import React, { useState } from 'react' import React, { useState } from 'react'
import { View, Text, TextInput, TouchableOpacity, StyleSheet, Alert, ScrollView } from 'react-native' import { View, Text, TextInput, TouchableOpacity, StyleSheet, Alert, ScrollView } from 'react-native'
import { useRouter } from 'expo-router' import { useRouter } from 'expo-router'
import axios from 'axios' import { useAuth } from '@clerk/clerk-expo'
import * as SecureStore from 'expo-secure-store' import * as SecureStore from 'expo-secure-store'
import { API_BASE_URL, API_ENDPOINTS } from '../config/api' import { profileApi } from '../api/profile'
interface FitnessProfile { interface FitnessProfile {
height: string height: string
@ -31,6 +31,7 @@ export default function WelcomeScreen() {
}) })
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const router = useRouter() const router = useRouter()
const { getToken } = useAuth()
const fitnessGoalsOptions = [ const fitnessGoalsOptions = [
'Weight Loss', 'Weight Loss',
@ -67,6 +68,11 @@ export default function WelcomeScreen() {
setLoading(true) setLoading(true)
try { try {
const token = await getToken()
if (!token) {
throw new Error('Authentication required')
}
const user = await SecureStore.getItemAsync('user') const user = await SecureStore.getItemAsync('user')
if (!user) { if (!user) {
throw new Error('No user found') throw new Error('No user found')
@ -74,19 +80,19 @@ export default function WelcomeScreen() {
const userData = JSON.parse(user) const userData = JSON.parse(user)
const response = await axios.post( await profileApi.createFitnessProfile(
`${API_BASE_URL}${API_ENDPOINTS.PROFILE.FITNESS}`,
{ {
userId: userData.id, userId: userData.id,
...profile ...profile
} },
token
) )
if (response.status === 201) {
Alert.alert('Success', 'Profile completed successfully!', [ Alert.alert('Success', 'Profile completed successfully!', [
{ text: 'OK', onPress: () => router.replace('/(tabs)') } { text: 'OK', onPress: () => router.replace('/(tabs)') }
]) ])
}
} catch (error: any) { } catch (error: any) {
console.log('Profile save error:', error) console.log('Profile save error:', error)
Alert.alert('Error', error.response?.data?.error || 'Failed to save profile. Please try again.') Alert.alert('Error', error.response?.data?.error || 'Failed to save profile. Please try again.')