Skip to content

Commit

Permalink
[UnifiedFieldList] Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jughosta committed Oct 25, 2022
1 parent 643e567 commit 1acc00d
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/plugins/unified_field_list/public/hooks/use_existing_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ export const useExistingFieldsFetcher = (

setActiveRequests((value) => value + 1);

const hasRestrictions = Boolean(dataView?.getAggregationRestrictions?.());
const hasRestrictions = Boolean(dataView.getAggregationRestrictions?.());
const info: ExistingFieldsInfo = {
...unknownInfo,
numberOfFetches,
};

try {
if (hasRestrictions) {
info.fetchStatus = ExistenceFetchStatus.succeeded;
info.hasDataViewRestrictions = true;
} else {
if (hasRestrictions) {
info.fetchStatus = ExistenceFetchStatus.succeeded;
info.hasDataViewRestrictions = true;
} else {
try {
const result = await loadFieldExisting({
dslQuery: await buildSafeEsQuery(
dataView,
Expand All @@ -135,18 +135,18 @@ export const useExistingFieldsFetcher = (
if (
!existingFieldNames.filter((fieldName) => !metaFields.includes?.(fieldName)).length &&
numberOfFetches === 1 &&
typeof onNoData === 'function'
onNoData
) {
onNoData(dataViewId);
}

info.existingFieldsByFieldNameMap = booleanMap(existingFieldNames);
info.fetchStatus = ExistenceFetchStatus.succeeded;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
info.fetchStatus = ExistenceFetchStatus.failed;
}
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
info.fetchStatus = ExistenceFetchStatus.failed;
}

// skip redundant results
Expand Down Expand Up @@ -201,7 +201,6 @@ export const useExistingFieldsFetcher = (

useEffect(() => {
return () => {
// console.log('resetting the cache');
mountedRef.current = false;
globalMap$.next({}); // reset the cache (readers will continue using their own data slice until they are unmounted too)
};
Expand Down Expand Up @@ -241,12 +240,12 @@ export const useExistingFieldsReader: () => ExistingFieldsReader = () => {
const info = existingFieldsByDataViewMap[dataViewId];

if (info?.fetchStatus === ExistenceFetchStatus.succeeded) {
return info?.hasDataViewRestrictions
? true
: Boolean(info?.existingFieldsByFieldNameMap[fieldName]);
return (
info?.hasDataViewRestrictions || Boolean(info?.existingFieldsByFieldNameMap[fieldName])
);
}

return true; // TODO: double check `true` returns when existence info is unknown
return true;
},
[existingFieldsByDataViewMap]
);
Expand Down

0 comments on commit 1acc00d

Please sign in to comment.