file image input
This commit is contained in:
parent
37d48faf16
commit
fb9b2d1d43
6 changed files with 183 additions and 17 deletions
|
|
@ -8,17 +8,23 @@ export default function IOInputField({
|
||||||
updateValue,
|
updateValue,
|
||||||
}: IOInputProps): JSX.Element | undefined {
|
}: IOInputProps): JSX.Element | undefined {
|
||||||
function handleInputType() {
|
function handleInputType() {
|
||||||
|
console.log(inputType);
|
||||||
|
|
||||||
|
const handleUpdateValue = (e) => {
|
||||||
|
updateValue(e, "text");
|
||||||
|
};
|
||||||
|
|
||||||
switch (inputType) {
|
switch (inputType) {
|
||||||
case "TextInput":
|
case "TextInput":
|
||||||
return (
|
return (
|
||||||
<Textarea
|
<Textarea
|
||||||
className="custom-scroll"
|
className="custom-scroll"
|
||||||
placeholder={"Enter text..."}
|
placeholder={"Enter text..."}
|
||||||
value={field.value}
|
value={field?.value}
|
||||||
onChange={updateValue}
|
onChange={handleUpdateValue}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
case "fileLoader":
|
case "FileLoader":
|
||||||
return <IOFileInput field={field} updateValue={updateValue} />;
|
return <IOFileInput field={field} updateValue={updateValue} />;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -26,8 +32,8 @@ export default function IOInputField({
|
||||||
<Textarea
|
<Textarea
|
||||||
className="custom-scroll"
|
className="custom-scroll"
|
||||||
placeholder={"Enter text..."}
|
placeholder={"Enter text..."}
|
||||||
value={field.value}
|
value={field?.value}
|
||||||
onChange={updateValue}
|
onChange={handleUpdateValue}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,151 @@
|
||||||
|
import { Button } from "../../ui/button";
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { BASE_URL_API } from "../../../constants/constants";
|
||||||
|
import { uploadFile } from "../../../controllers/API";
|
||||||
|
import useFlowsManagerStore from "../../../stores/flowsManagerStore";
|
||||||
import { IOFileInputProps } from "../../../types/components";
|
import { IOFileInputProps } from "../../../types/components";
|
||||||
|
import IconComponent from "../../genericIconComponent";
|
||||||
|
|
||||||
export default function IOFileInput({ field, updateValue }: IOFileInputProps) {
|
export default function IOFileInput({ field, updateValue }: IOFileInputProps) {
|
||||||
//component to handle file upload from chatIO
|
//component to handle file upload from chatIO
|
||||||
function handleCLick() {}
|
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
|
||||||
|
|
||||||
return <div></div>;
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
|
const [filePath, setFilePath] = useState("");
|
||||||
|
const [image, setImage] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (filePath) {
|
||||||
|
updateValue(filePath, "file");
|
||||||
|
}
|
||||||
|
}, [filePath]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (field) {
|
||||||
|
const fileName = field.split("/")[1];
|
||||||
|
const flowFileId = currentFlowId.toString();
|
||||||
|
setImage(`${BASE_URL_API}files/images/${flowFileId}/${fileName}`);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const dragOver = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (e.dataTransfer.types.some((types) => types === "Files")) {
|
||||||
|
setIsDragging(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const dragEnter = (e) => {
|
||||||
|
if (e.dataTransfer.types.some((types) => types === "Files")) {
|
||||||
|
setIsDragging(true);
|
||||||
|
}
|
||||||
|
e.preventDefault();
|
||||||
|
};
|
||||||
|
|
||||||
|
const dragLeave = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setIsDragging(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDrop = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (e.dataTransfer.files.length > 0) {
|
||||||
|
const file = e.dataTransfer.files[0];
|
||||||
|
|
||||||
|
upload(file);
|
||||||
|
|
||||||
|
const fileReader = new FileReader();
|
||||||
|
fileReader.onload = (event) => {
|
||||||
|
const imageDataUrl = event.target?.result as string;
|
||||||
|
setImage(imageDataUrl);
|
||||||
|
};
|
||||||
|
fileReader.readAsDataURL(file);
|
||||||
|
}
|
||||||
|
setIsDragging(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const upload = async (file) => {
|
||||||
|
if (file) {
|
||||||
|
// Check if a file was selected
|
||||||
|
const fileReader = new FileReader();
|
||||||
|
fileReader.onload = (event) => {
|
||||||
|
const imageDataUrl = event.target?.result as string;
|
||||||
|
setImage(imageDataUrl);
|
||||||
|
|
||||||
|
// Display the image on the screen
|
||||||
|
const imgElement = document.createElement("img");
|
||||||
|
imgElement.src = imageDataUrl;
|
||||||
|
document.body.appendChild(imgElement); // Add the image to the body or replace this with your desired location
|
||||||
|
};
|
||||||
|
fileReader.readAsDataURL(file);
|
||||||
|
|
||||||
|
uploadFile(file, currentFlowId)
|
||||||
|
.then((res) => res.data)
|
||||||
|
.then((data) => {
|
||||||
|
console.log("File uploaded successfully");
|
||||||
|
// Get the file name from the response
|
||||||
|
const { file_path, flowId } = data;
|
||||||
|
setFilePath(file_path);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
console.error("Error occurred while uploading file");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleButtonClick = (): void => {
|
||||||
|
// Create a file input element
|
||||||
|
const input = document.createElement("input");
|
||||||
|
input.type = "file";
|
||||||
|
input.style.display = "none"; // Hidden from view
|
||||||
|
input.multiple = false; // Allow only one file selection
|
||||||
|
input.onchange = (event: Event): void => {
|
||||||
|
// Get the selected file
|
||||||
|
const file = (event.target as HTMLInputElement).files?.[0];
|
||||||
|
upload(file);
|
||||||
|
};
|
||||||
|
// Trigger the file selection dialog
|
||||||
|
input.click();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div
|
||||||
|
onDragOver={dragOver}
|
||||||
|
onDragEnter={dragEnter}
|
||||||
|
onDragLeave={dragLeave}
|
||||||
|
onDrop={onDrop}
|
||||||
|
className={
|
||||||
|
"flex h-full w-full items-center justify-between" +
|
||||||
|
(isDragging
|
||||||
|
? "flex h-28 flex-col items-center justify-center gap-4 text-xs font-light"
|
||||||
|
: "")
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{!isDragging && (
|
||||||
|
<Button variant="primary" onClick={handleButtonClick}>
|
||||||
|
Upload or drop your file
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isDragging ? (
|
||||||
|
<>
|
||||||
|
<IconComponent name="ArrowUpToLine" className="h-5 w-5 stroke-1" />
|
||||||
|
"Drop your file here"
|
||||||
|
</>
|
||||||
|
) : image ? (
|
||||||
|
<img
|
||||||
|
className="order-last h-12 w-12 rounded-full object-cover "
|
||||||
|
src={image ?? ""}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<IconComponent name="SunIcon" className="h-8 w-8 stroke-1" />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,16 +67,27 @@ export default function IOView(): JSX.Element {
|
||||||
<div className="file-component-tab-column">
|
<div className="file-component-tab-column">
|
||||||
{node && (
|
{node && (
|
||||||
<IOInputField
|
<IOInputField
|
||||||
field={node.data.node!.template["value"]}
|
field={
|
||||||
|
node.data.node!.template["value"] ||
|
||||||
|
node.data.node!.template["file_path"]["value"]
|
||||||
|
}
|
||||||
inputType={input.type}
|
inputType={input.type}
|
||||||
updateValue={(e) => {
|
updateValue={(e, type) => {
|
||||||
e.target.value;
|
if (type === "file") {
|
||||||
|
if (node) {
|
||||||
|
let newNode = cloneDeep(node);
|
||||||
|
newNode.data.node!.template["file_path"].value =
|
||||||
|
e;
|
||||||
|
setNode(node.id, newNode);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (node) {
|
if (node) {
|
||||||
let newNode = cloneDeep(node);
|
let newNode = cloneDeep(node);
|
||||||
newNode.data.node!.template["value"].value =
|
newNode.data.node!.template["value"].value =
|
||||||
e.target.value;
|
e.target.value;
|
||||||
setNode(node.id, newNode);
|
setNode(node.id, newNode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
|
|
@ -519,6 +519,8 @@ export const ADMIN_HEADER_DESCRIPTION =
|
||||||
|
|
||||||
export const BASE_URL_API = "/api/v1/";
|
export const BASE_URL_API = "/api/v1/";
|
||||||
|
|
||||||
|
export const BACKEND_URL = "http://localhost:7860/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* URLs excluded from error retries.
|
* URLs excluded from error retries.
|
||||||
* @constant
|
* @constant
|
||||||
|
|
|
||||||
|
|
@ -865,3 +865,7 @@ export async function postBuildVertex(
|
||||||
): Promise<AxiosResponse<VertexBuildTypeAPI>> {
|
): Promise<AxiosResponse<VertexBuildTypeAPI>> {
|
||||||
return await api.post(`${BASE_URL_API}build/${flowId}/vertices/${vertexId}`);
|
return await api.post(`${BASE_URL_API}build/${flowId}/vertices/${vertexId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function downloadImage({ flowId, fileName }): Promise<any> {
|
||||||
|
return await api.get(`${BASE_URL_API}files/images/${flowId}/${fileName}`);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -650,10 +650,10 @@ export type dropdownButtonPropsType = {
|
||||||
export type IOInputProps = {
|
export type IOInputProps = {
|
||||||
inputType: string;
|
inputType: string;
|
||||||
field: TemplateVariableType;
|
field: TemplateVariableType;
|
||||||
updateValue: (e: any) => void;
|
updateValue: (e: any, type: string) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type IOFileInputProps = {
|
export type IOFileInputProps = {
|
||||||
field: TemplateVariableType;
|
field: TemplateVariableType;
|
||||||
updateValue: (e: any) => void;
|
updateValue: (e: any, type: string) => void;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue