Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
FullHierarchy search enhancement (#403).
Browse files Browse the repository at this point in the history
  • Loading branch information
Моргунов Андрей Александрович authored and AndrewRAM33 committed Aug 19, 2020
1 parent 87141e5 commit e9582f3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/components/FilterPopup/FilterPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ export const FilterPopup: React.FC<FilterPopupProps> = (props) => {
} else {
dispatch($do.bcAddFilter({ bcName: widget.bcName, filter: newFilter }))
}
dispatch($do.bcForceUpdate({ bcName: widget.bcName }))
// FullHierarchy has its own implementation of data search without backend query filtered data
if (!widget.options.hierarchyFull) {
dispatch($do.bcForceUpdate({ bcName: widget.bcName }))
}
props.onApply?.()
}

const handleCancel = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault()
if (filter) {
dispatch($do.bcRemoveFilter({ bcName: widget.bcName, filter }))
dispatch($do.bcForceUpdate({ bcName: widget.bcName }))
if (!widget.options.hierarchyFull) {
dispatch($do.bcForceUpdate({ bcName: widget.bcName }))
}
}
props.onCancel?.()
}
Expand Down
9 changes: 7 additions & 2 deletions src/components/FullHierarchyTable/FullHierarchyTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ export const FullHierarchyTable: React.FunctionComponent<FullHierarchyTableAllPr
[FilterType.contains, FilterType.equals].includes(filter.type)),
[props.bcFilters]
)
const [filteredData, searchedAncestorsKeys] = useHierarchyCache(props.meta.name, textFilters, props.data, props.depth)
const [filteredData, searchedAncestorsKeys] = useHierarchyCache(
props.meta.name,
textFilters,
props.data,
props.depth,
props.meta.options.hierarchyDisableDescendants)

const data = (props?.nestedData?.length > 0 && depthLevel > 1)
? props.nestedData
Expand All @@ -96,7 +101,7 @@ export const FullHierarchyTable: React.FunctionComponent<FullHierarchyTableAllPr

const [expandedKeys, setExpandedKeys] = useExpandedKeys(
props.expandedRowKeys, selectedRecords, filteredData,
textFilters, searchedAncestorsKeys
textFilters, searchedAncestorsKeys, props.meta.options.hierarchyDisableDescendants
)

const handleExpand = (expanded: boolean, dataItem: DataItem) => {
Expand Down
13 changes: 7 additions & 6 deletions src/components/FullHierarchyTable/utils/useExpandedKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ const emptyArray: string[] = []
* @param data TODO
* @param filters TODO
* @param searchAncestorsKeys TODO
* @param hierarchyDisableDescendants Disable searched item descendants in fullHierarchy search
*/
export function useExpandedKeys(
defaultExpandedKeys: string[],
selectedRecords: FullHierarchyDataItem[],
data: FullHierarchyDataItem[],
filters: BcFilter[],
searchAncestorsKeys: Set<string>
searchAncestorsKeys: Set<string>,
hierarchyDisableDescendants?: boolean
) {
const [expandedKeys, setExpandedKeys] = React.useState<string[]>([])
React.useEffect(() => {
Expand Down Expand Up @@ -76,11 +78,10 @@ export function useExpandedKeys(
})
.map(item => item.id)
: emptyArray
const distinctExpandedKeys = new Set([
...expandedKeys,
...searchResultBranches
])
setExpandedKeys(prev => [ ...prev, ...Array.from(distinctExpandedKeys) ])
const distinctExpandedKeys = new Set([...searchResultBranches])
hierarchyDisableDescendants
? setExpandedKeys(Array.from(distinctExpandedKeys))
: setExpandedKeys(prev => [ ...prev, ...Array.from(distinctExpandedKeys) ])
}, [filters, data, searchAncestorsKeys])
return [expandedKeys, setExpandedKeys] as const
}
12 changes: 10 additions & 2 deletions src/components/FullHierarchyTable/utils/useHierarchyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ const descendantsKeysCache = new HierarchySearchCache()
* @param filters Filters (only text fields supported)
* @param data Records
* @param depthLevel Level of the hierarchy for which hook is called
* @param hierarchyDisableDescendants Disable searched item descendants in fullHierarchy search
*/
export function useHierarchyCache(widgetName: string, filters: BcFilter[], data: FullHierarchyDataItem[], depthLevel: number) {
export function useHierarchyCache(
widgetName: string,
filters: BcFilter[],
data: FullHierarchyDataItem[],
depthLevel: number,
hierarchyDisableDescendants?: boolean) {

React.useEffect(() => {
const clearSearchCache = () => {
if (depthLevel === 1) {
Expand Down Expand Up @@ -61,7 +68,8 @@ export function useHierarchyCache(widgetName: string, filters: BcFilter[], data:

const filteredData = React.useMemo(() => {
return filters?.length
? data.filter(item => searchedAncestorsKeys.has(item.id) || searchedDescendantsKeys.has(item.id))
? data.filter(item => searchedAncestorsKeys.has(item.id)
|| !hierarchyDisableDescendants && searchedDescendantsKeys.has(item.id))
: data
}, [searchedAncestorsKeys, searchedDescendantsKeys, data, filters])
return [filteredData, searchedAncestorsKeys, searchedDescendantsKeys] as const
Expand Down
7 changes: 6 additions & 1 deletion src/epics/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ const bcFetchDataEpic: Epic = (action$, store) => action$.ofType(
return widget.bcName === bcName && widget.type === WidgetTypes.AssocListPopup
&& (widget.options?.hierarchy || widget.options?.hierarchySameBc || widget.options?.hierarchyFull)
})
const fullHierarchyWidget = state.view.widgets.find((widget) => {
return widget.bcName === bcName && widget.type === WidgetTypes.AssocListPopup && widget.options?.hierarchyFull
})

const sameBcHierarchyOptions = anyHierarchyWidget?.options?.hierarchySameBc && anyHierarchyWidget?.options
const depthLevel = sameBcHierarchyOptions && (action.type === types.bcFetchDataRequest && action.payload.depth || 1)

Expand All @@ -122,10 +126,11 @@ const bcFetchDataEpic: Epic = (action$, store) => action$.ofType(
})
}

// Hierarchy widgets has own filter implementation
const fetchParams: Record<string, any> = {
_page: page,
_limit: limit,
...getFilters(filters),
...getFilters(fullHierarchyWidget ? [] : filters),
...getSorters(sorters)
}

Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ export interface WidgetOptions {
hierarchyRadio?: boolean,
hierarchyRadioAll?: boolean,
hierarchyDisableRoot?: boolean,
/**
* Disable searched item descendants in fullHierarchy search
*/
hierarchyDisableDescendants?: boolean,
hierarchyDisableParent?: boolean,
actionGroups?: WidgetOperations,
readOnly?: boolean,
Expand Down

0 comments on commit e9582f3

Please sign in to comment.