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

+11 -25
View File
@@ -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) {
+21 -15
View File
@@ -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,10 +31,11 @@ export default function WelcomeScreen() {
})
const [loading, setLoading] = useState(false)
const router = useRouter()
const { getToken } = useAuth()
const fitnessGoalsOptions = [
'Weight Loss',
'Muscle Gain',
'Muscle Gain',
'Improve Endurance',
'Better Flexibility',
'General Fitness',
@@ -67,26 +68,31 @@ 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')
}
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)') }
])
}
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.')
@@ -103,7 +109,7 @@ export default function WelcomeScreen() {
<View style={styles.section}>
<Text style={styles.sectionTitle}>Basic Information</Text>
<View style={styles.row}>
<View style={[styles.inputContainer, { flex: 1, marginRight: 8 }]}>
<Text style={styles.label}>Height (cm)</Text>
@@ -115,7 +121,7 @@ export default function WelcomeScreen() {
placeholder="170"
/>
</View>
<View style={[styles.inputContainer, { flex: 1, marginLeft: 8 }]}>
<Text style={styles.label}>Weight (kg)</Text>
<TextInput
@@ -139,7 +145,7 @@ export default function WelcomeScreen() {
placeholder="25"
/>
</View>
<View style={[styles.inputContainer, { flex: 1, marginLeft: 8 }]}>
<Text style={styles.label}>Gender</Text>
<View style={styles.genderRow}>