import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, TouchableOpacity, StyleSheet, Alert } from 'react-native';
import { router } from 'expo-router';
import { getBackendUrl, setBackendUrl } from '../../lib/api';
export default function BackendSetup() {
const [url, setUrl] = useState('');
const [loading, setLoading] = useState(true);
useEffect(() => {
getBackendUrl().then((currentUrl: string) => {
setUrl(currentUrl);
setLoading(false);
});
}, []);
const handleSave = async () => {
try {
// Validate URL format
let validUrl = url.trim();
if (!validUrl.startsWith('http://') && !validUrl.startsWith('https://')) {
validUrl = 'http://' + validUrl;
}
// Remove trailing slash
validUrl = validUrl.replace(/\/$/, '');
await setBackendUrl(validUrl);
Alert.alert('Success', `Connected to ${validUrl}`, [
{ text: 'OK', onPress: () => router.replace('/(auth)/sign-in') },
]);
} catch (e) {
Alert.alert('Error', 'Failed to save backend URL');
}
};
const useDefault = async () => {
await setBackendUrl('http://localhost:3001');
router.replace('/(auth)/sign-in');
};
if (loading) {
return (
Loading...
);
}
return (
🔧 Connect to Backend
Enter the URL of your Bagsy API server
Save & Continue
Use Localhost (3001)
💡 For local dev: http://localhost:3001{'\n'}
For production: https://your-api-domain.com
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#004E89',
justifyContent: 'center',
alignItems: 'center',
padding: 20,
},
loadingText: {
color: '#fff',
fontSize: 18,
},
card: {
backgroundColor: '#fff',
borderRadius: 16,
padding: 24,
width: '100%',
maxWidth: 400,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#FF6B35',
marginBottom: 8,
},
subtitle: {
fontSize: 14,
color: '#666',
marginBottom: 24,
},
input: {
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 8,
padding: 12,
fontSize: 16,
marginBottom: 16,
color: '#333',
},
button: {
backgroundColor: '#FF6B35',
borderRadius: 8,
padding: 14,
alignItems: 'center',
marginBottom: 12,
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
secondaryButton: {
backgroundColor: '#f0f0f0',
borderRadius: 8,
padding: 12,
alignItems: 'center',
marginBottom: 16,
},
secondaryButtonText: {
color: '#666',
fontSize: 14,
},
hint: {
padding: 12,
backgroundColor: '#f9f9f9',
borderRadius: 8,
},
hintText: {
fontSize: 12,
color: '#888',
},
});