Solve - KTP
This commit is contained in:
parent
12b0519995
commit
a2bbd88df7
@ -142,42 +142,52 @@ const Verify = () => {
|
|||||||
|
|
||||||
// Update handleImageUpload to include dimension checking
|
// Update handleImageUpload to include dimension checking
|
||||||
const handleImageUpload = async (file) => {
|
const handleImageUpload = async (file) => {
|
||||||
|
// Clear previous states
|
||||||
setErrorMessage('');
|
setErrorMessage('');
|
||||||
setFile(file);
|
setImageError('');
|
||||||
setSelectedImageName(file.name);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if file is PNG
|
// Validate file existence
|
||||||
|
if (!file) {
|
||||||
|
throw new Error('Please select a file');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check file type
|
||||||
if (file.type === 'image/png') {
|
if (file.type === 'image/png') {
|
||||||
setImageError('The image format is not suitable. Only JPG and JPEG files are allowed.');
|
throw new Error('The image format is not suitable. Only JPG and JPEG files are allowed.');
|
||||||
setFile(null);
|
|
||||||
setSelectedImageName('');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate file type
|
|
||||||
if (!fileTypes.includes(file.type)) {
|
if (!fileTypes.includes(file.type)) {
|
||||||
setImageError('Invalid file type. Only JPG and JPEG are allowed.');
|
throw new Error('Invalid file type. Only JPG and JPEG are allowed.');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate file size
|
// Check file size (2MB = 2 * 1024 * 1024 bytes)
|
||||||
if (file.size > 2 * 1024 * 1024) {
|
const maxSize = 2 * 1024 * 1024;
|
||||||
setImageError('File size exceeds 2MB.');
|
if (file.size > maxSize) {
|
||||||
return;
|
throw new Error('File size exceeds 2MB.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If all validations pass, then set the file states
|
||||||
|
setFile(file);
|
||||||
|
setSelectedImageName(file.name);
|
||||||
|
|
||||||
// Validate image dimensions
|
// Validate image dimensions
|
||||||
await checkImageDimensions(file);
|
await checkImageDimensions(file);
|
||||||
setImageError('');
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setImageError(error);
|
// Clear all file-related states when validation fails
|
||||||
setFile(null);
|
setFile(null);
|
||||||
setSelectedImageName('');
|
setSelectedImageName('');
|
||||||
|
setImageError(error.message);
|
||||||
|
|
||||||
|
// Reset file input
|
||||||
|
if (fileInputRef.current) {
|
||||||
|
fileInputRef.current.value = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleImageCancel = () => {
|
const handleImageCancel = () => {
|
||||||
setFile(null);
|
setFile(null);
|
||||||
setSelectedImageName('');
|
setSelectedImageName('');
|
||||||
@ -605,9 +615,15 @@ const Verify = () => {
|
|||||||
className="form-control"
|
className="form-control"
|
||||||
style={{ display: 'none' }}
|
style={{ display: 'none' }}
|
||||||
accept="image/jpeg, image/jpg"
|
accept="image/jpeg, image/jpg"
|
||||||
onChange={(e) => handleImageUpload(e.target.files[0])}
|
onChange={(e) => {
|
||||||
|
const selectedFile = e.target.files[0];
|
||||||
|
handleImageUpload(selectedFile);
|
||||||
|
// Reset input value to allow selecting the same file again
|
||||||
|
e.target.value = '';
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
{/* Display selected file info */}
|
{/* Display selected file info */}
|
||||||
{selectedImageName && (
|
{selectedImageName && (
|
||||||
<div className="mt-3">
|
<div className="mt-3">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user