api calls simplified
This commit is contained in:
parent
6cbed4e8d1
commit
1b50bfd2ee
Binary file not shown.
47
apps/mobile/src/api/attendance.ts
Normal file
47
apps/mobile/src/api/attendance.ts
Normal 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
apps/mobile/src/api/client.ts
Normal file
18
apps/mobile/src/api/client.ts
Normal 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'];
|
||||
}
|
||||
};
|
||||
@ -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
apps/mobile/src/api/profile.ts
Normal file
30
apps/mobile/src/api/profile.ts
Normal 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;
|
||||
}
|
||||
},
|
||||
};
|
||||
@ -1,16 +1,9 @@
|
||||
import { View, Text, StyleSheet, TouchableOpacity, ActivityIndicator, ScrollView, Alert } from 'react-native'
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useAuth } from '@clerk/clerk-expo'
|
||||
import axios from 'axios'
|
||||
import { API_BASE_URL, API_ENDPOINTS } from '../../config/api'
|
||||
import { attendanceApi, Attendance } from '../../api/attendance'
|
||||
|
||||
|
||||
interface Attendance {
|
||||
id: string
|
||||
checkInTime: string
|
||||
checkOutTime?: string
|
||||
type: string
|
||||
notes?: string
|
||||
}
|
||||
|
||||
export default function AttendanceScreen() {
|
||||
const { getToken, userId } = useAuth()
|
||||
@ -22,13 +15,10 @@ export default function AttendanceScreen() {
|
||||
try {
|
||||
setLoading(true)
|
||||
const token = await getToken()
|
||||
const url = `${API_BASE_URL}${API_ENDPOINTS.ATTENDANCE.HISTORY}`
|
||||
console.log('Fetching attendance from:', url)
|
||||
const response = await axios.get(url, {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
if (!token) return
|
||||
|
||||
const data: Attendance[] = response.data
|
||||
console.log('Fetching attendance history')
|
||||
const data = await attendanceApi.getHistory(token)
|
||||
setHistory(data)
|
||||
|
||||
// Check if there's an active check-in (latest one has no checkOutTime)
|
||||
@ -52,11 +42,9 @@ export default function AttendanceScreen() {
|
||||
const handleCheckIn = async () => {
|
||||
try {
|
||||
const token = await getToken()
|
||||
await axios.post(
|
||||
`${API_BASE_URL}${API_ENDPOINTS.ATTENDANCE.CHECK_IN}`,
|
||||
{ type: 'gym' },
|
||||
{ headers: { Authorization: `Bearer ${token}` } }
|
||||
)
|
||||
if (!token) return
|
||||
|
||||
await attendanceApi.checkIn('gym', token)
|
||||
fetchAttendance()
|
||||
Alert.alert('Success', 'Checked in successfully!')
|
||||
} catch (error: any) {
|
||||
@ -68,11 +56,9 @@ export default function AttendanceScreen() {
|
||||
const handleCheckOut = async () => {
|
||||
try {
|
||||
const token = await getToken()
|
||||
await axios.post(
|
||||
`${API_BASE_URL}${API_ENDPOINTS.ATTENDANCE.CHECK_OUT}`,
|
||||
{},
|
||||
{ headers: { Authorization: `Bearer ${token}` } }
|
||||
)
|
||||
if (!token) return
|
||||
|
||||
await attendanceApi.checkOut(token)
|
||||
fetchAttendance()
|
||||
Alert.alert('Success', 'Checked out successfully!')
|
||||
} catch (error: any) {
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import React, { useState } from 'react'
|
||||
import { View, Text, TextInput, TouchableOpacity, StyleSheet, Alert, ScrollView } from 'react-native'
|
||||
import { useRouter } from 'expo-router'
|
||||
import axios from 'axios'
|
||||
import { useAuth } from '@clerk/clerk-expo'
|
||||
import * as SecureStore from 'expo-secure-store'
|
||||
import { API_BASE_URL, API_ENDPOINTS } from '../config/api'
|
||||
import { profileApi } from '../api/profile'
|
||||
|
||||
interface FitnessProfile {
|
||||
height: string
|
||||
@ -31,6 +31,7 @@ export default function WelcomeScreen() {
|
||||
})
|
||||
const [loading, setLoading] = useState(false)
|
||||
const router = useRouter()
|
||||
const { getToken } = useAuth()
|
||||
|
||||
const fitnessGoalsOptions = [
|
||||
'Weight Loss',
|
||||
@ -67,6 +68,11 @@ export default function WelcomeScreen() {
|
||||
|
||||
setLoading(true)
|
||||
try {
|
||||
const token = await getToken()
|
||||
if (!token) {
|
||||
throw new Error('Authentication required')
|
||||
}
|
||||
|
||||
const user = await SecureStore.getItemAsync('user')
|
||||
if (!user) {
|
||||
throw new Error('No user found')
|
||||
@ -74,19 +80,19 @@ export default function WelcomeScreen() {
|
||||
|
||||
const userData = JSON.parse(user)
|
||||
|
||||
const response = await axios.post(
|
||||
`${API_BASE_URL}${API_ENDPOINTS.PROFILE.FITNESS}`,
|
||||
await profileApi.createFitnessProfile(
|
||||
{
|
||||
userId: userData.id,
|
||||
...profile
|
||||
}
|
||||
},
|
||||
token
|
||||
)
|
||||
|
||||
if (response.status === 201) {
|
||||
Alert.alert('Success', 'Profile completed successfully!', [
|
||||
{ text: 'OK', onPress: () => router.replace('/(tabs)') }
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
} catch (error: any) {
|
||||
console.log('Profile save error:', error)
|
||||
Alert.alert('Error', error.response?.data?.error || 'Failed to save profile. Please try again.')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user