Skip to content

Commit

Permalink
fix: log details action not it (#3350)
Browse files Browse the repository at this point in the history
* fix: log details action not it

* fix: getting keys when query param redirect

* fix: getting operator

* fix: data type for not existing autocomplete value

---------

Co-authored-by: Palash Gupta <[email protected]>
  • Loading branch information
yeshev and palashgdev authored Aug 16, 2023
1 parent 8ef4c0b commit 9a6fcb6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
113 changes: 66 additions & 47 deletions frontend/src/hooks/logs/useActiveLog.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { OPERATORS, QueryBuilderKeys } from 'constants/queryBuilder';
import { getAggregateKeys } from 'api/queryBuilder/getAttributeKeys';
import { QueryBuilderKeys } from 'constants/queryBuilder';
import ROUTES from 'constants/routes';
import { getOperatorValue } from 'container/QueryBuilder/filters/QueryBuilderSearch/utils';
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
import { useNotifications } from 'hooks/useNotifications';
import { getGeneratedFilterQueryString } from 'lib/getGeneratedFilterQueryString';
import { chooseAutocompleteFromCustomValue } from 'lib/newQueryBuilder/chooseAutocompleteFromCustomValue';
import { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useQueryClient } from 'react-query';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory, useLocation } from 'react-router-dom';
import { AppState } from 'store/reducers';
import { SET_DETAILED_LOG_DATA } from 'types/actions/logs';
import { SuccessResponse } from 'types/api';
import { ILog } from 'types/api/logs/log';
import {
BaseAutocompleteData,
IQueryAutocompleteResponse,
} from 'types/api/queryBuilder/queryAutocompleteResponse';
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import { Query } from 'types/api/queryBuilder/queryBuilderData';
import { ILogsReducer } from 'types/reducer/logs';
import { v4 as uuid } from 'uuid';
Expand All @@ -31,6 +31,9 @@ export const useActiveLog = (): UseActiveLog => {
const { pathname } = useLocation();
const history = useHistory();
const { currentQuery, redirectWithQueryBuilderData } = useQueryBuilder();
const { notifications } = useNotifications();

const { t } = useTranslation('common');

const isLogsPage = useMemo(() => pathname === ROUTES.LOGS, [pathname]);

Expand Down Expand Up @@ -60,48 +63,64 @@ export const useActiveLog = (): UseActiveLog => {
const onClearActiveLog = useCallback((): void => setActiveLog(null), []);

const onAddToQueryExplorer = useCallback(
(fieldKey: string, fieldValue: string, operator: string): void => {
const keysAutocomplete: BaseAutocompleteData[] =
queryClient.getQueryData<SuccessResponse<IQueryAutocompleteResponse>>(
[QueryBuilderKeys.GET_AGGREGATE_KEYS],
{ exact: false },
)?.payload.attributeKeys || [];

const existAutocompleteKey = chooseAutocompleteFromCustomValue(
keysAutocomplete,
fieldKey,
);

const currentOperator =
Object.keys(OPERATORS).find((op) => op === operator) || '';

const nextQuery: Query = {
...currentQuery,
builder: {
...currentQuery.builder,
queryData: currentQuery.builder.queryData.map((item) => ({
...item,
filters: {
...item.filters,
items: [
...item.filters.items.filter(
(item) => item.key?.id !== existAutocompleteKey.id,
),
{
id: uuid(),
key: existAutocompleteKey,
op: currentOperator,
value: fieldValue,
},
],
},
})),
},
};

redirectWithQueryBuilderData(nextQuery);
async (
fieldKey: string,
fieldValue: string,
operator: string,
): Promise<void> => {
try {
const keysAutocompleteResponse = await queryClient.fetchQuery(
[QueryBuilderKeys.GET_AGGREGATE_KEYS, fieldKey],
async () =>
getAggregateKeys({
searchText: fieldKey,
aggregateOperator: currentQuery.builder.queryData[0].aggregateOperator,
dataSource: currentQuery.builder.queryData[0].dataSource,
aggregateAttribute:
currentQuery.builder.queryData[0].aggregateAttribute.key,
}),
);

const keysAutocomplete: BaseAutocompleteData[] =
keysAutocompleteResponse.payload?.attributeKeys || [];

const existAutocompleteKey = chooseAutocompleteFromCustomValue(
keysAutocomplete,
fieldKey,
);

const currentOperator = getOperatorValue(operator);

const nextQuery: Query = {
...currentQuery,
builder: {
...currentQuery.builder,
queryData: currentQuery.builder.queryData.map((item) => ({
...item,
filters: {
...item.filters,
items: [
...item.filters.items.filter(
(item) => item.key?.id !== existAutocompleteKey.id,
),
{
id: uuid(),
key: existAutocompleteKey,
op: currentOperator,
value: fieldValue,
},
],
},
})),
},
};

redirectWithQueryBuilderData(nextQuery);
} catch {
notifications.error({ message: t('something_went_wrong') });
}
},
[currentQuery, queryClient, redirectWithQueryBuilderData],
[currentQuery, notifications, queryClient, redirectWithQueryBuilderData, t],
);

const onAddToQueryLogs = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export const chooseAutocompleteFromCustomValue = (
(sourceAutoComplete) => value === sourceAutoComplete.key,
);

if (!firstBaseAutoCompleteValue)
return { ...initialAutocompleteData, key: value };
if (!firstBaseAutoCompleteValue) {
return { ...initialAutocompleteData, key: value, dataType: 'string' };
}

return firstBaseAutoCompleteValue;
};

0 comments on commit 9a6fcb6

Please sign in to comment.