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:
parent
433890504e
commit
9ec4784afb
1 changed files with 19 additions and 27 deletions
|
|
@ -125,7 +125,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
getTabsDataFromDB().then((DbData) => {
|
getTabsDataFromDB().then((DbData) => {
|
||||||
if (DbData && Object.keys(templates).length > 0) {
|
if (DbData && Object.keys(templates).length > 0) {
|
||||||
try {
|
try {
|
||||||
processDBData(DbData);
|
processFlows(DbData, false);
|
||||||
updateStateWithDbData(DbData);
|
updateStateWithDbData(DbData);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
@ -146,7 +146,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
return readFlowsFromDatabase();
|
return readFlowsFromDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
function processDBData(DbData: FlowType[]) {
|
function processFlows(DbData: FlowType[], skipUpdate = true) {
|
||||||
let storeComponents: { [key: string]: APIClassType } = {};
|
let storeComponents: { [key: string]: APIClassType } = {};
|
||||||
DbData.forEach((flow: FlowType) => {
|
DbData.forEach((flow: FlowType) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -158,7 +158,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
_.cloneDeep((flow.data.nodes[0].data as NodeDataType).node!);
|
_.cloneDeep((flow.data.nodes[0].data as NodeDataType).node!);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
processDataFromFlow(flow, false);
|
if (!skipUpdate) processDataFromFlow(flow, false);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
@ -200,7 +200,6 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
if (skipNodeUpdate.includes(node.data.type)) return;
|
if (skipNodeUpdate.includes(node.data.type)) return;
|
||||||
const template = templates[node.data.type];
|
const template = templates[node.data.type];
|
||||||
if (!template) {
|
if (!template) {
|
||||||
setErrorData({ title: `Unknown node type: ${node.data.type}` });
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Object.keys(template["template"]).length > 0) {
|
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);
|
const index = flows.findIndex((flow) => flow.id === id);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
await deleteFlowFromDatabase(id);
|
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));
|
setFlows(flows.filter((flow) => flow.id !== id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -595,9 +603,13 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
});
|
});
|
||||||
|
|
||||||
const addFlowToLocalState = (newFlow: FlowType) => {
|
const addFlowToLocalState = (newFlow: FlowType) => {
|
||||||
|
let newFlows: FlowType[] = [];
|
||||||
setFlows((prevState) => {
|
setFlows((prevState) => {
|
||||||
|
newFlows.concat(prevState);
|
||||||
|
newFlows.push(newFlow);
|
||||||
return [...prevState, newFlow];
|
return [...prevState, newFlow];
|
||||||
});
|
});
|
||||||
|
processFlows(newFlows);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -656,6 +668,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveComponent(component: NodeDataType, id: string) {
|
function saveComponent(component: NodeDataType, id: string) {
|
||||||
|
component.node!.official = false;
|
||||||
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(
|
||||||
|
|
@ -685,22 +698,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
component.node!.official = false;
|
component.node!.official = false;
|
||||||
|
addFlow(true, createFlowComponent(component));
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteComponent(id: string, key: string) {
|
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
|
(componentFlow.data?.nodes[0].data as NodeDataType).type === key
|
||||||
);
|
);
|
||||||
if (componentFlow) {
|
if (componentFlow) {
|
||||||
removeFlow(componentFlow.id).then((response) => {
|
removeFlow(componentFlow.id);
|
||||||
setData((prev) => {
|
|
||||||
let newData = _.cloneDeep(prev);
|
|
||||||
delete newData["custom_components"][key];
|
|
||||||
return newData;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue