import React, {useState} from "react";
import axios from "axios";
import {HStack, Image, Spinner, Text, useToast} from "@chakra-ui/react";
import {Stack} from "@chakra-ui/layout";
import {uploadIcon} from "../../images";
export function PhotoUpload(props: any) {
const { onUpload, label, height, color } = props;
const toast = useToast()
const [uploading, setUploading] = useState(false);
const uploadUrl = 'https://image-uploading-servers.herokuapp.com/upload';
const getFile = (e: any) => {
uploadFile(e.target.files[0])
};
const uploadFile = (fileData: any) => {
if (!fileData)
return;
const data = new FormData();
data.append("file", fileData);
setUploading(true);
axios({
method: "POST",
url: uploadUrl,
data: data,
}).then((res) => {
onUpload(res.data.url)
resetFilerChooser();
setUploading(false);
toast({
title: 'Uploaded.',
description: "File uploaded",
status: 'success',
duration: 5000,
isClosable: true,
})
}
).catch(e => {
resetFilerChooser();
console.log("error ", e)
setUploading(false);
toast({
title: 'Failed.',
description: "Uploaded Failed Try Again",
status: 'error',
duration: 5000,
isClosable: true,
})
})
};
const openFilerChooser = (e: any) => {
// @ts-ignore
document.getElementById("photofile").click();
}
const resetFilerChooser = () => {
// @ts-ignore
document.getElementById("photofile").value = "";
}
return (
<>
{label}
{uploading ?
:
Upload Image
}
>
);
}