🎨 style(inputFileComponent): add loading spinner to input file component to indicate file upload progress

This commit is contained in:
Cristhian Zanforlin Lousa 2023-06-22 20:08:03 -03:00
commit 4470ca792c
2 changed files with 24 additions and 2 deletions

View file

@ -0,0 +1,9 @@
import { useContext, useEffect, useRef, useState } from "react";
import { RadialProgressType } from "../../types/components";
export default function LoadingSpinner({}) {
return (
<></>
);
}

View file

@ -5,6 +5,7 @@ import { FileComponentType } from "../../types/components";
import { TabsContext } from "../../contexts/tabsContext"; import { TabsContext } from "../../contexts/tabsContext";
import { INPUT_STYLE } from "../../constants"; import { INPUT_STYLE } from "../../constants";
import { uploadFile } from "../../controllers/API"; import { uploadFile } from "../../controllers/API";
import RadialProgressComponent from "../RadialProgress";
export default function InputFileComponent({ export default function InputFileComponent({
value, value,
@ -16,6 +17,7 @@ export default function InputFileComponent({
editNode = false, editNode = false,
}: FileComponentType) { }: FileComponentType) {
const [myValue, setMyValue] = useState(value); const [myValue, setMyValue] = useState(value);
const [loading, setLoading] = useState(false);
const { setErrorData } = useContext(alertContext); const { setErrorData } = useContext(alertContext);
const { tabId } = useContext(TabsContext); const { tabId } = useContext(TabsContext);
useEffect(() => { useEffect(() => {
@ -48,6 +50,8 @@ export default function InputFileComponent({
input.multiple = false; // Allow only one file selection input.multiple = false; // Allow only one file selection
input.onchange = (e: Event) => { input.onchange = (e: Event) => {
setLoading(true);
// Get the selected file // Get the selected file
const file = (e.target as HTMLInputElement).files?.[0]; const file = (e.target as HTMLInputElement).files?.[0];
@ -68,9 +72,11 @@ export default function InputFileComponent({
onChange(file.name); onChange(file.name);
// sets the value that goes to the backend // sets the value that goes to the backend
onFileChange(file_path); onFileChange(file_path);
setLoading(false);
}) })
.catch(() => { .catch(() => {
console.error("Error occurred while uploading file"); console.error("Error occurred while uploading file");
setLoading(false);
}); });
} else { } else {
// Show an error if the file type is not allowed // Show an error if the file type is not allowed
@ -79,6 +85,7 @@ export default function InputFileComponent({
"Please select a valid file. Only these file types are allowed:", "Please select a valid file. Only these file types are allowed:",
list: fileTypes, list: fileTypes,
}); });
setLoading(false);
} }
}; };
@ -92,7 +99,7 @@ export default function InputFileComponent({
disabled ? "pointer-events-none cursor-not-allowed w-full" : "w-full" disabled ? "pointer-events-none cursor-not-allowed w-full" : "w-full"
} }
> >
<div className="w-full flex items-center gap-3"> <div className="w-full flex items-center gap-2">
<span <span
onClick={handleButtonClick} onClick={handleButtonClick}
className={ className={
@ -107,9 +114,15 @@ export default function InputFileComponent({
{myValue !== "" ? myValue : "No file"} {myValue !== "" ? myValue : "No file"}
</span> </span>
<button onClick={handleButtonClick}> <button onClick={handleButtonClick}>
{!editNode && ( {(!editNode && !loading) && (
<DocumentMagnifyingGlassIcon className="w-8 h-8 hover:text-ring" /> <DocumentMagnifyingGlassIcon className="w-8 h-8 hover:text-ring" />
)} )}
{
(!editNode && loading) && (
<span className="loading loading-spinner loading-sm pl-3 w-8 h-8 pointer-events-none"></span>
)
}
</button> </button>
</div> </div>
</div> </div>