-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.tsx
59 lines (51 loc) · 1.55 KB
/
test.tsx
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
import { Box, Flex, Heading, Image, Spinner } from "@chakra-ui/react";
import { useState } from "react";
import { uploadPet } from "../db/utils/pokemon";
export default function Test() {
const allInputs = { imgUrl: "" };
const [imageAsFile, setImageAsFile] = useState(null);
const [resultImageUrl, setResultImageUrl] = useState("");
const handleImageAsFile = (e: any) => {
const image = e.target.files[0];
setImageAsFile(image);
console.log(imageAsFile);
};
const handleFireBaseUpload = async (e: any) => {
e.preventDefault();
console.log("start of upload");
// async magic goes here...
if (imageAsFile === null) {
console.error(`not an image, the image file is a ${typeof imageAsFile}`);
} else {
const result = await uploadPet(imageAsFile);
console.log(result);
setResultImageUrl(result.public_url);
}
};
return (
<Box p={4}>
<Heading>Upload an image of your pet!</Heading>
<form onSubmit={handleFireBaseUpload}>
<input type="file" onChange={handleImageAsFile} />
<button>Submit</button>
</form>
<Flex gap={10}>
{imageAsFile ? (
<Box >
<Image w="300px" src={URL.createObjectURL(imageAsFile)} />
</Box>
) : (
<></>
)}
{resultImageUrl != "" ? (
<Box >
<Image w="300px" src={resultImageUrl} />
</Box>
) : (
<></>
)}
{imageAsFile && resultImageUrl == "" ? <Spinner></Spinner> : <></>}
</Flex>
</Box>
);
}