chore(flowsContext.tsx): remove unused imports and variables for better code cleanliness

feat(flowsContext.tsx): add support for creating a new flow with a default name if no flow is provided
feat(flowsContext.tsx): add support for adding a version number to duplicate flow names
feat(flowsContext.tsx): add support for saving a flow to the database and updating its id
feat(flowsContext.tsx): add support for deleting a component flow
feat(extraSidebarComponent/index.tsx): add support for disabling save button when there are no nodes in the flow
This commit is contained in:
cristhianzl 2023-11-22 20:45:55 -03:00
commit eb8ff52ac2
2 changed files with 14 additions and 16 deletions

View file

@ -43,8 +43,6 @@ import {
createRandomKey,
getRandomDescription,
getRandomName,
getSetFromObject,
removeCountFromString,
} from "../utils/utils";
import { alertContext } from "./alertContext";
import { AuthContext } from "./authContext";
@ -504,27 +502,22 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
let flowData = flow
? processDataFromFlow(flow)
: { nodes: [], edges: [], viewport: { zoom: 1, x: 0, y: 0 } };
// Create a new flow with a default name if no flow is provided.
const newFlow = createNewFlow(flowData, flow!);
const newName = addVersionToDuplicates(newFlow, flows);
newFlow.name = newName;
try {
const { id } = await saveFlowToDatabase(newFlow);
// Change the id to the new id.
newFlow.id = id;
// Add the new flow to the list of flows.
addFlowToLocalState(newFlow);
// Return the id
return id;
} catch (error) {
@ -607,6 +600,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
}
async function saveFlow(newFlow: FlowType, silent?: boolean) {
if (newFlow?.data?.nodes?.length === 0) return;
try {
// updates flow in db
const updatedFlow = await updateFlowInDatabase(newFlow);
@ -652,8 +646,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
function deleteComponent(key: string) {
let componentFlow = flows.find(
(componentFlow) =>
componentFlow.is_component &&
componentFlow.name === key
componentFlow.is_component && componentFlow.name === key
);
if (componentFlow) {

View file

@ -252,9 +252,12 @@ export default function ExtraSidebar(): JSX.Element {
<ShadTooltip content="Save" side="top">
<div>
<button
disabled={flow?.data?.nodes.length === 0}
className={
"extra-side-bar-buttons " +
(isPending ? "" : "button-disable")
(isPending && flow!.data!.nodes?.length > 0
? ""
: "button-disable")
}
onClick={(event) => {
saveFlow({ ...flow!, data: reactFlowInstance!.toObject() });
@ -264,7 +267,9 @@ export default function ExtraSidebar(): JSX.Element {
name="Save"
className={
"side-bar-button-size" +
(isPending ? " " : " extra-side-bar-save-disable")
(isPending && flow!.data!.nodes?.length > 0
? " "
: " extra-side-bar-save-disable")
}
/>
</button>