detect if not gmail and setEmpty Messag to login with Gmail account

This commit is contained in:
2024-08-16 12:11:43 +08:00
parent 13dd88cd66
commit 850675a083

View File

@@ -16,17 +16,15 @@ const EmailScreen: React.FC = () => {
const [rescanLoading, setRescanLoading] = useState(false); const [rescanLoading, setRescanLoading] = useState(false);
const [error, setError] = useState(null); const [error, setError] = useState(null);
const [userEmail, setUserEmail] = useState(''); const [userEmail, setUserEmail] = useState('');
const [bannerOpacity] = useState(new Animated.Value(0));
const [isModalVisible, setIsModalVisible] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false);
const [selectedQrCodeId, setSelectedQrCodeId] = useState(null); const [selectedQrCodeId, setSelectedQrCodeId] = useState(null);
const [errorBannerOpacity] = useState(new Animated.Value(0));
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false); const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const [messageToDelete, setMessageToDelete] = useState(null); const [messageToDelete, setMessageToDelete] = useState(null);
const [emptyMessage, setEmptyMessage] = useState('');
// Start scanning inbox only once when the component mounts // Start scanning inbox only once when the component mounts
useEffect(() => { useEffect(() => {
startInboxScanning(); fetchUserEmail(); // Fetch user email before scanning inbox
fetchUserEmail();
}, []); }, []);
// Function to fetch user email // Function to fetch user email
@@ -34,17 +32,30 @@ const EmailScreen: React.FC = () => {
try { try {
console.log('fetchUserEmail triggered'); console.log('fetchUserEmail triggered');
const userInfo = await getUserInfo(); const userInfo = await getUserInfo();
if (userInfo && userInfo.email) {
setUserEmail(userInfo.email); setUserEmail(userInfo.email);
// Check if the email is a Gmail account
if (userInfo.email.endsWith('@gmail.com')) {
startInboxScanning();
} else {
setEmptyMessage('Please login using a Gmail account to view Emails');
setLoading(false);
}
} else {
setEmptyMessage('Please login using a Gmail account to view Emails');
setLoading(false);
}
} catch (error) { } catch (error) {
console.error('Error fetching user email:', error); console.error('Error fetching user email:', error);
setUserEmail('Error fetching email'); setEmptyMessage('Please login using a Gmail account to view Emails');
setLoading(false);
} }
}; };
// Function to initiate the email fetching process // Function to initiate the email fetching process
const startInboxScanning = async () => { const startInboxScanning = async () => {
setRescanLoading(true); setRescanLoading(true);
showBanner();
try { try {
// Fetch the current authentication session // Fetch the current authentication session
@@ -75,45 +86,12 @@ const EmailScreen: React.FC = () => {
} }
}; };
// Function to show the scan email in background banner
const showBanner = () => {
Animated.timing(bannerOpacity, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}).start(() => {
setTimeout(() => {
Animated.timing(bannerOpacity, {
toValue: 0,
duration: 500,
useNativeDriver: true,
}).start();
}, 3000);
});
};
const showErrorBanner = () => {
Animated.timing(errorBannerOpacity, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}).start(() => {
setTimeout(() => {
Animated.timing(errorBannerOpacity, {
toValue: 0,
duration: 500,
useNativeDriver: true,
}).start();
}, 3000);
});
};
// Function to poll for scanned emails // Function to poll for scanned emails
const startPollingForScannedEmails = useCallback(() => { const startPollingForScannedEmails = useCallback(() => {
const pollingInterval = setInterval(async () => { const pollingInterval = setInterval(async () => {
try { try {
const scannedEmails = await getScannedEmails(); const scannedEmails = await getScannedEmails();
if (scannedEmails) { if (scannedEmails && scannedEmails.messages && scannedEmails.messages.length > 0) {
setEmailData((prevEmailData) => { setEmailData((prevEmailData) => {
// Preserve the selected message if it's still in the new list // Preserve the selected message if it's still in the new list
const selectedMessageExists = prevEmailData?.messages.some( const selectedMessageExists = prevEmailData?.messages.some(
@@ -133,13 +111,14 @@ const EmailScreen: React.FC = () => {
return scannedEmails; return scannedEmails;
} }
}); });
setLoading(false); } else {
setEmptyMessage('No Emails with QR Code');
} }
setLoading(false);
} catch (error) { } catch (error) {
console.error('Error fetching scanned emails:', error); console.error('Error fetching scanned emails:', error);
setError('Error fetching emails.'); setEmptyMessage('No Emails with QR Code');
setLoading(false); setLoading(false);
showErrorBanner();
} }
}, 10000); // Poll every 10 seconds }, 10000); // Poll every 10 seconds
@@ -207,17 +186,20 @@ const EmailScreen: React.FC = () => {
return ( return (
<View style={styles.container}> <View style={styles.container}>
{loading && ( {loading && (
<View style={styles.loadingContainer}> <View style={styles.loadingContainer}>
<ActivityIndicator size="large" color="#ff69b4" /> <ActivityIndicator size="large" color="#ff69b4" />
<Text style={{ color: '#ff69b4' }}>Fetching emails...</Text> <Text style={{ color: '#ff69b4' }}>Fetching emails...</Text>
</View> </View>
)} )}
{error && (
{!loading && emptyMessage && (
<View style={styles.errorContainer}> <View style={styles.errorContainer}>
<Text style={{ color: '#ff69b4' }}>{error}</Text> <Text style={styles.errorText}>{emptyMessage}</Text>
</View> </View>
)} )}
{emailData && ( {emailData && (
<> <>
<View style={styles.headerContainer}> <View style={styles.headerContainer}>
@@ -261,14 +243,6 @@ const EmailScreen: React.FC = () => {
</> </>
)} )}
<Animated.View style={[styles.banner, { opacity: bannerOpacity }]} pointerEvents="none">
<Text style={styles.bannerText}>Scanning emails in the background. This may take a while...</Text>
</Animated.View>
<Animated.View style={[styles.errorBanner, { opacity: errorBannerOpacity }]} pointerEvents="none">
<Text style={styles.bannerText}>Error fetching scanned emails</Text>
</Animated.View>
{/* Modal for ScannedDataBox */} {/* Modal for ScannedDataBox */}
<Modal <Modal
visible={isModalVisible} visible={isModalVisible}
@@ -415,61 +389,6 @@ const styles = StyleSheet.create({
marginBottom: screenHeight * 0.01, marginBottom: screenHeight * 0.01,
marginTop: screenHeight * 0.015, marginTop: screenHeight * 0.015,
}, },
banner: {
position: 'absolute',
top: screenHeight * 0.45,
left: screenWidth * 0.1,
right: screenWidth * 0.1,
backgroundColor: '#ff69b4',
paddingVertical: screenHeight * 0.02,
paddingHorizontal: screenWidth * 0.05,
borderRadius: screenWidth * 0.05,
alignItems: 'center',
justifyContent: 'center',
zIndex: 10,
},
errorBanner: {
position: 'absolute',
top: screenHeight * 0.4,
left: screenWidth * 0.1,
right: screenWidth * 0.1,
backgroundColor: '#ff69b4',
paddingVertical: screenHeight * 0.02,
paddingHorizontal: screenWidth * 0.05,
borderRadius: screenWidth * 0.05,
alignItems: 'center',
justifyContent: 'center',
zIndex: 10,
}, bannerText: {
color: '#fff',
fontWeight: 'bold',
textAlign: 'center',
fontSize: screenWidth * 0.04,
},
innerModalContainer: {
backgroundColor: '#ffe6f0',
padding: screenWidth * 0.05,
borderRadius: screenWidth * 0.03,
alignItems: 'center',
},
modalCloseButton: {
position: 'absolute',
top: screenWidth * 0.02,
right: screenWidth * 0.02,
zIndex: 1,
},
deleteButtonContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
padding: screenWidth * 0.02,
},
deleteButtonText: {
marginRight: screenWidth * 0.02,
color: '#ff69b4',
fontSize: screenWidth * 0.035,
},
modalOverlay: { modalOverlay: {
flex: 1, flex: 1,
justifyContent: 'center', justifyContent: 'center',
@@ -482,13 +401,6 @@ const styles = StyleSheet.create({
padding: screenWidth * 0.025, padding: screenWidth * 0.025,
elevation: 5, elevation: 5,
}, },
modalContent: {
width: '80%',
backgroundColor: 'white',
borderRadius: screenWidth * 0.025,
padding: screenWidth * 0.05,
alignItems: 'center',
},
modalTitle: { modalTitle: {
fontSize: screenWidth * 0.05, fontSize: screenWidth * 0.05,
fontWeight: 'bold', fontWeight: 'bold',
@@ -514,6 +426,17 @@ const styles = StyleSheet.create({
fontSize: screenWidth * 0.04, fontSize: screenWidth * 0.04,
color: '#000', color: '#000',
}, },
deleteButtonContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
padding: screenWidth * 0.02,
},
deleteButtonText: {
marginRight: screenWidth * 0.02,
color: '#ff69b4',
fontSize: screenWidth * 0.035,
},
}); });
export default EmailScreen; export default EmailScreen;