2022-08-19 17:16:04 +05:30
|
|
|
import { Button, Popover } from 'antd';
|
2022-08-11 11:45:28 +05:30
|
|
|
import { generateFilterQuery } from 'lib/logs/generateFilterQuery';
|
|
|
|
|
import React, { memo, useCallback, useMemo } from 'react';
|
2022-12-26 16:20:34 +05:30
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2022-08-11 11:45:28 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
2022-12-26 16:20:34 +05:30
|
|
|
import { SET_SEARCH_QUERY_STRING } from 'types/actions/logs';
|
2022-08-19 17:16:04 +05:30
|
|
|
import { ILogsReducer } from 'types/reducer/logs';
|
2022-08-11 11:45:28 +05:30
|
|
|
|
|
|
|
|
function AddToQueryHOC({
|
|
|
|
|
fieldKey,
|
|
|
|
|
fieldValue,
|
|
|
|
|
children,
|
2022-08-19 17:16:04 +05:30
|
|
|
}: AddToQueryHOCProps): JSX.Element {
|
2022-08-11 11:45:28 +05:30
|
|
|
const {
|
|
|
|
|
searchFilter: { queryString },
|
|
|
|
|
} = useSelector<AppState, ILogsReducer>((store) => store.logs);
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const generatedQuery = useMemo(
|
|
|
|
|
() => generateFilterQuery({ fieldKey, fieldValue, type: 'IN' }),
|
|
|
|
|
[fieldKey, fieldValue],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleQueryAdd = useCallback(() => {
|
|
|
|
|
let updatedQueryString = queryString || '';
|
|
|
|
|
|
|
|
|
|
if (updatedQueryString.length === 0) {
|
|
|
|
|
updatedQueryString += `${generatedQuery}`;
|
|
|
|
|
} else {
|
|
|
|
|
updatedQueryString += ` AND ${generatedQuery}`;
|
|
|
|
|
}
|
|
|
|
|
dispatch({
|
|
|
|
|
type: SET_SEARCH_QUERY_STRING,
|
|
|
|
|
payload: updatedQueryString,
|
|
|
|
|
});
|
2022-12-26 16:20:34 +05:30
|
|
|
}, [dispatch, generatedQuery, queryString]);
|
|
|
|
|
|
|
|
|
|
const popOverContent = useMemo(() => <span>Add to query: {fieldKey}</span>, [
|
|
|
|
|
fieldKey,
|
2022-08-11 11:45:28 +05:30
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return (
|
2022-12-26 16:20:34 +05:30
|
|
|
<Button size="small" type="text" onClick={handleQueryAdd}>
|
2022-08-11 11:45:28 +05:30
|
|
|
<Popover placement="top" content={popOverContent}>
|
|
|
|
|
{children}
|
|
|
|
|
</Popover>
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 16:20:34 +05:30
|
|
|
interface AddToQueryHOCProps {
|
|
|
|
|
fieldKey: string;
|
|
|
|
|
fieldValue: string;
|
|
|
|
|
children: React.ReactNode;
|
2022-08-11 11:45:28 +05:30
|
|
|
}
|
|
|
|
|
|
2022-12-26 16:20:34 +05:30
|
|
|
export default memo(AddToQueryHOC);
|