import { useState, useEffect } from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
Alert,
ActivityIndicator,
SafeAreaView,
StatusBar
} from 'react-native';
import { WebView } from 'react-native-webview';
import AsyncStorage from '@react-native-async-storage/async-storage';
const STORAGE_KEY = '@condomob_url';
const DEFAULT_URL = 'https://condopay.fcarra.mywire.org';
export default function App() {
const [url, setUrl] = useState('');
const [isLoading, setIsLoading] = useState(true);
const [isValidating, setIsValidating] = useState(false);
const [showWebView, setShowWebView] = useState(false);
useEffect(() => {
loadSavedUrl();
}, []);
const loadSavedUrl = async () => {
try {
const savedUrl = await AsyncStorage.getItem(STORAGE_KEY);
if (savedUrl) {
setUrl(savedUrl);
setShowWebView(true);
}
} catch (error) {
console.error('Error loading saved URL:', error);
} finally {
setIsLoading(false);
}
};
const validateUrl = (urlString: string): boolean => {
try {
const parsedUrl = new URL(urlString);
return parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:';
} catch {
return false;
}
};
const isCondoPayUrl = (urlString: string): boolean => {
// Check if URL contains 'condopay' in the hostname
const lowerUrl = urlString.toLowerCase();
return lowerUrl.includes('condopay');
};
const handleConnect = async () => {
if (!url.trim()) {
Alert.alert('Errore', 'Inserisci un URL valido');
return;
}
// Add https:// if no protocol is provided
let finalUrl = url.trim();
if (!finalUrl.startsWith('http://') && !finalUrl.startsWith('https://')) {
finalUrl = 'https://' + finalUrl;
}
// Validate URL format
if (!validateUrl(finalUrl)) {
Alert.alert('Errore', 'L\'URL inserito non รจ valido');
return;
}
// Check if it's a CondoPay URL
if (!isCondoPayUrl(finalUrl)) {
Alert.alert(
'URL non valido',
'L\'URL deve puntare a un\'istanza di CondoPay (deve contenere "condopay" nel dominio)'
);
return;
}
setIsValidating(true);
// Try to validate by checking if the URL is reachable
try {
const response = await fetch(finalUrl, {
method: 'HEAD',
signal: AbortSignal.timeout(10000)
});
if (response.ok || response.status === 200 || response.status === 301 || response.status === 302) {
// Save URL and show WebView
await AsyncStorage.setItem(STORAGE_KEY, finalUrl);
setUrl(finalUrl);
setShowWebView(true);
} else {
Alert.alert('Errore', 'Impossibile connettersi all\'istanza di CondoPay');
}
} catch (error) {
// If fetch fails, still try to show the WebView - it might work
console.log('Validation fetch failed, trying WebView anyway:', error);
try {
await AsyncStorage.setItem(STORAGE_KEY, finalUrl);
setUrl(finalUrl);
setShowWebView(true);
} catch (storageError) {
Alert.alert('Errore', 'Impossibile salvare l\'URL');
}
} finally {
setIsValidating(false);
}
};
const handleBack = () => {
setShowWebView(false);
};
// Loading screen while checking for saved URL
if (isLoading) {
return (
);
}
// WebView screen
if (showWebView) {
return (
โ Indietro
CondoPay
(
Caricamento...
)}
/>
);
}
// Configuration screen
return (
๐
CondoMob
Connetti al tuo CondoPay
URL Istanza CondoPay
Inserisci l'URL della tua istanza CondoPay
{isValidating ? (
) : (
Connetti
)}
Esempio:
{DEFAULT_URL}
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
},
content: {
flex: 1,
padding: 24,
justifyContent: 'center',
},
logoContainer: {
alignItems: 'center',
marginBottom: 40,
},
logoText: {
fontSize: 64,
marginBottom: 16,
},
title: {
fontSize: 32,
fontWeight: 'bold',
color: '#333',
marginBottom: 8,
},
subtitle: {
fontSize: 16,
color: '#666',
},
inputContainer: {
marginBottom: 24,
},
label: {
fontSize: 14,
fontWeight: '600',
color: '#333',
marginBottom: 8,
},
input: {
backgroundColor: '#fff',
borderRadius: 12,
padding: 16,
fontSize: 16,
borderWidth: 1,
borderColor: '#ddd',
},
hint: {
fontSize: 12,
color: '#999',
marginTop: 8,
},
button: {
backgroundColor: '#007AFF',
borderRadius: 12,
padding: 16,
alignItems: 'center',
marginBottom: 24,
},
buttonDisabled: {
backgroundColor: '#99c2ff',
},
buttonText: {
color: '#fff',
fontSize: 18,
fontWeight: '600',
},
exampleContainer: {
alignItems: 'center',
marginTop: 20,
},
exampleTitle: {
fontSize: 14,
color: '#999',
marginBottom: 8,
},
exampleUrl: {
fontSize: 14,
color: '#007AFF',
fontFamily: 'monospace',
},
// WebView styles
webViewContainer: {
flex: 1,
backgroundColor: '#fff',
},
webViewHeader: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 16,
paddingVertical: 12,
backgroundColor: '#fff',
borderBottomWidth: 1,
borderBottomColor: '#ddd',
},
backButton: {
padding: 8,
},
backButtonText: {
fontSize: 16,
color: '#007AFF',
},
headerTitle: {
fontSize: 18,
fontWeight: '600',
color: '#333',
flex: 1,
textAlign: 'center',
},
placeholder: {
width: 60,
},
webView: {
flex: 1,
},
loadingContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
loadingText: {
marginTop: 12,
fontSize: 16,
color: '#666',
},
});