Refactor buildVertices function to improve readability and error handling
This commit is contained in:
parent
e6b8cf551b
commit
411060af93
1 changed files with 34 additions and 31 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
import { getVerticesOrder, postBuildVertex } from "../controllers/API";
|
import { getVerticesOrder, postBuildVertex } from "../controllers/API";
|
||||||
import { FlowType } from "../types/flow";
|
|
||||||
import { VertexBuildTypeAPI } from "../types/api";
|
import { VertexBuildTypeAPI } from "../types/api";
|
||||||
|
import { FlowType } from "../types/flow";
|
||||||
|
|
||||||
type BuildVerticesParams = {
|
type BuildVerticesParams = {
|
||||||
flow: FlowType; // Assuming FlowType is the type for your flow
|
flow: FlowType; // Assuming FlowType is the type for your flow
|
||||||
|
|
@ -22,52 +22,55 @@ export async function buildVertices({
|
||||||
}: BuildVerticesParams) {
|
}: BuildVerticesParams) {
|
||||||
let orderResponse = await getVerticesOrder(flow.id);
|
let orderResponse = await getVerticesOrder(flow.id);
|
||||||
let verticesOrder: Array<Array<string>> = orderResponse.data.ids;
|
let verticesOrder: Array<Array<string>> = orderResponse.data.ids;
|
||||||
let vertices : Array<Array<string>> = [];
|
let vertices: Array<Array<string>> = [];
|
||||||
if (nodeId) {
|
if (nodeId) {
|
||||||
for (let i = 0; i < verticesOrder.length; i += 1) {
|
for (let i = 0; i < verticesOrder.length; i += 1) {
|
||||||
const innerArray = verticesOrder[i];
|
const innerArray = verticesOrder[i];
|
||||||
const idIndex = innerArray.indexOf(nodeId);
|
const idIndex = innerArray.indexOf(nodeId);
|
||||||
|
|
||||||
if (idIndex !== -1) {
|
if (idIndex !== -1) {
|
||||||
// If the targetId is found in the inner array, cut the array before the id
|
// If the targetId is found in the inner array, cut the array before the id
|
||||||
vertices.push(innerArray.slice(0, idIndex + 1));
|
vertices.push(innerArray.slice(0, idIndex + 1));
|
||||||
break; // Stop searching after finding the first occurrence
|
break; // Stop searching after finding the first occurrence
|
||||||
}
|
}
|
||||||
// If the targetId is not found, include the entire inner array
|
// If the targetId is not found, include the entire inner array
|
||||||
vertices.push(innerArray);
|
vertices.push(innerArray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildResults: Array<boolean> = [];
|
const buildResults: Array<boolean> = [];
|
||||||
for (let i = 0; i < vertices.length; i += 1) {
|
for (let i = 0; i < vertices.length; i += 1) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
vertices[i].map(async (id) => {
|
vertices[i].map(async (id) => {
|
||||||
try {
|
try {
|
||||||
const buildRes = await postBuildVertex(flow, id);
|
const buildRes = await postBuildVertex(flow, id);
|
||||||
const buildData: VertexBuildTypeAPI = buildRes.data;
|
const buildData: VertexBuildTypeAPI = buildRes.data;
|
||||||
if (onBuildUpdate) {
|
if (onBuildUpdate) {
|
||||||
let data = {};
|
let data = {};
|
||||||
if (!buildData.valid) {
|
if (!buildData.valid) {
|
||||||
if (onBuildError) {
|
if (onBuildError) {
|
||||||
onBuildError("Error Building Component", [buildData.params])
|
onBuildError("Error Building Component", [buildData.params]);
|
||||||
}
|
}
|
||||||
data[buildData.id] = buildData;
|
data[buildData.id] = buildData;
|
||||||
|
|
||||||
onBuildUpdate({ data, id: buildData.id });
|
onBuildUpdate({ data, id: buildData.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buildResults.push(buildData.valid);
|
||||||
|
if (onBuildComplete) {
|
||||||
|
const allNodesValid = buildResults.every((result) => result);
|
||||||
|
onBuildComplete(allNodesValid);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (onBuildError) {
|
||||||
|
console.log(error);
|
||||||
|
onBuildError("Error Building Component", [
|
||||||
|
(error as AxiosError<any>).response?.data?.detail ??
|
||||||
|
"Unknown Error",
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buildResults.push(buildData.valid);
|
})
|
||||||
if (onBuildComplete) {
|
);
|
||||||
const allNodesValid = buildResults.every((result) => result);
|
|
||||||
onBuildComplete(allNodesValid);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (onBuildError) {
|
|
||||||
console.log(error)
|
|
||||||
onBuildError("Error Building Component", [(error as AxiosError<any>).response?.data?.detail??"Unknown Error"]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue