Added importing flow to the same flow

This commit is contained in:
Lucas Oliveira 2023-06-13 20:07:08 -03:00
commit bc254a8aee
3 changed files with 33 additions and 28 deletions

View file

@ -263,7 +263,7 @@ export function TabsProvider({ 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.
*/ */
function uploadFlow() { function uploadFlow(newProject?: boolean) {
// create a file input // create a file input
const input = document.createElement("input"); const input = document.createElement("input");
input.type = "file"; input.type = "file";
@ -278,7 +278,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
// parse the text into a JSON object // parse the text into a JSON object
let flow: FlowType = JSON.parse(text); let flow: FlowType = JSON.parse(text);
addFlow(flow); addFlow(flow, newProject);
}); });
} }
}; };
@ -415,30 +415,35 @@ export function TabsProvider({ children }: { children: ReactNode }) {
reactFlowInstance.setEdges(edges); reactFlowInstance.setEdges(edges);
} }
const addFlow = async (flow?: FlowType): Promise<String> => { const addFlow = async (flow?: FlowType, newProject?: Boolean): Promise<String> => {
let flowData = extractDataFromFlow(flow); if(newProject){
if(flowData.description == ""){ let flowData = extractDataFromFlow(flow);
flowData.description = "This is a new flow."; if(flowData.description == ""){
flowData.description = "This is a new flow.";
}
// Create a new flow with a default name if no flow is provided.
const newFlow = createNewFlow(flowData, flow);
try {
const id = await saveFlowToDatabase(newFlow);
// Change the id to the new id.
newFlow.id = id.id;
// Add the new flow to the list of flows.
addFlowToLocalState(newFlow);
// Return the id
return id.id;
} catch (error) {
// Handle the error if needed
console.error('Error while adding flow:', error);
throw error; // Re-throw the error so the caller can handle it if needed
}
} else {
paste({nodes: flow.data.nodes, edges: flow.data.edges}, {x:10, y:10})
} }
// Create a new flow with a default name if no flow is provided.
const newFlow = createNewFlow(flowData, flow);
try {
const id = await saveFlowToDatabase(newFlow);
// Change the id to the new id.
newFlow.id = id.id;
// Add the new flow to the list of flows.
addFlowToLocalState(newFlow);
// Return the id
return id.id;
} catch (error) {
// Handle the error if needed
console.error('Error while adding flow:', error);
throw error; // Re-throw the error so the caller can handle it if needed
}
}; };
const extractDataFromFlow = (flow) => { const extractDataFromFlow = (flow) => {

View file

@ -160,7 +160,7 @@ export default function ImportModal() {
<ComputerDesktopIcon className="h-10 w-10 flex-shrink-0" /> <ComputerDesktopIcon className="h-10 w-10 flex-shrink-0" />
} }
onClick={() => { onClick={() => {
uploadFlow(); uploadFlow(false);
setModalOpen(false); setModalOpen(false);
}} }}
textColor="text-blue-500 dark:text-blue-500/75" textColor="text-blue-500 dark:text-blue-500/75"
@ -189,7 +189,7 @@ export default function ImportModal() {
<DocumentDuplicateIcon className="h-6 w-6 flex-shrink-0" /> <DocumentDuplicateIcon className="h-6 w-6 flex-shrink-0" />
} }
onClick={() => { onClick={() => {
addFlow(example); addFlow(example, false);
setModalOpen(false); setModalOpen(false);
}} }}
textColor="text-emerald-500 dark:text-emerald-500/75" textColor="text-emerald-500 dark:text-emerald-500/75"

View file

@ -5,7 +5,7 @@ export type TabsContextType = {
setTabId: (index: string) => void; setTabId: (index: string) => void;
flows: Array<FlowType>; flows: Array<FlowType>;
removeFlow: (id: string) => void; removeFlow: (id: string) => void;
addFlow: (flowData?: FlowType, newFlow?: boolean) => Promise<String>; addFlow: (flowData?: FlowType, newProject?: boolean) => Promise<String>;
updateFlow: (newFlow: FlowType) => void; updateFlow: (newFlow: FlowType) => void;
incrementNodeId: () => string; incrementNodeId: () => string;
downloadFlow: (flow: FlowType) => void; downloadFlow: (flow: FlowType) => void;