-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add query builder widget * locales * add unit tests * add debounce for on change
- Loading branch information
Showing
17 changed files
with
318 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...mmon/Form/JSONSchema/JsonSchemaWidgets/QueryBuilderWidget/QueryBuilderWidget.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright 2024 Collate. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export enum QueryBuilderOutputType { | ||
ELASTICSEARCH = 'elasticsearch', | ||
JSON_LOGIC = 'jsonlogic', | ||
} |
75 changes: 75 additions & 0 deletions
75
...s/common/Form/JSONSchema/JsonSchemaWidgets/QueryBuilderWidget/QueryBuilderWidget.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2024 Collate. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Registry } from '@rjsf/utils'; | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { BasicConfig } from 'react-awesome-query-builder'; | ||
import AntdConfig from 'react-awesome-query-builder/lib/config/antd'; | ||
import QueryBuilderWidget from './QueryBuilderWidget'; | ||
|
||
const mockOnFocus = jest.fn(); | ||
const mockOnBlur = jest.fn(); | ||
const mockOnChange = jest.fn(); | ||
const baseConfig = AntdConfig as BasicConfig; | ||
|
||
jest.mock( | ||
'../../../../../Explore/AdvanceSearchProvider/AdvanceSearchProvider.component', | ||
() => ({ | ||
AdvanceSearchProvider: ({ children }: { children: React.ReactNode }) => ( | ||
<div data-testid="advance-search-provider-mock">{children}</div> | ||
), | ||
useAdvanceSearch: jest.fn().mockImplementation(() => ({ | ||
toggleModal: jest.fn(), | ||
sqlQuery: '', | ||
onResetAllFilters: jest.fn(), | ||
onChangeSearchIndex: jest.fn(), | ||
config: { | ||
...baseConfig, | ||
fields: {}, | ||
}, | ||
})), | ||
}) | ||
); | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useLocation: jest.fn(), | ||
})); | ||
|
||
const mockProps = { | ||
onFocus: mockOnFocus, | ||
onBlur: mockOnBlur, | ||
onChange: mockOnChange, | ||
registry: {} as Registry, | ||
schema: { | ||
description: 'this is query builder field', | ||
title: 'rules', | ||
format: 'queryBuilder', | ||
entityType: 'table', | ||
}, | ||
value: '', | ||
id: 'root/queryBuilder', | ||
label: 'Query Builder', | ||
name: 'queryBuilder', | ||
options: { | ||
enumOptions: [], | ||
}, | ||
}; | ||
|
||
describe('QueryBuilderWidget', () => { | ||
it('should render the query builder', () => { | ||
render(<QueryBuilderWidget {...mockProps} />); | ||
const builder = screen.getByTestId('query-builder-form-field'); | ||
|
||
expect(builder).toBeInTheDocument(); | ||
}); | ||
}); |
159 changes: 159 additions & 0 deletions
159
...onents/common/Form/JSONSchema/JsonSchemaWidgets/QueryBuilderWidget/QueryBuilderWidget.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2024 Collate. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { InfoCircleOutlined } from '@ant-design/icons'; | ||
import { WidgetProps } from '@rjsf/utils'; | ||
import { Alert, Button, Col, Typography } from 'antd'; | ||
import { t } from 'i18next'; | ||
import { debounce, isEmpty, isUndefined } from 'lodash'; | ||
import Qs from 'qs'; | ||
import React, { FC, useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { | ||
Builder, | ||
Config, | ||
ImmutableTree, | ||
Query, | ||
Utils as QbUtils, | ||
} from 'react-awesome-query-builder'; | ||
import { getExplorePath } from '../../../../../../constants/constants'; | ||
import { EntityType } from '../../../../../../enums/entity.enum'; | ||
import { SearchIndex } from '../../../../../../enums/search.enum'; | ||
import { searchQuery } from '../../../../../../rest/searchAPI'; | ||
import searchClassBase from '../../../../../../utils/SearchClassBase'; | ||
import { withAdvanceSearch } from '../../../../../AppRouter/withAdvanceSearch'; | ||
import { useAdvanceSearch } from '../../../../../Explore/AdvanceSearchProvider/AdvanceSearchProvider.component'; | ||
import './query-builder-widget.less'; | ||
import { QueryBuilderOutputType } from './QueryBuilderWidget.interface'; | ||
|
||
const QueryBuilderWidget: FC<WidgetProps> = ({ | ||
onChange, | ||
schema, | ||
value, | ||
...props | ||
}: WidgetProps) => { | ||
const { config, treeInternal, onTreeUpdate, onChangeSearchIndex } = | ||
useAdvanceSearch(); | ||
const [searchResults, setSearchResults] = useState<number>(0); | ||
const entityType = | ||
(props.formContext?.entityType ?? props?.entityType) || EntityType.ALL; | ||
const searchIndexMapping = searchClassBase.getEntityTypeSearchIndexMapping(); | ||
const searchIndex = searchIndexMapping[entityType as string]; | ||
const outputType = props?.outputType ?? QueryBuilderOutputType.ELASTICSEARCH; | ||
|
||
const fetchEntityCount = useCallback( | ||
async (queryFilter: Record<string, unknown>) => { | ||
try { | ||
const res = await searchQuery({ | ||
query: '', | ||
pageNumber: 0, | ||
pageSize: 0, | ||
queryFilter, | ||
searchIndex: SearchIndex.ALL, | ||
includeDeleted: false, | ||
trackTotalHits: true, | ||
fetchSource: false, | ||
}); | ||
setSearchResults(res.hits.total.value ?? 0); | ||
} catch (_) { | ||
// silent fail | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const debouncedFetchEntityCount = useMemo( | ||
() => debounce(fetchEntityCount, 300), | ||
[fetchEntityCount] | ||
); | ||
|
||
const queryURL = useMemo(() => { | ||
const queryFilterString = !isEmpty(treeInternal) | ||
? Qs.stringify({ queryFilter: JSON.stringify(treeInternal) }) | ||
: ''; | ||
|
||
return `${getExplorePath({})}${queryFilterString}`; | ||
}, [treeInternal]); | ||
|
||
const handleChange = (nTree: ImmutableTree, nConfig: Config) => { | ||
onTreeUpdate(nTree, nConfig); | ||
|
||
if (outputType === QueryBuilderOutputType.ELASTICSEARCH) { | ||
const data = QbUtils.elasticSearchFormat(nTree, config) ?? {}; | ||
const qFilter = { | ||
query: data, | ||
}; | ||
if (data) { | ||
debouncedFetchEntityCount(qFilter); | ||
} | ||
|
||
onChange(JSON.stringify(qFilter)); | ||
} else { | ||
const data = QbUtils.jsonLogicFormat(nTree, config); | ||
onChange(JSON.stringify(data.logic ?? '{}')); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
onChangeSearchIndex(searchIndex); | ||
}, [searchIndex]); | ||
|
||
return ( | ||
<div | ||
className="query-builder-form-field" | ||
data-testid="query-builder-form-field"> | ||
<Query | ||
{...config} | ||
renderBuilder={(props) => ( | ||
<div className="query-builder-container query-builder qb-lite"> | ||
<Builder {...props} /> | ||
</div> | ||
)} | ||
value={treeInternal} | ||
onChange={handleChange} | ||
/> | ||
{outputType === QueryBuilderOutputType.ELASTICSEARCH && | ||
!isUndefined(value) && ( | ||
<Col span={24}> | ||
<Button | ||
className="w-full p-0 text-left" | ||
data-testid="view-assets-banner-button" | ||
disabled={false} | ||
href={queryURL} | ||
target="_blank" | ||
type="link"> | ||
<Alert | ||
closable | ||
showIcon | ||
icon={<InfoCircleOutlined height={16} />} | ||
message={ | ||
<> | ||
<Typography.Text> | ||
{t('message.search-entity-count', { | ||
count: searchResults, | ||
})} | ||
</Typography.Text> | ||
|
||
<Typography.Text className="m-l-sm text-xs text-grey-muted"> | ||
{t('message.click-here-to-view-assets-on-explore')} | ||
</Typography.Text> | ||
</> | ||
} | ||
type="info" | ||
/> | ||
</Button> | ||
</Col> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default withAdvanceSearch(QueryBuilderWidget); |
42 changes: 42 additions & 0 deletions
42
...nts/common/Form/JSONSchema/JsonSchemaWidgets/QueryBuilderWidget/query-builder-widget.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2024 Collate. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
.query-builder-form-field { | ||
.hide--line.one--child { | ||
margin-top: 0; | ||
padding-top: 16px; | ||
} | ||
|
||
.group.rule_group { | ||
border: none !important; | ||
padding: 0; | ||
|
||
.group--children { | ||
padding-top: 0; | ||
padding-bottom: 0; | ||
margin: 0; | ||
} | ||
} | ||
|
||
.group--field { | ||
width: 180px; | ||
|
||
.ant-select { | ||
width: 100% !important; | ||
} | ||
|
||
label { | ||
font-weight: normal; | ||
margin-bottom: 6px; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.