"use client" /* * File: DocumentUploadScreen.tsx * Description: Image upload screen for onboarding flow with react-native-image-picker. * Design & Developed by Tech4Biz Solutions * Copyright (c) Spurrin Innovations. All rights reserved. */ import type React from "react" import { useState } from "react" import { View, Text, StyleSheet, TouchableWithoutFeedback, Keyboard, TouchableOpacity, Alert, PermissionsAndroid, Platform, Image } from "react-native" import { launchImageLibrary, launchCamera, ImagePickerResponse, MediaType, ImagePickerOptions } from 'react-native-image-picker' import { Button } from "../../../../shared/src/components/Button" import { Colors, Spacing, Typography } from "../../../../shared/src/theme" import { showError, showSuccess } from "../../../../shared/src/utils/helpers/Toast" import Icon from "react-native-vector-icons/Feather" import OnboardingContainer from "./OnboardingContainer" import IconContainer from "./IconContainer" import { HospitalUploadSVG } from "../../../../shared/src/components/Icons/SvgIcons" interface SignUpData { email: string password: string first_name: string last_name: string username:string id_photo_url: {} hospital_id: string } interface DocumentUploadScreenProps { onContinue: (imageData: { uri: string name: string type: string size?: number }) => void onBack: () => void Data:SignUpData } interface ImageData { uri: string name: string type: string size?: number } const DocumentUploadScreen: React.FC = ({ onContinue, onBack ,Data }) => { const [selectedImage, setSelectedImage] = useState(Data.id_photo_url) const [loading, setLoading] = useState(false) // Request camera permission for Android const requestCameraPermission = async (): Promise => { if (Platform.OS === 'android') { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA, { title: 'Camera Permission', message: 'This app needs camera permission to capture images.', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'OK', } ) return granted === PermissionsAndroid.RESULTS.GRANTED } catch (err) { console.warn(err) return false } } return true } const handleImagePicker = () => { Alert.alert("Select Image", "Choose how you want to upload your image", [ { text: "Camera", onPress: () => handleCameraCapture(), }, { text: "Gallery", onPress: () => handleGalleryPicker(), }, { text: "Cancel", style: "cancel", }, ]) } const handleCameraCapture = async () => { const hasPermission = await requestCameraPermission() if (!hasPermission) { showError("Permission Error", "Camera permission is required to capture images.") return } const options: ImagePickerOptions = { mediaType: 'photo' as MediaType, quality: 0.8, maxWidth: 2000, maxHeight: 2000, includeBase64: false, } launchCamera(options, (response: ImagePickerResponse) => { if (response.didCancel) { return } if (response.errorMessage) { showError("Camera Error", response.errorMessage) return } if (response.assets && response.assets[0]) { const asset = response.assets[0] const imageData: ImageData = { uri: asset.uri!, name: asset.fileName || `image_${Date.now()}.jpg`, type: asset.type || 'image/jpeg', size: asset.fileSize, } setSelectedImage(imageData) showSuccess("Success", "Image captured successfully!") } }) } const handleGalleryPicker = () => { const options: ImagePickerOptions = { mediaType: 'photo' as MediaType, quality: 0.8, maxWidth: 2000, maxHeight: 2000, includeBase64: false, } launchImageLibrary(options, (response: ImagePickerResponse) => { if (response.didCancel) { return } if (response.errorMessage) { showError("Gallery Error", response.errorMessage) return } if (response.assets && response.assets[0]) { const asset = response.assets[0] const imageData: ImageData = { uri: asset.uri!, name: asset.fileName || `image_${Date.now()}.jpg`, type: asset.type || 'image/jpeg', size: asset.fileSize, } setSelectedImage(imageData) showSuccess("Success", "Image selected from gallery!") } }) } const formatFileSize = (bytes?: number): string => { if (!bytes) return '' const sizes = ['Bytes', 'KB', 'MB', 'GB'] if (bytes === 0) return '0 Bytes' const i = Math.floor(Math.log(bytes) / Math.log(1024)) return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i] } const getFileTypeDisplay = (type: string): string => { if (type.includes('jpeg') || type.includes('jpg')) return 'JPEG' if (type.includes('png')) return 'PNG' if (type.includes('gif')) return 'GIF' if (type.includes('webp')) return 'WebP' return 'Image' } const handleContinue = () => { if (!selectedImage) { showError("Validation Error", "Please upload an image to continue.") return } setLoading(true) setTimeout(() => { setLoading(false) onContinue(selectedImage) }, 1000) } return ( } /> Upload Image Please upload your profile picture or identification image. {selectedImage ? ( setSelectedImage(null)}> Image Uploaded {selectedImage.name} {getFileTypeDisplay(selectedImage.type)} {selectedImage.size && ( • {formatFileSize(selectedImage.size)} )} ) : ( <> Tap to upload image JPG, PNG supported )} {selectedImage && ( Change Image )}