import React, { useRef, useState } from 'react'; import { Box, Button, Input, Text } from '@chakra-ui/react'; interface CertificationImportProps { onImport: (csvContent: string) => { success: boolean; count?: number; error?: string }; } export const CertificationImport: React.FC = ({ onImport }) => { const fileInputRef = useRef(null); const [isLoading, setIsLoading] = useState(false); const [message, setMessage] = useState<{text: string, type: 'success' | 'error'} | null>(null); const handleFileChange = async (e: React.ChangeEvent) => { const files = e.target.files; if (!files || files.length === 0) return; setIsLoading(true); setMessage(null); try { const file = files[0]; const content = await readFileAsText(file); const result = onImport(content); if (result.success) { setMessage({ text: `Successfully imported ${result.count} certifications.`, type: 'success' }); } else { setMessage({ text: result.error || 'Unknown error occurred during import.', type: 'error' }); } } catch (error) { setMessage({ text: error instanceof Error ? error.message : 'Unknown error reading file.', type: 'error' }); } finally { setIsLoading(false); // Reset the file input if (fileInputRef.current) { fileInputRef.current.value = ''; } } }; const readFileAsText = (file: File): Promise => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = (e) => { if (e.target?.result) { resolve(e.target.result as string); } else { reject(new Error('Failed to read file')); } }; reader.onerror = () => reject(new Error('Failed to read file')); reader.readAsText(file); }); }; return ( Import certifications from CSV Expected format: columns for Name, Status, and Start date {message && ( {message.text} )} ); };