Spaces:
Build error
Build error
File size: 2,338 Bytes
8040aeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
'use client';
import { useState, useEffect, useRef, ChangeEvent } from 'react';
import axios from 'axios';
import Canvas from './canvas';
import { BASE_URL } from './constants';
export default function Home() {
const [images, setImages] = useState<string[]>([]);
const [displayImage, setDisplayImage] = useState<string>('/loading.png');
const [displayIndex, setDisplayIndex] = useState<number>(0);
const getAllImages = async () => {
const res = await axios.get(`${BASE_URL}/v1/get_all_images`);
setImages(res.data.images);
}
const setCurrentImage = async () => {
if (images[displayIndex] === undefined) return;
const res = await axios.get(`${BASE_URL}/v1/get_image/${images[displayIndex]}`);
const contentType = res.headers['content-type'];
setDisplayImage(`data:${contentType};base64,${res.data}`);
}
const handleFileUpload = async (e: ChangeEvent<HTMLInputElement>) => {
const reader = new FileReader();
reader.addEventListener("load", async () => {
const newImageName = `img${images.length + 1}`;
const res = await axios.put(`${BASE_URL}/v1/upload_image/${newImageName}`,
{
image: reader.result,
}
);
setImages([...images, newImageName]);
setDisplayIndex(images.length);
}, false);
if (e.target.files) {
for (let i = 0; i < e.target.files.length; i++) {
reader.readAsDataURL(e.target.files[i]);
}
}
}
useEffect(() => {
getAllImages();
}, [])
useEffect(() => {
setCurrentImage();
}, [images, displayIndex])
return (
<>
<Canvas imageUrl={displayImage} imageName={images[displayIndex]} />
<br />
<br />
<div className="float right">
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-2"
onClick={() => {
setDisplayIndex(displayIndex - 1 >= 0 ? displayIndex - 1 : images.length - 1);
}}>Previous</button>
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-2"
onClick={() => {
setDisplayIndex(displayIndex + 1 < images.length ? displayIndex + 1 : 0);
}}>Next</button>
</div>
<br />
<input type="file" id="image" accept="image/png, image/jpeg" onChange={handleFileUpload} />
</>
)
};
|