fix(tabsContext.tsx): rename processDBData function to processFlows to improve semantics

fix(tabsContext.tsx): add skipUpdate parameter to processFlows function to control whether to update state or not
fix(tabsContext.tsx): remove setErrorData call in processFlows function to prevent unnecessary error logging
fix(tabsContext.tsx): remove unused error handling in processFlows function
fix(tabsContext.tsx): remove unused saveFlowToDatabase and addFlowToLocalState calls in deleteFlowFromDatabase function
fix(tabsContext.tsx): remove unused saveComponent function and addFlowToLocalState call in deleteComponent function
fix(tabsContext.tsx): remove unused increment variable in saveComponent function
fix(tabsContext.tsx): remove unused res parameter in saveComponent function
fix(tabsContext.tsx): remove unused console.error call in saveComponent function
feat(tabsContext.tsx): add support for skipping node update in processFlows function
feat(tabsContext.tsx): add support for removing component from data when deleting a flow if it is a custom component
feat(tabsContext.tsx): add support for adding new flow to local state and processing it in addFlowToLocalState function
feat(tabsContext.tsx): set component node official property to false in saveComponent function before saving to database
feat(tabsContext.tsx): remove addFlowToLocalState call and addFlow function call in saveComponent function
This commit is contained in:
anovazzi1 2023-10-23 17:03:27 -03:00
commit 9ec4784afb

View file

@ -125,7 +125,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
getTabsDataFromDB().then((DbData) => {
if (DbData && Object.keys(templates).length > 0) {
try {
processDBData(DbData);
processFlows(DbData, false);
updateStateWithDbData(DbData);
setIsLoading(false);
} catch (e) {}
@ -146,7 +146,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
return readFlowsFromDatabase();
}
function processDBData(DbData: FlowType[]) {
function processFlows(DbData: FlowType[], skipUpdate = true) {
let storeComponents: { [key: string]: APIClassType } = {};
DbData.forEach((flow: FlowType) => {
try {
@ -158,7 +158,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
_.cloneDeep((flow.data.nodes[0].data as NodeDataType).node!);
return;
}
processDataFromFlow(flow, false);
if (!skipUpdate) processDataFromFlow(flow, false);
} catch (e) {
console.log(e);
}
@ -200,7 +200,6 @@ export function TabsProvider({ children }: { children: ReactNode }) {
if (skipNodeUpdate.includes(node.data.type)) return;
const template = templates[node.data.type];
if (!template) {
setErrorData({ title: `Unknown node type: ${node.data.type}` });
return;
}
if (Object.keys(template["template"]).length > 0) {
@ -384,6 +383,15 @@ export function TabsProvider({ children }: { children: ReactNode }) {
const index = flows.findIndex((flow) => flow.id === id);
if (index >= 0) {
await deleteFlowFromDatabase(id);
//removes component from data if there is any
if (flows[index].is_component) {
setData((prev) => {
let newData = _.cloneDeep(prev);
const key = flows[index].data!.nodes[0].data.type;
delete newData["custom_components"][key];
return newData;
});
}
setFlows(flows.filter((flow) => flow.id !== id));
}
}
@ -595,9 +603,13 @@ export function TabsProvider({ children }: { children: ReactNode }) {
});
const addFlowToLocalState = (newFlow: FlowType) => {
let newFlows: FlowType[] = [];
setFlows((prevState) => {
newFlows.concat(prevState);
newFlows.push(newFlow);
return [...prevState, newFlow];
});
processFlows(newFlows);
};
/**
@ -656,6 +668,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
}
function saveComponent(component: NodeDataType, id: string) {
component.node!.official = false;
let key = component.type;
if (data["custom_components"][key] !== undefined) {
let { newKey, increment } = IncrementObjectKey(
@ -685,22 +698,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
}
}
component.node!.official = false;
saveFlowToDatabase(createFlowComponent(component))
.then((res) => {
setData((prev) => {
let newData = { ...prev };
//clone to prevent reference erro
newData["custom_components"][key] = _.cloneDeep({
...component.node,
official: false,
});
return newData;
});
})
.catch((err) => {
console.error(err);
});
addFlow(true, createFlowComponent(component));
}
function deleteComponent(id: string, key: string) {
@ -710,13 +708,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
(componentFlow.data?.nodes[0].data as NodeDataType).type === key
);
if (componentFlow) {
removeFlow(componentFlow.id).then((response) => {
setData((prev) => {
let newData = _.cloneDeep(prev);
delete newData["custom_components"][key];
return newData;
});
});
removeFlow(componentFlow.id);
}
}