Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useRouter } from 'next/router';

import { InventoryFilterData } from '@components/inventory/hooks/useInventory/types/useInventoryTypes';
Expand All @@ -9,6 +9,10 @@ export type ReactFlowData = {
edges: any[];
};

export type DependencyGraphProps = {
data: ReactFlowData;
};

// converting the json object into data that reactflow needs
// TODO - based on selected library
function GetData(res: any) {
Expand Down Expand Up @@ -66,7 +70,7 @@ function GetData(res: any) {
return d;
}

function useDependencyGraph() {
function useDependencyGraph(resourceId?: string) {
const [loading, setLoading] = useState(true);
const [data, setData] = useState<ReactFlowData>();
const [error, setError] = useState(false);
Expand All @@ -75,26 +79,55 @@ function useDependencyGraph() {
useState<InventoryFilterData[]>();

const router = useRouter();
const fetchRelationsByResourceId = useCallback(
(id: string) => {
settingsService
.getResourceById(`?resourceId=${id}`)
.then(res => {
if (res === Error) {
setLoading(false);
setError(true);
} else {
setLoading(false);
setData(GetData([].concat(res)));
}
})
.finally(() => {
setLoading(false);
});
},
[resourceId]
);

const fetchAllRelations = useCallback(() => {
settingsService
.getRelations(filters)
.then(res => {
if (res === Error) {
setLoading(false);
setError(true);
} else {
setLoading(false);
setData(GetData([].concat(res)));
}
})
.finally(() => {
setLoading(false);
});
}, [filters]);

function fetch() {
const fetch = useCallback(() => {
if (!loading) {
setLoading(true);
}

if (error) {
setError(false);
}

settingsService.getRelations(filters).then(res => {
if (res === Error) {
setLoading(false);
setError(true);
} else {
setLoading(false);
setData(GetData(res));
}
});
}
if (resourceId) {
fetchRelationsByResourceId(resourceId);
} else fetchAllRelations();
}, [filters, resourceId]);

function deleteFilter(idx: number) {
const updatedFilters: InventoryFilterData[] = [...filters!];
Expand Down
Loading