Skip to content

Commit

Permalink
feat: fixed dropdown open triggering the api calls for single-select …
Browse files Browse the repository at this point in the history
…and misc
  • Loading branch information
SagarRajput-7 committed Dec 11, 2024
1 parent eb75e63 commit 421d355
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IDashboardVariable } from 'types/api/dashboard/getAll';
import {
buildDependencies,
buildDependencyGraph,
buildParentDependencyGraph,
onUpdateVariableNode,
VariableGraph,
} from './util';
Expand All @@ -29,6 +30,7 @@ function DashboardVariableSelection(): JSX.Element | null {
const [dependencyData, setDependencyData] = useState<{
order: string[];
graph: VariableGraph;
parentDependencyGraph: VariableGraph;
} | null>(null);

useEffect(() => {
Expand Down Expand Up @@ -59,9 +61,11 @@ function DashboardVariableSelection(): JSX.Element | null {
if (variablesTableData.length > 0 && !initializationRef.current) {
const depGrp = buildDependencies(variablesTableData);
const { order, graph } = buildDependencyGraph(depGrp);
const parentDependencyGraph = buildParentDependencyGraph(graph);
setDependencyData({
order,
graph,
parentDependencyGraph,
});
initializationRef.current = true;
}
Expand Down Expand Up @@ -120,7 +124,7 @@ function DashboardVariableSelection(): JSX.Element | null {
dependencyData.order,
(node) => updatedVariables.push(node),
);
setVariablesToGetUpdated(updatedVariables.filter((v) => v !== name)); // question?
setVariablesToGetUpdated(updatedVariables.filter((v) => v !== name));
} else if (isMountedCall) {
setVariablesToGetUpdated((prev) => prev.filter((v) => v !== name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import { popupContainer } from 'utils/selectPopupContainer';

import { variablePropsToPayloadVariables } from '../utils';
import { SelectItemStyle } from './styles';
import { areArraysEqual, checkAPIInvocation, VariableGraph } from './util';
import {
areArraysEqual,
checkAPIInvocation,
onUpdateVariableNode,
VariableGraph,
} from './util';

const ALL_SELECT_VALUE = '__ALL__';

Expand All @@ -59,6 +64,7 @@ interface VariableItemProps {
dependencyData: {
order: string[];
graph: VariableGraph;
parentDependencyGraph: VariableGraph;
} | null;
}

Expand Down Expand Up @@ -212,11 +218,14 @@ function VariableItem({
`${maxTime}`,
],
{
// enabled: variableData && variableData.type === 'QUERY',
enabled:
variableData &&
variableData.type === 'QUERY' &&
checkAPIInvocation(variablesToGetUpdated, variableData, dependencyData),
checkAPIInvocation(
variablesToGetUpdated,
variableData,
dependencyData?.parentDependencyGraph,
),
queryFn: () =>
dashboardVariablesQuery({
query: variableData.queryValue || '',
Expand All @@ -225,6 +234,21 @@ function VariableItem({
refetchOnWindowFocus: false,
onSuccess: (response) => {
getOptions(response.payload);
if (
dependencyData?.parentDependencyGraph[variableData.name || ''].length === 0
) {
const updatedVariables: string[] = [];
onUpdateVariableNode(
variableData.name || '',
dependencyData.graph,
dependencyData.order,
(node) => updatedVariables.push(node),
);
setVariablesToGetUpdated((prev) => [
...prev,
...updatedVariables.filter((v) => v !== variableData.name),
]);
}
},
onError: (error: {
details: {
Expand All @@ -246,6 +270,15 @@ function VariableItem({
);

const handleChange = (value: string | string[]): void => {
// if value is equal to selected value then return
if (
value === variableData.selectedValue ||
(Array.isArray(value) &&
Array.isArray(variableData.selectedValue) &&
areArraysEqual(value, variableData.selectedValue))
) {
return;
}
if (variableData.name) {
if (
value === ALL_SELECT_VALUE ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEmpty } from 'lodash-es';
import { Dashboard, IDashboardVariable } from 'types/api/dashboard/getAll';

export function areArraysEqual(
Expand Down Expand Up @@ -161,32 +162,26 @@ export const buildParentDependencyGraph = (
export const checkAPIInvocation = (
variablesToGetUpdated: string[],
variableData: IDashboardVariable,
dependencyData: {
order: string[];
graph: VariableGraph;
} | null,
parentDependencyGraph?: VariableGraph,
): boolean => {
console.log(variableData.name, dependencyData, variablesToGetUpdated);
if (!dependencyData) {
return true;
}

if (!variableData.name) {
if (isEmpty(variableData.name)) {
return false;
}

const parentDependencies = buildParentDependencyGraph(dependencyData.graph);
if (isEmpty(parentDependencyGraph)) {
return true;
}

// if no dependency then true
const haveDependency = parentDependencies[variableData.name]?.length > 0;
const haveDependency =
parentDependencyGraph?.[variableData.name || '']?.length > 0;
if (!haveDependency) {
return true;
}

// if variable is in the list and has dependency then check if its the top element in the queue then true else false
return (
variablesToGetUpdated &&
variablesToGetUpdated.includes(variableData.name) &&
variablesToGetUpdated.length > 0 &&
variablesToGetUpdated[0] === variableData.name
);
};

0 comments on commit 421d355

Please sign in to comment.