Fixed flows being able to upload to components and vice versa

This commit is contained in:
Lucas Oliveira 2023-11-21 20:19:29 -03:00
commit 3d32be344d
4 changed files with 104 additions and 50 deletions

View file

@ -318,46 +318,71 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
* If the file type is application/json, the file is read and parsed into a JSON object. * If the file type is application/json, the file is read and parsed into a JSON object.
* The resulting JSON object is passed to the addFlow function. * The resulting JSON object is passed to the addFlow function.
*/ */
async function uploadFlow( async function uploadFlow({
newProject: boolean, newProject,
file?: File file,
): Promise<String | undefined> { isComponent = false,
let id; }: {
if (file) { newProject: boolean;
let text = await file.text(); file?: File;
let fileData = JSON.parse(text); isComponent?: boolean;
if (fileData.flows) { }): Promise<String | never> {
fileData.flows.forEach((flow: FlowType) => { return new Promise(async (resolve, reject) => {
id = addFlow(newProject, flow); let id;
}); if (file) {
} let text = await file.text();
// parse the text into a JSON object let fileData = JSON.parse(text);
let flow: FlowType = JSON.parse(text); console.log(fileData);
if (fileData.is_component === undefined) {
id = await addFlow(newProject, flow); reject("Your file doesn't have the is_component property.");
} else { } else if (
// create a file input fileData.is_component !== undefined &&
const input = document.createElement("input"); fileData.is_component !== isComponent
input.type = "file"; ) {
input.accept = ".json"; reject("You cannot upload a component as a flow or vice versa");
// add a change event listener to the file input } else {
id = await new Promise((resolve) => { if (fileData.flows) {
fileData.flows.forEach((flow: FlowType) => {
id = addFlow(newProject, flow);
});
resolve("");
} else {
id = await addFlow(newProject, fileData);
resolve(id);
}
}
} else {
// create a file input
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
// add a change event listener to the file input
input.onchange = async (e: Event) => { input.onchange = async (e: Event) => {
if ( if (
(e.target as HTMLInputElement).files![0].type === "application/json" (e.target as HTMLInputElement).files![0].type === "application/json"
) { ) {
const currentfile = (e.target as HTMLInputElement).files![0]; const currentfile = (e.target as HTMLInputElement).files![0];
let text = await currentfile.text(); let text = await currentfile.text();
let flow: FlowType = JSON.parse(text); let fileData: FlowType = await JSON.parse(text);
const flowId = await addFlow(newProject, flow); console.log(isComponent, fileData);
resolve(flowId);
if (fileData.is_component === undefined) {
reject("Your file doesn't have the is_component property.");
} else if (
fileData.is_component !== undefined &&
fileData.is_component !== isComponent
) {
reject("You cannot upload a component as a flow or vice versa");
} else {
id = await addFlow(newProject, fileData);
resolve(id);
}
} }
}; };
// trigger the file input click event to open the file dialog // trigger the file input click event to open the file dialog
input.click(); input.click();
}); }
} });
return id;
} }
function uploadFlows() { function uploadFlows() {

View file

@ -16,7 +16,7 @@ export default function ComponentsComponent({
is_component?: boolean; is_component?: boolean;
}) { }) {
const { flows, removeFlow, uploadFlow, isLoading } = useContext(FlowsContext); const { flows, removeFlow, uploadFlow, isLoading } = useContext(FlowsContext);
const { setErrorData } = useContext(alertContext); const { setErrorData, setSuccessData } = useContext(alertContext);
const [pageSize, setPageSize] = useState(10); const [pageSize, setPageSize] = useState(10);
const [pageIndex, setPageIndex] = useState(1); const [pageIndex, setPageIndex] = useState(1);
const [allData, setAllData] = useState( const [allData, setAllData] = useState(
@ -43,14 +43,24 @@ export default function ComponentsComponent({
e.preventDefault(); e.preventDefault();
if (e.dataTransfer.types.some((types) => types === "Files")) { if (e.dataTransfer.types.some((types) => types === "Files")) {
if (e.dataTransfer.files.item(0).type === "application/json") { if (e.dataTransfer.files.item(0).type === "application/json") {
try { uploadFlow({
uploadFlow(true, e.dataTransfer.files.item(0)!); newProject: true,
} catch (error: any) { file: e.dataTransfer.files.item(0)!,
setErrorData({ isComponent: is_component,
title: "Error uploading file", })
list: [error.message], .then(() => {
setSuccessData({
title: `${
is_component ? "Component" : "Flow"
} uploaded successfully`,
});
})
.catch((error) => {
setErrorData({
title: "Error uploading file",
list: [error],
});
}); });
}
} else { } else {
setErrorData({ setErrorData({
title: "Invalid file type", title: "Invalid file type",

View file

@ -1,5 +1,5 @@
import { useContext, useEffect } from "react"; import { useContext, useEffect } from "react";
import { Outlet, useNavigate } from "react-router-dom"; import { Outlet, useLocation, useNavigate } from "react-router-dom";
import DropdownButton from "../../components/DropdownButtonComponent"; import DropdownButton from "../../components/DropdownButtonComponent";
import IconComponent from "../../components/genericIconComponent"; import IconComponent from "../../components/genericIconComponent";
import PageLayout from "../../components/pageLayout"; import PageLayout from "../../components/pageLayout";
@ -19,21 +19,32 @@ export default function HomePage(): JSX.Element {
uploadFlow, uploadFlow,
isLoading, isLoading,
} = useContext(FlowsContext); } = useContext(FlowsContext);
const { setErrorData } = useContext(alertContext); const { setErrorData, setSuccessData } = useContext(alertContext);
const location = useLocation();
const pathname = location.pathname;
const is_component = pathname === "/components";
const dropdownOptions = [ const dropdownOptions = [
{ {
name: "Import from JSON", name: "Import from JSON",
onBtnClick: () => { onBtnClick: () => {
try { uploadFlow({
uploadFlow(true).then((id) => { newProject: true,
navigate("/flow/" + id); isComponent: is_component,
})
.then((id) => {
setSuccessData({
title: `${
is_component ? "Component" : "Flow"
} uploaded successfully`,
});
if (!is_component) navigate("/flow/" + id);
})
.catch((error) => {
setErrorData({
title: "Error uploading file",
list: [error],
});
}); });
} catch (error: any) {
setErrorData({
title: "Invalid file type",
list: [error],
});
}
}, },
}, },
]; ];

View file

@ -23,7 +23,15 @@ export type FlowsContextType = {
uploadFlows: () => void; uploadFlows: () => void;
isBuilt: boolean; isBuilt: boolean;
setIsBuilt: (state: boolean) => void; setIsBuilt: (state: boolean) => void;
uploadFlow: (newFlow: boolean, file?: File) => Promise<String | undefined>; uploadFlow: ({
newProject,
file,
isComponent,
}: {
newProject: boolean;
file?: File;
isComponent?: boolean;
}) => Promise<String | never>;
hardReset: () => void; hardReset: () => void;
getNodeId: (nodeType: string) => string; getNodeId: (nodeType: string) => string;
tabsState: FlowsState; tabsState: FlowsState;