feat: Updated frontend/src/components/ImageUpload.
This commit is contained in:
parent
183ddde941
commit
8bbee0b60e
@ -1,7 +1,7 @@
|
|||||||
import { useState, useEffect, useMemo, useCallback } from "react";
|
import { useState, useEffect, useMemo, useCallback } from "react";
|
||||||
import { useDropzone } from "react-dropzone";
|
import { useDropzone } from "react-dropzone";
|
||||||
// import { PromptImage } from "../../../types";
|
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
const baseStyle = {
|
const baseStyle = {
|
||||||
flex: 1,
|
flex: 1,
|
||||||
@ -48,16 +48,28 @@ function fileToDataURL(file: File) {
|
|||||||
type FileWithPreview = {
|
type FileWithPreview = {
|
||||||
preview: string;
|
preview: string;
|
||||||
} & File;
|
} & File;
|
||||||
|
);
|
||||||
interface Props {
|
|
||||||
setReferenceImages: (referenceImages: string[]) => void;
|
// Convert images to data URLs and set the prompt images state
|
||||||
}
|
Promise.all(acceptedFiles.map((file) => fileToDataURL(file)))
|
||||||
|
.then((dataUrls) => {
|
||||||
function ImageUpload({ setReferenceImages }: Props) {
|
setReferenceImages(dataUrls.map((dataUrl) => dataUrl as string));
|
||||||
const [files, setFiles] = useState<FileWithPreview[]>([]);
|
// Make a POST request to the /extract-colors endpoint with the uploaded image
|
||||||
const { getRootProps, getInputProps, isFocused, isDragAccept, isDragReject } =
|
axios.post('/extract-colors', { image: dataUrls[0] })
|
||||||
useDropzone({
|
.then((response) => {
|
||||||
maxFiles: 1,
|
// Store the returned colors in the component's state
|
||||||
|
setColors(response.data.colors);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
toast.error("Error extracting colors: " + error);
|
||||||
|
console.error("Error extracting colors:", error);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
toast.error("Error reading files" + error);
|
||||||
|
console.error("Error reading files:", error);
|
||||||
|
});
|
||||||
|
},
|
||||||
maxSize: 1024 * 1024 * 5, // 5 MB
|
maxSize: 1024 * 1024 * 5, // 5 MB
|
||||||
accept: {
|
accept: {
|
||||||
"image/png": [".png"],
|
"image/png": [".png"],
|
||||||
@ -65,6 +77,7 @@ function ImageUpload({ setReferenceImages }: Props) {
|
|||||||
"image/jpg": [".jpg"],
|
"image/jpg": [".jpg"],
|
||||||
},
|
},
|
||||||
onDrop: (acceptedFiles) => {
|
onDrop: (acceptedFiles) => {
|
||||||
|
const [colors, setColors] = useState<string[]>([]);
|
||||||
// Set up the preview thumbnail images
|
// Set up the preview thumbnail images
|
||||||
setFiles(
|
setFiles(
|
||||||
acceptedFiles.map((file: File) =>
|
acceptedFiles.map((file: File) =>
|
||||||
@ -147,3 +160,8 @@ function ImageUpload({ setReferenceImages }: Props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default ImageUpload;
|
export default ImageUpload;
|
||||||
|
<div className="colors">
|
||||||
|
{colors.map((color, index) => (
|
||||||
|
<div key={index} style={{ backgroundColor: color, width: '50px', height: '50px' }}></div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user