From 2939926c825daca60160d05d5526cbfa792057f5 Mon Sep 17 00:00:00 2001 From: Leszek Stachowski Date: Tue, 13 Jan 2026 20:02:33 +0100 Subject: [PATCH] fix: register document from ManageDocuments screen (#1597) * fix: register document from ManageDocuments screen * coderabbit suggestion --- .../management/ManageDocumentsScreen.tsx | 156 +++++++++++++----- 1 file changed, 118 insertions(+), 38 deletions(-) diff --git a/app/src/screens/documents/management/ManageDocumentsScreen.tsx b/app/src/screens/documents/management/ManageDocumentsScreen.tsx index 56785056f..260662c3a 100644 --- a/app/src/screens/documents/management/ManageDocumentsScreen.tsx +++ b/app/src/screens/documents/management/ManageDocumentsScreen.tsx @@ -8,7 +8,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { Button, ScrollView, Spinner, Text, XStack, YStack } from 'tamagui'; import { useNavigation } from '@react-navigation/native'; import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; -import { Check, Eraser } from '@tamagui/lucide-icons'; +import { Check, Eraser, HousePlus } from '@tamagui/lucide-icons'; import type { DocumentCatalog, @@ -33,6 +33,8 @@ import { usePassport } from '@/providers/passportDataProvider'; import { extraYPadding } from '@/utils/styleUtils'; const PassportDataSelector = () => { + const navigation = + useNavigation>(); const selfClient = useSelfClient(); const { loadDocumentCatalog, @@ -73,7 +75,20 @@ const PassportDataSelector = () => { loadPassportDataInfo(); }, [loadPassportDataInfo]); - const handleDocumentSelection = async (documentId: string) => { + const handleDocumentSelection = async ( + documentId: string, + isRegistered: boolean | undefined, + ) => { + if (!isRegistered) { + Alert.alert( + 'Document not registered', + 'This document cannot be selected as active, because it is not registered. Click the add button next to it to register it first.', + [{ text: 'OK', style: 'cancel' }], + ); + + return; + } + await setSelectedDocument(documentId); // Reload to update UI without loading state for quick operations const catalog = await loadDocumentCatalog(); @@ -90,24 +105,40 @@ const PassportDataSelector = () => { await loadPassportDataInfo(); }; - const handleDeleteButtonPress = (documentId: string) => { - Alert.alert( - '⚠️ Delete Document ⚠️', - 'Are you sure you want to delete this document?\n\nThis document is already linked to your identity in Self Protocol and cannot be linked by another person.', - [ - { - text: 'Cancel', - style: 'cancel', + const handleRegisterDocument = async (documentId: string) => { + try { + await setSelectedDocument(documentId); + navigation.navigate('ConfirmBelonging', {}); + } catch (error) { + Alert.alert( + 'Registration Error', + 'Failed to prepare document for registration. Please try again.', + [{ text: 'OK', style: 'cancel' }], + ); + } + }; + + const handleDeleteButtonPress = ( + documentId: string, + isRegistered: boolean | undefined, + ) => { + const message = isRegistered + ? 'Are you sure you want to delete this document?\n\nThis document is already linked to your identity in Self Protocol and cannot be linked by another person.' + : 'Are you sure you want to delete this document?'; + + Alert.alert('⚠️ Delete Document ⚠️', message, [ + { + text: 'Cancel', + style: 'cancel', + }, + { + text: 'Delete', + style: 'destructive', + onPress: async () => { + await handleDeleteSpecific(documentId); }, - { - text: 'Delete', - style: 'destructive', - onPress: async () => { - await handleDeleteSpecific(documentId); - }, - }, - ], - ); + }, + ]); }; const getDisplayName = (documentType: string): string => { @@ -156,6 +187,16 @@ const PassportDataSelector = () => { } }; + const getDocumentBackgroundColor = ( + isSelected: boolean, + isRegistered: boolean | undefined, + ): string => { + if (!isRegistered) { + return '#ffebee'; // Light red for unregistered documents + } + return isSelected ? '$gray2' : 'white'; + }; + if (loading) { return ( @@ -196,6 +237,10 @@ const PassportDataSelector = () => { ); } + const hasUnregisteredDocuments = documentCatalog.documents.some( + doc => !doc.isRegistered, + ); + return ( { > Available Documents + {hasUnregisteredDocuments && ( + + + ⚠️ We've detected some documents that are not registered. In order + to use them, you'll have to register them first by clicking the plus + icon next to them. + + + )} {documentCatalog.documents.map((metadata: DocumentMetadata) => ( { : borderColor } borderRadius="$3" - backgroundColor={ - documentCatalog.selectedDocumentId === metadata.id - ? '$gray2' - : 'white' + backgroundColor={getDocumentBackgroundColor( + documentCatalog.selectedDocumentId === metadata.id, + metadata.isRegistered, + )} + onPress={() => + handleDocumentSelection(metadata.id, metadata.isRegistered) } - onPress={() => handleDocumentSelection(metadata.id)} pressStyle={{ opacity: 0.8 }} > { } borderColor={textBlack} borderWidth={1} - onPress={() => handleDocumentSelection(metadata.id)} + onPress={() => + handleDocumentSelection(metadata.id, metadata.isRegistered) + } > {documentCatalog.selectedDocumentId === metadata.id && ( @@ -256,19 +319,36 @@ const PassportDataSelector = () => { - + + {metadata.isRegistered !== true && ( + + )} + + ))}