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

Commit

Permalink
fix(indexUtils): allow index with dots in it (#2350)
Browse files Browse the repository at this point in the history
* fix(indexUtils): allow index with dots in it

How it's done:
- remove usage of lodash.get & lodash.has
- that's it

In the mean time, I did a small refactor, removing the callback to getCurrentRefinementValue, because it's useless.

TODO: add new tests specifically for this use case

* refactor: implement feedback

* refactor(indexUtils): make context less pervasive

* chore(indexUtils): searchState can't be undefined

* chore(refinementList): make connector logic smaller

* chore(indexUtils): remove needless checks

* chore: repeat args
  • Loading branch information
Haroenv authored May 16, 2019
1 parent ea0a2ef commit f91906f
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ const getId = () => 'query';

function getCurrentRefinement(props, searchState, context) {
const id = getId();
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
id,
'',
currentRefinement => {
if (currentRefinement) {
return currentRefinement;
}
return '';
}
''
);

if (currentRefinement) {
return currentRefinement;
}
return '';
}

function getHits(searchResults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ export const getId = props => props.attributes[0];
const namespace = 'hierarchicalMenu';

function getCurrentRefinement(props, searchState, context) {
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
`${namespace}.${getId(props)}`,
null,
currentRefinement => {
if (currentRefinement === '') {
return null;
}
return currentRefinement;
}
null
);

if (currentRefinement === '') {
return null;
}
return currentRefinement;
}

function getValue(path, props, searchState, context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ function getId() {

function getCurrentRefinement(props, searchState, context) {
const id = getId();
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
id,
null,
currentRefinement => {
if (typeof currentRefinement === 'string') {
return parseInt(currentRefinement, 10);
}
return currentRefinement;
}
null
);

if (typeof currentRefinement === 'string') {
return parseInt(currentRefinement, 10);
}
return currentRefinement;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ function getId() {
function getCurrentRefinement(props, searchState, context) {
const id = getId();
const page = 1;
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
id,
page,
currentRefinement => {
if (typeof currentRefinement === 'string') {
currentRefinement = parseInt(currentRefinement, 10);
}
return currentRefinement;
}
page
);

if (typeof currentRefinement === 'string') {
return parseInt(currentRefinement, 10);
}
return currentRefinement;
}

/**
Expand Down
15 changes: 7 additions & 8 deletions packages/react-instantsearch-core/src/connectors/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ function getId(props) {
}

function getCurrentRefinement(props, searchState, context) {
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
`${namespace}.${getId(props)}`,
null,
currentRefinement => {
if (currentRefinement === '') {
return null;
}
return currentRefinement;
}
null
);

if (currentRefinement === '') {
return null;
}
return currentRefinement;
}

function getValue(name, props, searchState, context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ function getId() {
function getCurrentRefinement(props, searchState, context) {
const id = getId();
const page = 1;
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
id,
page,
currentRefinement => {
if (typeof currentRefinement === 'string') {
return parseInt(currentRefinement, 10);
}
return currentRefinement;
}
page
);

if (typeof currentRefinement === 'string') {
return parseInt(currentRefinement, 10);
}
return currentRefinement;
}

function refine(props, searchState, nextPage, context) {
Expand Down
44 changes: 19 additions & 25 deletions packages/react-instantsearch-core/src/connectors/connectRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,31 @@ function getCurrentRange(boundaries, stats, precision) {
}

function getCurrentRefinement(props, searchState, currentRange, context) {
const refinement = getCurrentRefinementValue(
const { min, max } = getCurrentRefinementValue(
props,
searchState,
context,
`${namespace}.${getId(props)}`,
{},
currentRefinement => {
const { min, max } = currentRefinement;
const isFloatPrecision = Boolean(props.precision);

let nextMin = min;
if (typeof nextMin === 'string') {
nextMin = isFloatPrecision
? parseFloat(nextMin)
: parseInt(nextMin, 10);
}

let nextMax = max;
if (typeof nextMax === 'string') {
nextMax = isFloatPrecision
? parseFloat(nextMax)
: parseInt(nextMax, 10);
}

return {
min: nextMin,
max: nextMax,
};
}
{}
);

const isFloatPrecision = Boolean(props.precision);

let nextMin = min;
if (typeof nextMin === 'string') {
nextMin = isFloatPrecision ? parseFloat(nextMin) : parseInt(nextMin, 10);
}

let nextMax = max;
if (typeof nextMax === 'string') {
nextMax = isFloatPrecision ? parseFloat(nextMax) : parseInt(nextMax, 10);
}

const refinement = {
min: nextMin,
max: nextMax,
};

const hasMinBound = props.min !== undefined;
const hasMaxBound = props.max !== undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@ function getId(props) {
}

function getCurrentRefinement(props, searchState, context) {
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
`${namespace}.${getId(props)}`,
[],
currentRefinement => {
if (typeof currentRefinement === 'string') {
// All items were unselected
if (currentRefinement === '') {
return [];
}

// Only one item was in the searchState but we know it should be an array
return [currentRefinement];
}
return currentRefinement;
}
[]
);

if (typeof currentRefinement !== 'string') {
return currentRefinement;
}

if (currentRefinement) {
return [currentRefinement];
}

return [];
}

function getValue(name, props, searchState, context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export default createConnector({
searchState,
this.context,
id,
null,
currentRefinement => currentRefinement
null
);

if (!this._prevSearchState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ function getId() {

function getCurrentRefinement(props, searchState, context) {
const id = getId(props);
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
id,
'',
currentRefinement => {
if (currentRefinement) {
return currentRefinement;
}
return '';
}
''
);

if (currentRefinement) {
return currentRefinement;
}
return '';
}

function refine(props, searchState, nextRefinement, context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ function getId() {

function getCurrentRefinement(props, searchState, context) {
const id = getId(props);
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
id,
null,
currentRefinement => {
if (currentRefinement) {
return currentRefinement;
}
return null;
}
null
);

if (currentRefinement) {
return currentRefinement;
}
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ function getId(props) {
const namespace = 'toggle';

function getCurrentRefinement(props, searchState, context) {
return getCurrentRefinementValue(
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
`${namespace}.${getId(props)}`,
false,
currentRefinement => {
if (currentRefinement) {
return currentRefinement;
}
return false;
}
false
);

if (currentRefinement) {
return currentRefinement;
}
return false;
}

function refine(props, searchState, nextRefinement, context) {
Expand Down
Loading

0 comments on commit f91906f

Please sign in to comment.