grocery-app/frontend/src/components/modals/ImageUploadModal.jsx
2026-01-02 15:59:01 -08:00

103 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useRef, useState } from "react";
import "../../styles/ImageUploadModal.css";
export default function ImageUploadModal({ itemName, onConfirm, onSkip, onCancel }) {
const [selectedImage, setSelectedImage] = useState(null);
const [imagePreview, setImagePreview] = useState(null);
const cameraInputRef = useRef(null);
const galleryInputRef = useRef(null);
const handleImageChange = (e) => {
const file = e.target.files[0];
if (file) {
setSelectedImage(file);
const reader = new FileReader();
reader.onloadend = () => {
setImagePreview(reader.result);
};
reader.readAsDataURL(file);
}
};
const removeImage = () => {
setSelectedImage(null);
setImagePreview(null);
};
const handleConfirm = () => {
onConfirm(selectedImage);
};
const handleCancel = () => {
if (onCancel) {
onCancel();
}
};
const handleCameraClick = () => {
cameraInputRef.current?.click();
};
const handleGalleryClick = () => {
galleryInputRef.current?.click();
};
return (
<div className="image-upload-modal-overlay" onClick={handleCancel}>
<div className="image-upload-modal" onClick={(e) => e.stopPropagation()}>
<h2>Add Image for "{itemName}"</h2>
<p className="image-upload-subtitle">This is a new item. Would you like to add a verification image?</p>
<div className="image-upload-content">
{!imagePreview ? (
<div className="image-upload-options">
<button onClick={handleCameraClick} className="image-upload-option-btn camera">
📷 Use Camera
</button>
<button onClick={handleGalleryClick} className="image-upload-option-btn gallery">
🖼 Choose from Gallery
</button>
</div>
) : (
<div className="modal-image-preview">
<img src={imagePreview} alt="Preview" />
<button type="button" onClick={removeImage} className="modal-remove-image">
×
</button>
</div>
)}
</div>
<input
ref={cameraInputRef}
type="file"
accept="image/*"
capture="environment"
onChange={handleImageChange}
style={{ display: "none" }}
/>
<input
ref={galleryInputRef}
type="file"
accept="image/*"
onChange={handleImageChange}
style={{ display: "none" }}
/>
<div className="image-upload-actions">
<button onClick={handleCancel} className="image-upload-cancel">
Cancel
</button>
<button onClick={onSkip} className="image-upload-skip">
Skip
</button>
<button onClick={handleConfirm} className="image-upload-confirm" disabled={!selectedImage}>
{selectedImage ? "Add with Image" : "Select an Image"}
</button>
</div>
</div>
</div>
);
}