Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
refactor(indexUtils): use multiple functions for cleanUp
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss committed May 24, 2018
1 parent 0e864da commit 8061b03
Showing 1 changed file with 57 additions and 24 deletions.
81 changes: 57 additions & 24 deletions packages/react-instantsearch/src/core/indexUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,64 @@ export function getCurrentRefinementValue(
}

export function cleanUpValue(searchState, context, id) {
const index = getIndex(context);
const indexName = getIndex(context);
const { namespace, attributeName } = getNamespaceAndAttributeName(id);

if (hasMultipleIndex(context) && Boolean(searchState.indices)) {
const searchStateIndex = searchState.indices[index];
return namespace && searchStateIndex
? {
...searchState,
indices: {
...searchState.indices,
[index]: {
...searchStateIndex,
[namespace]: omit(
searchStateIndex[namespace],
`${attributeName}`
),
},
},
}
: omit(searchState, `indices.${index}.${id}`);
} else {
return namespace
? {
...searchState,
[namespace]: omit(searchState[namespace], `${attributeName}`),
}
: omit(searchState, `${id}`);
return cleanUpValueWithMutliIndex({
attribute: attributeName,
searchState,
indexName,
id,
namespace,
});
}

return cleanUpValueWithSingleIndex({
attribute: attributeName,
searchState,
id,
namespace,
});
}

function cleanUpValueWithSingleIndex({
searchState,
id,
namespace,
attribute,
}) {
if (namespace) {
return {
...searchState,
[namespace]: omit(searchState[namespace], attribute),
};
}

return omit(searchState, id);
}

function cleanUpValueWithMutliIndex({
searchState,
indexName,
id,
namespace,
attribute,
}) {
const index = searchState.indices[indexName];

if (namespace && index) {
return {
...searchState,
indices: {
...searchState.indices,
[indexName]: {
...index,
[namespace]: omit(index[namespace], attribute),
},
},
};
}

return omit(searchState, `indices.${indexName}.${id}`);
}

0 comments on commit 8061b03

Please sign in to comment.