import React, { useContext, useState, useEffect } from 'react'; import { View, Text, StyleSheet, FlatList, TouchableOpacity, Image, BackHandler, Modal } from 'react-native'; import { useDispatch, useSelector } from 'react-redux'; import ScannedDataBox from '../components/ScannedDataBox'; import { Ionicons } from '@expo/vector-icons'; import { RootState } from '../store'; import { QRCode } from '../types'; import { toggleBookmark, deleteQRCode } from '../actions/qrCodeActions'; const HistoryScreen: React.FC = () => { const dispatch = useDispatch(); const qrCodes = useSelector((state: RootState) => state.qrCodes); const [selectedData, setSelectedData] = useState(null); const [selectedScanResult, setSelectedScanResult] = useState(null); const [selectedType, setSelectedType] = useState(null); const [showBookmarks, setShowBookmarks] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false); const [indexToDelete, setIndexToDelete] = useState(null); useEffect(() => { const backAction = () => { if (selectedData) { setSelectedData(null); setSelectedScanResult(null); setSelectedType(null); return true; } return false; }; const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction); return () => backHandler.remove(); }, [selectedData]); const filteredQrCodes = showBookmarks ? qrCodes.filter(qr => qr.bookmarked) : qrCodes.slice().reverse(); const handleItemPress = (item: QRCode) => { setSelectedData(item.data); setSelectedScanResult(item.scanResult); setSelectedType(item.type); console.log('Selected QR code data:', item.data); console.log('Selected QR code type:', item.type); }; const confirmDelete = (index: number) => { setIndexToDelete(index); setIsModalVisible(true); console.log('Confirm delete for QR code at index:', index); }; const clearSelectedData = () => { setSelectedData(null); setSelectedScanResult(null); setSelectedType(null); }; return ( {/* Header for toggling between History and Bookmarks */} { setShowBookmarks(false); clearSelectedData(); }}> History { setShowBookmarks(true); clearSelectedData(); }}> Bookmarks {/* Display scanned data details */} {selectedData && ( )} {/* List of QR codes */} { console.log('Rendering QR code item:', item); const itemData = item.data; return ( handleItemPress(item)} style={styles.itemContent}> {itemData} {new Date().toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit' })} dispatch(toggleBookmark(index))}> confirmDelete(index)}> ); }} keyExtractor={(item, index) => index.toString()} contentContainerStyle={styles.flatListContent} /> {/* Modal for delete confirmation */} setIsModalVisible(false)} > Are you sure? If bookmarked, this will be removed from both History and Bookmarks. dispatch(deleteQRCode(indexToDelete))}> Yes, Delete setIsModalVisible(false)}> No, Keep It ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f8f0fc', padding: 20, }, headerContainer: { flexDirection: 'row', justifyContent: 'space-around', marginBottom: 20, }, headerTextActive: { fontSize: 24, fontWeight: 'bold', color: '#ff69b4', }, headerTextInactive: { fontSize: 24, fontWeight: 'bold', color: '#ccc', }, itemContainer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', backgroundColor: '#ffe6f0', padding: 10, borderRadius: 10, marginBottom: 10, }, itemLeft: { flexDirection: 'column', }, itemContent: { flexDirection: 'row', alignItems: 'center', }, itemRight: { flexDirection: 'row', alignItems: 'center', }, dataText: { fontSize: 11, color: '#000', marginLeft: 10, }, dateText: { fontSize: 12, color: '#000', marginLeft: 10, }, scanIcon: { width: 40, height: 40, }, flatListContent: { paddingBottom: 100, }, modalContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.5)', }, modalContent: { width: '80%', backgroundColor: 'white', borderRadius: 10, padding: 20, alignItems: 'center', }, modalTitle: { fontSize: 20, fontWeight: 'bold', marginBottom: 10, }, modalText: { fontSize: 16, marginBottom: 20, textAlign: 'center', }, modalButtons: { flexDirection: 'row', justifyContent: 'space-between', width: '100%', }, modalButton: { flex: 1, alignItems: 'center', padding: 10, }, modalButtonText: { fontSize: 16, color: '#000', }, scannedDataBoxContainer: { marginBottom: 20, }, }); export default HistoryScreen;