Skip to content

Commit

Permalink
fix: added time range key for query and local storage handling (#6009)
Browse files Browse the repository at this point in the history
* fix: added time range key for query and local storage handling

* chore: fix jest test cases

* fix: send single element array for only variable option as well

* fix: intermediate stale data should not be shown
  • Loading branch information
vikrantgupta25 authored and srikanthccv committed Sep 19, 2024
1 parent 521b7c6 commit 5d6ff47
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
21 changes: 21 additions & 0 deletions frontend/src/container/GridCardLayout/GridCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isEqual } from 'lodash-es';
import isEmpty from 'lodash-es/isEmpty';
import { useDashboard } from 'providers/Dashboard/Dashboard';
import { memo, useEffect, useRef, useState } from 'react';
import { useQueryClient } from 'react-query';
import { useDispatch, useSelector } from 'react-redux';
import { UpdateTimeInterval } from 'store/actions';
import { AppState } from 'store/reducers';
Expand Down Expand Up @@ -45,6 +46,7 @@ function GridCardGraph({
AppState,
GlobalReducer
>((state) => state.globalTime);
const queryClient = useQueryClient();

const handleBackNavigation = (): void => {
const searchParams = new URLSearchParams(window.location.search);
Expand Down Expand Up @@ -134,6 +136,25 @@ function GridCardGraph({
};
});

// TODO [vikrantgupta25] remove this useEffect with refactor as this is prone to race condition
// this is added to tackle the case of async communication between VariableItem.tsx and GridCard.tsx
useEffect(() => {
if (variablesToGetUpdated.length > 0) {
queryClient.cancelQueries([
maxTime,
minTime,
globalSelectedInterval,
variables,
widget?.query,
widget?.panelTypes,
widget.timePreferance,
widget.fillSpans,
requestData,
]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [variablesToGetUpdated]);

useEffect(() => {
if (!isEqual(updatedQuery, requestData.query)) {
setRequestData((prev) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import '@testing-library/jest-dom/extend-expect';

import {
act,
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react';
import MockQueryClientProvider from 'providers/test/MockQueryClientProvider';
import React, { useEffect } from 'react';
import { act, fireEvent, render, screen, waitFor } from 'tests/test-utils';
import { IDashboardVariable } from 'types/api/dashboard/getAll';

import VariableItem from './VariableItem';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable sonarjs/cognitive-complexity */
/* eslint-disable jsx-a11y/click-events-have-key-events */
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -25,8 +26,11 @@ import { debounce, isArray, isString } from 'lodash-es';
import map from 'lodash-es/map';
import { ChangeEvent, memo, useEffect, useMemo, useState } from 'react';
import { useQuery } from 'react-query';
import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers';
import { IDashboardVariable } from 'types/api/dashboard/getAll';
import { VariableResponseProps } from 'types/api/dashboard/variables/query';
import { GlobalReducer } from 'types/reducer/globalTime';
import { popupContainer } from 'utils/selectPopupContainer';

import { variablePropsToPayloadVariables } from '../utils';
Expand Down Expand Up @@ -80,6 +84,23 @@ function VariableItem({
[],
);

const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(
(state) => state.globalTime,
);

useEffect(() => {
if (variableData.allSelected && variableData.type === 'QUERY') {
setVariablesToGetUpdated((prev) => {
const variablesQueue = [...prev.filter((v) => v !== variableData.name)];
if (variableData.name) {
variablesQueue.push(variableData.name);
}
return variablesQueue;
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [minTime, maxTime]);

const [errorMessage, setErrorMessage] = useState<null | string>(null);

const getDependentVariables = (queryValue: string): string[] => {
Expand Down Expand Up @@ -111,7 +132,14 @@ function VariableItem({

const variableKey = dependentVariablesStr.replace(/\s/g, '');

return [REACT_QUERY_KEY.DASHBOARD_BY_ID, variableName, variableKey];
// added this time dependency for variables query as API respects the passed time range now
return [
REACT_QUERY_KEY.DASHBOARD_BY_ID,
variableName,
variableKey,
`${minTime}`,
`${maxTime}`,
];
};

// eslint-disable-next-line sonarjs/cognitive-complexity
Expand Down Expand Up @@ -151,10 +179,14 @@ function VariableItem({
valueNotInList = true;
}
}
// variablesData.allSelected is added for the case where on change of options we need to update the
// local storage
if (
variableData.type === 'QUERY' &&
variableData.name &&
(variablesToGetUpdated.includes(variableData.name) || valueNotInList)
(variablesToGetUpdated.includes(variableData.name) ||
valueNotInList ||
variableData.allSelected)
) {
let value = variableData.selectedValue;
let allSelected = false;
Expand Down Expand Up @@ -338,8 +370,8 @@ function VariableItem({
(Array.isArray(selectValue) && selectValue?.includes(option.toString()));

if (isChecked) {
if (mode === ToggleTagValue.Only) {
handleChange(option.toString());
if (mode === ToggleTagValue.Only && variableData.multiSelect) {
handleChange([option.toString()]);
} else if (!variableData.multiSelect) {
handleChange(option.toString());
} else {
Expand Down

0 comments on commit 5d6ff47

Please sign in to comment.