import React, { useState, useEffect, useRef } from 'react'; import { Link } from 'react-router-dom'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faChevronDown, faChevronLeft, faImage, faTimes } from '@fortawesome/free-solid-svg-icons'; const Verify = () => { const BASE_URL = process.env.REACT_APP_BASE_URL; const API_KEY = process.env.REACT_APP_API_KEY; const fileTypes = ["image/jpeg", "image/png"]; // Use MIME types for better file validation const fileInputRef = useRef(null); const [isSelectOpen, setIsSelectOpen] = useState(false); const [errorMessage, setErrorMessage] = useState(''); const [selectedImageName, setSelectedImageName] = useState(''); const [file, setFile] = useState(null); const [applicationId, setApplicationId] = useState(''); const [isLoading, setIsLoading] = useState(false); const [applicationIds, setApplicationIds] = useState([]); const [imageError, setImageError] = useState(''); const [data, setData] = useState(null); const [showResult, setShowResult] = useState(false); // Fetch Application IDs useEffect(() => { const fetchApplicationIds = async () => { try { setIsLoading(true); const response = await fetch(`${BASE_URL}/application/list`, { method: 'GET', headers: { 'accept': 'application/json', 'x-api-key': API_KEY, }, }); if (!response.ok) { throw new Error('Failed to fetch application IDs'); } const data = await response.json(); if (data.status_code === 200) { setApplicationIds(data.details.data); } else { throw new Error('Failed to fetch application IDs'); } } catch (error) { setErrorMessage(error.message || 'Error fetching application IDs'); } finally { setIsLoading(false); } }; fetchApplicationIds(); }, []); const handleFocus = () => setIsSelectOpen(true); const handleBlur = () => setIsSelectOpen(false); // Handle file upload const handleImageUpload = (e) => { const file = e.target.files[0]; if (!file) return; const fileType = file.type; // Use MIME type instead of file extension if (!fileTypes.includes(fileType)) { setImageError('Image format is not supported'); setFile(null); return; } if (file.size > 2 * 1024 * 1024) { // 2MB check setImageError('File size exceeds 2MB'); setFile(null); return; } setSelectedImageName(file.name); setFile(file); setImageError(''); }; // Cancel file upload const handleImageCancel = () => { setSelectedImageName(''); setFile(null); setImageError(''); fileInputRef.current.value = ''; }; // Submit form and trigger OCR API const handleCheckClick = async () => { if (!file || !applicationId) { setErrorMessage('Please select an application and upload a file.'); return; } setIsLoading(true); const formData = new FormData(); formData.append('application_id', applicationId); formData.append('file', file); console.log(`id: ${applicationId}, file: ${file}`) try { const response = await fetch(`${BASE_URL}/ocr-ktp`, { method: 'POST', headers: { 'accept': 'application/json', 'x-api-key': API_KEY, }, body: formData, }); if (!response.ok) { throw new Error('OCR processing failed'); } const result = await response.json(); console.log('Full response:', result); // Log full response to inspect the structure if (result.status_code === 201) { console.log('Success'); // Correct the path to access the data-ktp object const responseData = result.details.data?.['data-ktp'] || {}; // Log each field to inspect the data console.log('NIK:', responseData.nik); console.log('District:', responseData.kecamatan); console.log('Name:', responseData.nama); console.log('City:', responseData.kabkot); console.log('Date of Birth:', responseData.tanggal_lahir); console.log('State:', responseData.provinsi); console.log('Gender:', responseData.jenis_kelamin); console.log('Religion:', responseData.agama); console.log('Blood Type:', responseData.golongan_darah); console.log('Marital Status:', responseData.status_perkawinan); console.log('Address:', responseData.alamat); console.log('Occupation:', responseData.pekerjaan); console.log('RT/RW:', `${responseData.rt}/${responseData.rw}`); console.log('Nationality:', responseData.kewarganegaraan); console.log('Image URL:', result.details.image_url); console.log('Dark:', responseData.dark); console.log('Blur:', responseData.blur); console.log('Grayscale:', responseData.grayscale); console.log('Flashlight:', responseData.flashlight); // Map the response data to a new object with default values if the property doesn't exist const data = { nik: responseData.nik || 'N/A', district: responseData.kecamatan || 'N/A', name: responseData.nama || 'N/A', city: responseData.kabkot || 'N/A', dob: responseData.tanggal_lahir || 'N/A', state: responseData.provinsi || 'N/A', gender: responseData.jenis_kelamin || 'N/A', religion: responseData.agama || 'N/A', bloodType: responseData.golongan_darah || 'N/A', maritalStatus: responseData.status_perkawinan || 'N/A', address: responseData.alamat || 'N/A', occupation: responseData.pekerjaan || 'N/A', rtRw: `${responseData.rt || 'N/A'}/${responseData.rw || 'N/A'}`, nationality: responseData.kewarganegaraan || 'N/A', imageUrl: result.details.image_url || '', dark: responseData.dark || 'N/A', blur: responseData.blur || 'N/A', grayscale: responseData.grayscale || 'N/A', flashlight: responseData.flashlight || 'N/A', }; setData(data); setShowResult(true); setErrorMessage(''); setSelectedImageName(''); } else { setErrorMessage('OCR processing failed'); } } catch (error) { setErrorMessage(error.message || 'Error during OCR processing'); } finally { setIsLoading(false); } }; return (
Loading...
Get started now by creating an Application ID and explore all the demo services available on the dashboard. Experience the ease and flexibility of trying out all our features firsthand.
Remaining Quota
File: {selectedImageName}
{imageError}
}{errorMessage}
NIK | {data.nik} |
District | {data.district} |
Name | {data.name} |
City | {data.city} |
Date of Birth | {data.dob} |
State | {data.state} |
Gender | {data.gender} |
Religion | {data.religion} |
Blood Type | {data.bloodType} |
Marital Status | {data.maritalStatus} |
Address | {data.address} |
Occupation | {data.occupation} |
RT/RW | {data.rtRw} |
Nationality | {data.nationality} |
Image |