updated getEmail and getScannedEmail endpoints. Added Polling and Rescan Inbox feature for EmailScreen.tsx
This commit is contained in:
@@ -1,55 +1,83 @@
|
||||
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { View, Text, TouchableOpacity, FlatList, StyleSheet, ActivityIndicator, Alert } from 'react-native';
|
||||
import { getEmails } from '../api/qrCodeAPI';
|
||||
import { getEmails, getScannedEmails } from '../api/qrCodeAPI';
|
||||
|
||||
const EmailScreen: React.FC = () => {
|
||||
const [selectedMessage, setSelectedMessage] = useState(null);
|
||||
const [emailData, setEmailData] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [rescanLoading, setRescanLoading] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const fetchEmails = async () => {
|
||||
startPollingForScannedEmails();
|
||||
}, []);
|
||||
|
||||
// Function to initiate the email fetching process
|
||||
const initiateEmailFetch = async () => {
|
||||
setRescanLoading(true);
|
||||
try {
|
||||
// Call to start email fetching process
|
||||
const response = await getEmails(
|
||||
'Access Token here',
|
||||
'Refresh Token here'
|
||||
);
|
||||
setRescanLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Error initiating email fetch:', error);
|
||||
setError('Error rescanning inbox.');
|
||||
setRescanLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Function to poll for scanned emails
|
||||
const startPollingForScannedEmails = () => {
|
||||
const pollingInterval = setInterval(async () => {
|
||||
try {
|
||||
const emails = await getEmails('access Token here!!!'); // Replace with actual token retrieval
|
||||
setEmailData(emails);
|
||||
const scannedEmails = await getScannedEmails();
|
||||
if (scannedEmails) {
|
||||
setEmailData(scannedEmails);
|
||||
clearInterval(pollingInterval);
|
||||
setLoading(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching scanned emails:', error);
|
||||
setError('Error fetching emails.');
|
||||
} finally {
|
||||
clearInterval(pollingInterval);
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchEmails();
|
||||
}, []);
|
||||
}, 3000); // Poll every 3 seconds
|
||||
};
|
||||
|
||||
const handleSelectMessage = (message) => {
|
||||
setSelectedMessage(selectedMessage === message ? null : message);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<View style={styles.loadingContainer}>
|
||||
<ActivityIndicator size="large" color="#ff69b4" />
|
||||
<Text >Fetching emails...</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<View style={styles.errorContainer}>
|
||||
<Text>{error}</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
{loading && (
|
||||
<View style={styles.loadingContainer}>
|
||||
<ActivityIndicator size="large" color="#ff69b4" />
|
||||
<Text style={{ color: '#ff69b4' }}>Fetching emails...</Text>
|
||||
</View>
|
||||
)}
|
||||
{error && (
|
||||
<View style={styles.errorContainer}>
|
||||
<Text style={{ color: '#ff69b4' }}>{error}</Text>
|
||||
</View>
|
||||
)}
|
||||
{emailData && (
|
||||
<>
|
||||
<Text style={styles.header}>{emailData.emailAddress}</Text>
|
||||
<View style={styles.buttonContainer}>
|
||||
<TouchableOpacity onPress={initiateEmailFetch} style={styles.button}>
|
||||
<Text style={styles.buttonText}>Rescan Inbox</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
{rescanLoading && (
|
||||
<View style={styles.rescanIndicator}>
|
||||
<Text style={{ color: '#ff69b4' }}>Rescanning inbox...</Text>
|
||||
</View>
|
||||
)}
|
||||
<FlatList
|
||||
data={emailData.messages}
|
||||
keyExtractor={(item) => item.messageId}
|
||||
@@ -59,34 +87,34 @@ const EmailScreen: React.FC = () => {
|
||||
<Text style={styles.date}>{item.date}</Text>
|
||||
{selectedMessage === item && (
|
||||
<View style={styles.emailListContainer}>
|
||||
{item.qrCodeByContentId && (
|
||||
<View>
|
||||
<Text style={styles.qrCodeHeader}>QR Codes by Content ID:</Text>
|
||||
{item.qrCodeByContentId.map((qrCode, index) => (
|
||||
<View key={index} style={styles.qrCodeContainer}>
|
||||
{qrCode.decodedContent.map((url, i) => (
|
||||
<TouchableOpacity key={i} onPress={() => Alert.alert("Testing")}>
|
||||
<Text style={styles.qrCodeLink}>{url}</Text>
|
||||
</TouchableOpacity>
|
||||
{item.qrCodeByContentId && (
|
||||
<View>
|
||||
<Text style={styles.qrCodeHeader}>QR Codes by Content ID:</Text>
|
||||
{item.qrCodeByContentId.map((qrCode, index) => (
|
||||
<View key={index} style={styles.qrCodeContainer}>
|
||||
{qrCode.decodedContent.map((url, i) => (
|
||||
<TouchableOpacity key={i} onPress={() => Alert.alert("Testing")}>
|
||||
<Text style={styles.qrCodeLink}>{url}</Text>
|
||||
</TouchableOpacity>
|
||||
))}
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
)}
|
||||
{item.qrCodeByURL && (
|
||||
<View>
|
||||
<Text style={styles.qrCodeHeader}>QR Codes by URL:</Text>
|
||||
{item.qrCodeByURL.map((qrCode, index) => (
|
||||
<View key={index} style={styles.qrCodeContainer}>
|
||||
{qrCode.decodedContent.map((url, i) => (
|
||||
<TouchableOpacity key={i} onPress={() => Alert.alert("Testing")}>
|
||||
<Text style={styles.qrCodeLink}>{url}</Text>
|
||||
</TouchableOpacity>
|
||||
))}
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
<View>
|
||||
<Text style={styles.qrCodeHeader}>QR Codes by URL:</Text>
|
||||
{item.qrCodeByURL.map((qrCode, index) => (
|
||||
<View key={index} style={styles.qrCodeContainer}>
|
||||
{qrCode.decodedContent.map((url, i) => (
|
||||
<TouchableOpacity key={i} onPress={() => Alert.alert("Testing")}>
|
||||
<Text style={styles.qrCodeLink}>{url}</Text>
|
||||
</TouchableOpacity>
|
||||
))}
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
)}
|
||||
</TouchableOpacity>
|
||||
@@ -115,11 +143,24 @@ const styles = StyleSheet.create({
|
||||
backgroundColor: '#f8f0fc',
|
||||
padding: 10,
|
||||
},
|
||||
loadingText: {
|
||||
fontSize: 18,
|
||||
color: '#ff69b4',
|
||||
marginBottom: 10,
|
||||
textAlign: 'center',
|
||||
buttonContainer: {
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
marginVertical: 20,
|
||||
},
|
||||
button: {
|
||||
backgroundColor: '#ff69b4',
|
||||
padding: 10,
|
||||
borderRadius: 5,
|
||||
},
|
||||
buttonText: {
|
||||
color: '#fff',
|
||||
fontWeight: 'bold',
|
||||
},
|
||||
rescanIndicator: {
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
marginVertical: 10,
|
||||
},
|
||||
emailListContainer: {
|
||||
flex: 1,
|
||||
|
||||
Reference in New Issue
Block a user