import React, { useState } from 'react';
import {
View,
Text,
ScrollView,
TouchableOpacity,
Alert,
ActivityIndicator,
} from 'react-native';
import { useAppTheme } from '@theme';
import { ScreenHeader, KycDocumentCard, LoadingOverlay } from '@components';
import { useNavigation, useFocusEffect } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { OnBoardingParamList } from '@navigation/navigationTypes';
import { RouteNames } from '@utils/constants';
import Svg, { Path } from 'react-native-svg';
import { useAppDispatch, useAppSelector } from '@store';
import { uploadKyc, fetchMyKyc } from './thunk';
import { getStyles } from './kycScreen.styles';
// Interface for Document state matching KycDocumentCard structure
interface KycDocument {
id: string;
name: string;
idNumber: string;
fileName: string | null;
fileUri: string | null;
fileType: string | null;
status?: string;
}
// Inline Cloud Upload SVG representation for the top banner card
const CloudUploadIcon = ({ size = 22, color = '#666' }) => (
);
// Inline Plus icon for adding a new document
const PlusIcon = ({ size = 16, color = '#333' }) => (
);
// Inline Submit/Disk icon for the submit button
const SubmitIcon = ({ size = 18, color = '#fff' }) => (
);
export const KycScreen: React.FC = () => {
const { colors } = useAppTheme();
const styles = getStyles(colors);
const navigation =
useNavigation>();
const dispatch = useAppDispatch();
// Get upload loading status from KYC store slice
const { isLoading, myKycResponse } = useAppSelector(state => state.kyc);
const [documents, setDocuments] = useState([
{
id: '1',
name: '',
idNumber: '',
fileName: null,
fileUri: null,
fileType: null,
},
]);
// Fetch current KYC verification status when screen is focused
useFocusEffect(
React.useCallback(() => {
dispatch(fetchMyKyc());
}, [dispatch]),
);
const handleAddDocument = () => {
setDocuments([
...documents,
{
id: Date.now().toString(),
name: '',
idNumber: '',
fileName: null,
fileUri: null,
fileType: null,
},
]);
};
const handleRemoveDocument = (id: string) => {
setDocuments(documents.filter(doc => doc.id !== id));
};
const handleUpdateDocument = (
id: string,
updatedFields: Partial,
) => {
setDocuments(
documents.map(doc =>
doc.id === id ? { ...doc, ...updatedFields } : doc,
),
);
};
const handleSubmit = () => {
// Validation
for (let i = 0; i < documents.length; i++) {
const doc = documents[i];
if (!doc.name.trim()) {
Alert.alert(
'Required Field',
`Please fill in the Document Name/Type for Document ${i + 1}.`,
);
return;
}
if (!doc.idNumber.trim()) {
Alert.alert(
'Required Field',
`Please fill in the Document Identification Number for Document ${
i + 1
}.`,
);
return;
}
if (!doc.fileName || !doc.fileUri) {
Alert.alert(
'Required Field',
`Please select and attach a document file for Document ${i + 1}.`,
);
return;
}
}
// Build files and metadata array payloads
const payloadFiles = documents.map(doc => ({
uri: doc.fileUri || '',
name: doc.fileName || '',
type: doc.fileName?.endsWith('.pdf') ? 'application/pdf' : 'image/png',
}));
const payloadMetadata = documents.map(doc => ({
docName: doc.name,
docNumber: doc.idNumber,
}));
dispatch(
uploadKyc({
files: payloadFiles,
metadata: payloadMetadata,
}),
)
.unwrap()
.then(() => {
Alert.alert('Success', 'KYC Documents submitted successfully!', [
{
text: 'OK',
onPress: () => {
dispatch(fetchMyKyc());
navigation.navigate(RouteNames.OnboardingComplete);
},
},
]);
})
.catch(err => {
Alert.alert(
'Upload Failed',
err || 'Failed to submit KYC documents. Please try again.',
);
});
};
// Determine if documents exist to render in read-only mode
const hasSubmittedDocs = !!(
myKycResponse &&
myKycResponse.documents &&
myKycResponse.documents.length > 0
);
// Map backend documents if submitted, else use local state
const displayedDocuments: KycDocument[] = hasSubmittedDocs
? myKycResponse.documents.map(doc => ({
id: doc.id,
name: doc.documentType,
idNumber: doc.documentNumber,
fileName: doc.fileName,
fileUri: doc.fileUrl,
fileType: doc.fileName.endsWith('.pdf')
? 'application/pdf'
: 'image/png',
status: doc.status,
}))
: documents;
return (
navigation.goBack()}
/>
{/* Compliance Check Required Banner */}
KYC Compliance Check Required
Please upload store identity documents to configure payouts,
deliveries, and operational scopes.
{/* Upload KYC verification documents main card header info */}
{hasSubmittedDocs
? 'Submitted KYC Documents'
: 'Upload KYC Verification Documents'}
{hasSubmittedDocs
? 'Below are the KYC documents submitted for verification.'
: 'Provide details and attachments to initiate compliance verification.'}
{/* Render each document card */}
{displayedDocuments.map((doc, idx) => (
handleUpdateDocument(doc.id, fields)
: undefined
}
onRemove={
!hasSubmittedDocs && idx > 0
? () => handleRemoveDocument(doc.id)
: undefined
}
readOnly={hasSubmittedDocs}
/>
))}
{!hasSubmittedDocs && (
<>
{/* Add another document button */}
Add Another Document
{/* Upload & Submit KYC Action Button */}
{isLoading ? (
) : (
<>
Upload & Submit KYC
>
)}
>
)}
{hasSubmittedDocs && (
{
navigation.navigate(RouteNames.OnboardingComplete);
}}
disabled={isLoading}
activeOpacity={0.8}
>
{isLoading ? (
) : (
<>
Next Page
>
)}
)}
{/* Loading Overlay for the focus fetch check */}
);
};
export default KycScreen;