fix(App.tsx): remove unused setErrorData and setLoading variables to improve code readability
feat(App.tsx): add support for successData, successOpen, and setSuccessOpen variables from alertContext to handle success alerts feat(App.tsx): add support for fetchError variable from typesContext to handle fetch errors fix(tabsContext.tsx): add setData variable to useContext to update data state feat(tabsContext.tsx): add support for saving custom components to data state in typesContext fix(typesContext.tsx): remove unused imports and commented code feat(typesContext.tsx): add support for saveFlowToDatabase function to save flow to the database fix(index.ts): add missing newline at the end of the file
This commit is contained in:
parent
cc6da7626e
commit
38d668e5f3
4 changed files with 49 additions and 27 deletions
|
|
@ -42,9 +42,7 @@ export default function App() {
|
||||||
successData,
|
successData,
|
||||||
successOpen,
|
successOpen,
|
||||||
setSuccessOpen,
|
setSuccessOpen,
|
||||||
setErrorData,
|
|
||||||
loading,
|
loading,
|
||||||
setLoading,
|
|
||||||
} = useContext(alertContext);
|
} = useContext(alertContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { fetchError } = useContext(typesContext);
|
const { fetchError } = useContext(typesContext);
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
const [flows, setFlows] = useState<Array<FlowType>>([]);
|
const [flows, setFlows] = useState<Array<FlowType>>([]);
|
||||||
const [id, setId] = useState(uid());
|
const [id, setId] = useState(uid());
|
||||||
const { templates, reactFlowInstance } = useContext(typesContext);
|
const { templates, reactFlowInstance, setData } = useContext(typesContext);
|
||||||
const [lastCopiedSelection, setLastCopiedSelection] = useState<{
|
const [lastCopiedSelection, setLastCopiedSelection] = useState<{
|
||||||
nodes: any;
|
nodes: any;
|
||||||
edges: any;
|
edges: any;
|
||||||
|
|
@ -139,11 +139,24 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function processDBData(DbData: FlowType[]) {
|
function processDBData(DbData: FlowType[]) {
|
||||||
|
let storeComponents: { [key: string]: APIClassType } = {};
|
||||||
DbData.forEach((flow: FlowType) => {
|
DbData.forEach((flow: FlowType) => {
|
||||||
try {
|
try {
|
||||||
if (!flow.data) {
|
if (!flow.data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (flow.data && flow.is_component) {
|
||||||
|
storeComponents[(flow.data.nodes[0].data as NodeDataType).type] = (
|
||||||
|
flow.data.nodes[0].data as NodeDataType
|
||||||
|
).node!;
|
||||||
|
}
|
||||||
|
setData((prev) => {
|
||||||
|
let newData = _.cloneDeep(prev);
|
||||||
|
Object.keys(storeComponents).forEach((key) => {
|
||||||
|
newData["custom_components"][key] = storeComponents[key];
|
||||||
|
});
|
||||||
|
return newData;
|
||||||
|
});
|
||||||
processDataFromFlow(flow, false);
|
processDataFromFlow(flow, false);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import {
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { Edge, Node, ReactFlowInstance } from "reactflow";
|
import { Edge, Node, ReactFlowInstance } from "reactflow";
|
||||||
import { getAll, getHealth, saveFlowStore } from "../controllers/API";
|
import { getAll, getHealth, saveFlowToDatabase } from "../controllers/API";
|
||||||
import { APIClassType, APIKindType } from "../types/api";
|
import { APIClassType, APIKindType } from "../types/api";
|
||||||
import { localStorageUserType } from "../types/entities";
|
import { localStorageUserType } from "../types/entities";
|
||||||
import { NodeDataType } from "../types/flow";
|
import { NodeDataType } from "../types/flow";
|
||||||
|
|
@ -72,19 +72,19 @@ export function TypesProvider({ children }: { children: ReactNode }) {
|
||||||
if (isMounted && result?.status === 200) {
|
if (isMounted && result?.status === 200) {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
let { data } = _.cloneDeep(result);
|
let { data } = _.cloneDeep(result);
|
||||||
const savedComponents = autoLogin
|
// const savedComponents = autoLogin
|
||||||
? localStorage.getItem("auto")
|
// ? localStorage.getItem("auto")
|
||||||
: localStorage.getItem(userData?.id!);
|
// : localStorage.getItem(userData?.id!);
|
||||||
if (savedComponents !== null) {
|
// if (savedComponents !== null) {
|
||||||
const { components }: localStorageUserType = JSON.parse(
|
// // const { components }: localStorageUserType = JSON.parse(
|
||||||
savedComponents!
|
// // savedComponents!
|
||||||
);
|
// // );
|
||||||
Object.keys(components).forEach((key) => {
|
// Object.keys(components).forEach((key) => {
|
||||||
data["custom_components"][key] = (
|
// data["custom_components"][key] = (
|
||||||
components[key].data?.nodes[0].data! as NodeDataType
|
// components[key].data?.nodes[0].data! as NodeDataType
|
||||||
).node!;
|
// ).node!;
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
setData(data);
|
setData(data);
|
||||||
setTemplates(
|
setTemplates(
|
||||||
Object.keys(data).reduce((acc, curr) => {
|
Object.keys(data).reduce((acc, curr) => {
|
||||||
|
|
@ -150,12 +150,12 @@ export function TypesProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveComponent(component: NodeDataType, id: string) {
|
function saveComponent(component: NodeDataType, id: string) {
|
||||||
let savedComponentsJSON: localStorageUserType = { components: {} };
|
// let savedComponentsJSON: localStorageUserType = { components: {} };
|
||||||
if (checkLocalStorageKey(id)) {
|
// if (checkLocalStorageKey(id)) {
|
||||||
let savedComponents = localStorage.getItem(id)!;
|
// let savedComponents = localStorage.getItem(id)!;
|
||||||
savedComponentsJSON = JSON.parse(savedComponents);
|
// savedComponentsJSON = JSON.parse(savedComponents);
|
||||||
}
|
// }
|
||||||
let components = savedComponentsJSON.components;
|
// let components = savedComponentsJSON.components;
|
||||||
let key = component.type;
|
let key = component.type;
|
||||||
if (data["custom_components"][key] !== undefined) {
|
if (data["custom_components"][key] !== undefined) {
|
||||||
let { newKey, increment } = IncrementObjectKey(
|
let { newKey, increment } = IncrementObjectKey(
|
||||||
|
|
@ -184,10 +184,10 @@ export function TypesProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
component.node!.official = false;
|
component.node!.official = false;
|
||||||
components[key] = createFlowComponent(component);
|
// components[key] = createFlowComponent(component);
|
||||||
saveFlowStore(createFlowComponent(component));
|
saveFlowToDatabase(createFlowComponent(component));
|
||||||
savedComponentsJSON.components = components;
|
// savedComponentsJSON.components = components;
|
||||||
localStorage.setItem(id, JSON.stringify(savedComponentsJSON));
|
// localStorage.setItem(id, JSON.stringify(savedComponentsJSON));
|
||||||
setData((prev) => {
|
setData((prev) => {
|
||||||
let newData = { ...prev };
|
let newData = { ...prev };
|
||||||
//clone to prevent reference erro
|
//clone to prevent reference erro
|
||||||
|
|
|
||||||
|
|
@ -112,12 +112,14 @@ export async function saveFlowToDatabase(newFlow: {
|
||||||
data: ReactFlowJsonObject | null;
|
data: ReactFlowJsonObject | null;
|
||||||
description: string;
|
description: string;
|
||||||
style?: FlowStyleType;
|
style?: FlowStyleType;
|
||||||
|
is_component?: boolean;
|
||||||
}): Promise<FlowType> {
|
}): Promise<FlowType> {
|
||||||
try {
|
try {
|
||||||
const response = await api.post(`${BASE_URL_API}flows/`, {
|
const response = await api.post(`${BASE_URL_API}flows/`, {
|
||||||
name: newFlow.name,
|
name: newFlow.name,
|
||||||
data: newFlow.data,
|
data: newFlow.data,
|
||||||
description: newFlow.description,
|
description: newFlow.description,
|
||||||
|
is_component: newFlow.is_component,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status !== 201) {
|
if (response.status !== 201) {
|
||||||
|
|
@ -568,3 +570,12 @@ export async function saveFlowStore(newFlow: {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the flows from the store.
|
||||||
|
* @returns {Promise<>} A promise that resolves to an AxiosResponse containing the build status.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export async function getFlowsStore(): Promise<AxiosResponse<FlowType[]>> {
|
||||||
|
return await api.get(`${BASE_URL_API}store/`);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue