|
| 1 | +import { useContext, useState } from 'react'; |
| 2 | + |
| 3 | +import { getComponentFromMap } from '../../../bridge/helpers/sdk_component_map'; |
| 4 | +import DataReferenceAdvancedSearchContext from '../DataReference/DataReferenceAdvancedSearchContext'; |
| 5 | +import { getFirstChildConfig } from '../DataReference/utils'; |
| 6 | + |
| 7 | +export default function AdvancedSearch(props) { |
| 8 | + const { getPConnect, targetObjectClass, localeReference } = props; |
| 9 | + const SearchGroups = getComponentFromMap('SearchGroups'); |
| 10 | + const { dataReferenceConfigToChild, isCreateNewReferenceEnabled, disableStartingFieldsForReference, pyID, searchSelectCacheKey } = useContext( |
| 11 | + DataReferenceAdvancedSearchContext |
| 12 | + ) as any; |
| 13 | + |
| 14 | + const { selectionMode, value: singleSelectFieldValue, readonlyContextList: multiSelectField } = dataReferenceConfigToChild; |
| 15 | + |
| 16 | + let isSelectionExist = false; |
| 17 | + const { MULTI } = PCore.getConstants().LIST_SELECTION_MODE; |
| 18 | + |
| 19 | + if (selectionMode === MULTI) { |
| 20 | + isSelectionExist = getPConnect().getValue(multiSelectField)?.length || false; |
| 21 | + } else { |
| 22 | + isSelectionExist = getPConnect().getValue(singleSelectFieldValue) || false; |
| 23 | + } |
| 24 | + |
| 25 | + const [showRecords, setShowRecords] = useState(isSelectionExist); |
| 26 | + |
| 27 | + const pConn = getPConnect(); |
| 28 | + const rawViewMetadata = pConn.getRawMetadata(); |
| 29 | + |
| 30 | + const searchFieldsSet = new Set(); |
| 31 | + const searchFields: any = []; |
| 32 | + rawViewMetadata.config.searchGroups.forEach(group => { |
| 33 | + group.children.forEach(child => { |
| 34 | + if (!searchFieldsSet.has(child.config.value) && !child.config.validator) { |
| 35 | + searchFields.push(child); |
| 36 | + searchFieldsSet.add(child.config.value); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + const firstChildPConnect = getPConnect().getChildren()[0].getPConnect; |
| 42 | + const [firstChildMeta] = rawViewMetadata.children; |
| 43 | + |
| 44 | + const localizedVal = PCore.getLocaleUtils().getLocaleValue; |
| 45 | + // @ts-ignore |
| 46 | + const cache = PCore.getNavigationUtils().getComponentCache(searchSelectCacheKey) ?? {}; |
| 47 | + |
| 48 | + const editableFieldComp = firstChildPConnect().createComponent({ |
| 49 | + type: firstChildMeta.type, |
| 50 | + config: { |
| 51 | + ...getFirstChildConfig({ |
| 52 | + firstChildMeta, |
| 53 | + getPConnect, |
| 54 | + rawViewMetadata, |
| 55 | + contextClass: targetObjectClass, |
| 56 | + dataReferenceConfigToChild, |
| 57 | + isCreateNewReferenceEnabled, |
| 58 | + disableStartingFieldsForReference, |
| 59 | + pyID |
| 60 | + }), |
| 61 | + searchFields, |
| 62 | + showRecords, |
| 63 | + label: localizedVal('Search results', 'DataReference'), |
| 64 | + searchSelectCacheKey, |
| 65 | + cache |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + const { selectionList, dataRelationshipContext } = editableFieldComp.props.getPConnect().getConfigProps(); |
| 70 | + const editableField = selectionMode === MULTI ? selectionList.substring(1) : dataRelationshipContext; |
| 71 | + |
| 72 | + const searchGroupsProps = { |
| 73 | + getPConnect, |
| 74 | + editableField, |
| 75 | + localeReference, |
| 76 | + setShowRecords, |
| 77 | + searchSelectCacheKey, |
| 78 | + cache |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <SearchGroups {...searchGroupsProps} /> |
| 84 | + {editableFieldComp} |
| 85 | + </> |
| 86 | + ); |
| 87 | +} |
0 commit comments