Skip to content

Commit

Permalink
setAlgoliaConfig: useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
notsidney committed Jun 7, 2021
1 parent 73360f1 commit 8c5096f
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,29 @@ export function useAlgolia<Hit = any>(
};

// Updates Algolia config and creates a new index, then updates state
const setAlgoliaConfig = (
newConfig: Partial<
Pick<SearchState<Hit>, 'appId' | 'searchKey' | 'indexName'>
>
) => {
const updates: Partial<SearchState<Hit>> = {};
// Only pass updated config items that are not undefined
if (newConfig.appId) updates.appId = newConfig.appId;
if (newConfig.searchKey) updates.searchKey = newConfig.searchKey;
if (newConfig.indexName) updates.indexName = newConfig.indexName;

// Generate new index with latest data
updates.index = createAlgoliaIndex(
updates.appId ?? searchState.appId,
updates.searchKey ?? searchState.searchKey,
updates.indexName ?? searchState.indexName
);

searchDispatch(updates);
};
const setAlgoliaConfig = useCallback(
(
newConfig: Partial<
Pick<SearchState<Hit>, 'appId' | 'searchKey' | 'indexName'>
>
) => {
const updates: Partial<SearchState<Hit>> = {};
// Only pass updated config items that are not undefined
if (newConfig.appId) updates.appId = newConfig.appId;
if (newConfig.searchKey) updates.searchKey = newConfig.searchKey;
if (newConfig.indexName) updates.indexName = newConfig.indexName;

// Generate new index with latest data
updates.index = createAlgoliaIndex(
updates.appId ?? searchState.appId,
updates.searchKey ?? searchState.searchKey,
updates.indexName ?? searchState.indexName
);

searchDispatch(updates);
},
[]
);
// Update config when main useAlgolia props update
useEffect(() => {
setAlgoliaConfig({ appId, searchKey, indexName });
Expand Down

0 comments on commit 8c5096f

Please sign in to comment.