This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 386
/
connectToggleRefinement.js
189 lines (162 loc) · 5.86 KB
/
connectToggleRefinement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import PropTypes from 'prop-types';
import { find } from '../core/utils';
import createConnector from '../core/createConnector';
import {
cleanUpValue,
getIndexId,
getResults,
refineValue,
getCurrentRefinementValue,
} from '../core/indexUtils';
function getId(props) {
return props.attribute;
}
const namespace = 'toggle';
const falsyStrings = ['0', 'false', 'null', 'undefined'];
function getCurrentRefinement(props, searchState, context) {
const currentRefinement = getCurrentRefinementValue(
props,
searchState,
context,
`${namespace}.${getId(props)}`,
false
);
if (falsyStrings.indexOf(currentRefinement) !== -1) {
return false;
}
return Boolean(currentRefinement);
}
function refine(props, searchState, nextRefinement, context) {
const id = getId(props);
const nextValue = { [id]: nextRefinement ? nextRefinement : false };
const resetPage = true;
return refineValue(searchState, nextValue, context, resetPage, namespace);
}
function cleanUp(props, searchState, context) {
return cleanUpValue(searchState, context, `${namespace}.${getId(props)}`);
}
/**
* connectToggleRefinement connector provides the logic to build a widget that will
* provides an on/off filtering feature based on an attribute value.
* @name connectToggleRefinement
* @kind connector
* @requirements To use this widget, you'll need an attribute to toggle on.
*
* You can't toggle on null or not-null values. If you want to address this particular use-case you'll need to compute an
* extra boolean attribute saying if the value exists or not. See this [thread](https://discourse.algolia.com/t/how-to-create-a-toggle-for-the-absence-of-a-string-attribute/2460) for more details.
*
* @propType {string} attribute - Name of the attribute on which to apply the `value` refinement. Required when `value` is present.
* @propType {string} label - Label for the toggle.
* @propType {string} value - Value of the refinement to apply on `attribute`.
* @propType {boolean} [defaultRefinement=false] - Default searchState of the widget. Should the toggle be checked by default?
* @providedPropType {boolean} currentRefinement - `true` when the refinement is applied, `false` otherwise
* @providedPropType {object} count - an object that contains the count for `checked` and `unchecked` state
* @providedPropType {function} refine - a function to toggle a refinement
* @providedPropType {function} createURL - a function to generate a URL for the corresponding search state
*/
export default createConnector({
displayName: 'AlgoliaToggle',
$$type: 'ais.toggle',
propTypes: {
label: PropTypes.string.isRequired,
attribute: PropTypes.string.isRequired,
value: PropTypes.any.isRequired,
filter: PropTypes.func,
defaultRefinement: PropTypes.bool,
},
getProvidedProps(props, searchState, searchResults) {
const { attribute, value } = props;
const results = getResults(searchResults, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
const currentRefinement = getCurrentRefinement(props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
const allFacetValues =
results && results.getFacetByName(attribute)
? results.getFacetValues(attribute)
: null;
const facetValue =
// Use null to always be consistent with type of the value
// count: number | null
allFacetValues && allFacetValues.length
? find(allFacetValues, (item) => item.name === value.toString())
: null;
const facetValueCount = facetValue && facetValue.count;
const allFacetValuesCount =
// Use null to always be consistent with type of the value
// count: number | null
allFacetValues && allFacetValues.length
? allFacetValues.reduce((acc, item) => acc + item.count, 0)
: null;
const canRefine = currentRefinement
? allFacetValuesCount !== null && allFacetValuesCount > 0
: facetValueCount !== null && facetValueCount > 0;
const count = {
checked: allFacetValuesCount,
unchecked: facetValueCount,
};
return {
currentRefinement,
canRefine,
count,
};
},
refine(props, searchState, nextRefinement) {
return refine(props, searchState, nextRefinement, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
},
cleanUp(props, searchState) {
return cleanUp(props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
},
getSearchParameters(searchParameters, props, searchState) {
const { attribute, value, filter } = props;
const checked = getCurrentRefinement(props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
let nextSearchParameters = searchParameters.addDisjunctiveFacet(attribute);
if (checked) {
nextSearchParameters = nextSearchParameters.addDisjunctiveFacetRefinement(
attribute,
value
);
if (filter) {
nextSearchParameters = filter(nextSearchParameters);
}
}
return nextSearchParameters;
},
getMetadata(props, searchState) {
const id = getId(props);
const checked = getCurrentRefinement(props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
const items = [];
const index = getIndexId({
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});
if (checked) {
items.push({
label: props.label,
currentRefinement: checked,
attribute: props.attribute,
value: (nextState) =>
refine(props, nextState, false, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
}),
});
}
return { id, index, items };
},
});