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

Commit 03dea3a

Browse files
authored
feat(StateResults): give access to status and error (#1151)
* feat(StateResults): give access to status and error FX-1770 Note that this changes the implementation from connectStateResults to a render listener, as `loading` is otherwise inconsistently visible. This means it will no longer be counted in metadata. Updated the helper in devDep to use ^ to avoid a duplicate with the InstantSearch.js version * up margin
1 parent 93dfdc1 commit 03dea3a

File tree

7 files changed

+96
-30
lines changed

7 files changed

+96
-30
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"release": "shipjs prepare"
4747
},
4848
"dependencies": {
49-
"instantsearch.js": "^4.45.0",
49+
"instantsearch.js": "^4.47.0",
5050
"mitt": "^2.1.0"
5151
},
5252
"peerDependencies": {
@@ -81,7 +81,7 @@
8181
"@wdio/spec-reporter": "^5.11.7",
8282
"@wdio/static-server-service": "^5.11.0",
8383
"algoliasearch": "4.0.1",
84-
"algoliasearch-helper": "3.10.0",
84+
"algoliasearch-helper": "^3.10.0",
8585
"babel-eslint": "10.0.1",
8686
"babel-jest": "23.6.0",
8787
"babel-preset-es2015": "6.24.1",
@@ -133,11 +133,11 @@
133133
"bundlesize": [
134134
{
135135
"path": "./vue2/umd/index.js",
136-
"maxSize": "56.75 kB"
136+
"maxSize": "57.25 kB"
137137
},
138138
{
139139
"path": "./vue3/umd/index.js",
140-
"maxSize": "58.00 kB"
140+
"maxSize": "58.25 kB"
141141
},
142142
{
143143
"path": "./vue2/cjs/index.js",

src/__tests__/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const nonWidgetComponents = [
1616
'AisSnippet',
1717
'AisPanel',
1818
'AisPoweredBy',
19+
'AisStateResults',
1920
];
2021

2122
function getAllComponents() {

src/components/StateResults.vue

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,77 @@
1212
</p>
1313
<pre>results: {{ Object.keys(state.results) }}</pre>
1414
<pre>state: {{ Object.keys(state.state) }}</pre>
15+
<pre>status: {{ state.status }}</pre>
16+
<pre>error: {{ state.error }}</pre>
1517
</slot>
1618
</div>
1719
</template>
1820

1921
<script>
22+
import { isVue3 } from '../util/vue-compat';
2023
import { createSuitMixin } from '../mixins/suit';
2124
import { createWidgetMixin } from '../mixins/widget';
2225
import { _objectSpread } from '../util/polyfills';
23-
import connectStateResults from '../connectors/connectStateResults';
2426
2527
export default {
2628
name: 'AisStateResults',
2729
mixins: [
28-
createWidgetMixin(
29-
{
30-
connector: connectStateResults,
31-
},
32-
{
33-
$$widgetType: 'ais.stateResults',
34-
}
35-
),
30+
createWidgetMixin({ connector: true }),
3631
createSuitMixin({ name: 'StateResults' }),
3732
],
33+
props: {
34+
catchError: {
35+
type: Boolean,
36+
default: false,
37+
},
38+
},
39+
data() {
40+
return {
41+
renderFn: () => {
42+
const { status, error } = this.instantSearchInstance;
43+
const results = this.getParentIndex().getResults();
44+
const helper = this.getParentIndex().getHelper();
45+
const state = helper ? helper.state : null;
46+
47+
// @MAJOR no longer spread this inside `results`
48+
this.state = {
49+
results,
50+
state,
51+
status,
52+
error,
53+
};
54+
},
55+
};
56+
},
57+
created() {
58+
this.instantSearchInstance.addListener('render', this.renderFn);
59+
},
60+
[isVue3 ? 'beforeUnmount' : 'beforeDestroy']() {
61+
if (this.widget) {
62+
this.instantSearchInstance.removeListener('render', this.renderFn);
63+
if (this.errorFn) {
64+
this.instantSearchInstance.removeListener('error', this.errorFn);
65+
}
66+
}
67+
},
68+
watch: {
69+
catchError: {
70+
immediate: true,
71+
handler(catchError) {
72+
if (catchError) {
73+
this.errorFn = () => {};
74+
this.instantSearchInstance.addListener('error', this.errorFn);
75+
} else if (this.errorFn) {
76+
this.instantSearchInstance.removeListener('error', this.errorFn);
77+
this.errorFn = undefined;
78+
}
79+
},
80+
},
81+
},
3882
computed: {
3983
stateResults() {
40-
// @MAJOR: replace v-bind="stateResults" with :state="state.state" :results="state.results"
41-
const { state, results } = this.state;
42-
return _objectSpread({}, results, { results, state });
84+
const { results, state, status, error } = this.state;
85+
return _objectSpread({}, results, { results, state, status, error });
4386
},
4487
},
4588
};

src/components/__tests__/StateResults.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ it('renders explanation if no slot is used', () => {
1313
state: {
1414
query: 'this is the query',
1515
},
16+
status: 'idle',
17+
error: undefined,
1618
});
1719
const wrapper = mount(StateResults);
1820
expect(wrapper.html()).toMatchSnapshot();
@@ -39,6 +41,8 @@ it('gives state & results to default slot', () => {
3941
__setState({
4042
state,
4143
results,
44+
status: 'idle',
45+
error: undefined,
4246
});
4347

4448
mount(StateResults, {
@@ -47,6 +51,8 @@ it('gives state & results to default slot', () => {
4751
expect(props).toEqual(expect.objectContaining(results));
4852
expect(props.results).toEqual(results);
4953
expect(props.state).toEqual(state);
54+
expect(props.status).toEqual('idle');
55+
expect(props.error).toEqual(undefined);
5056
},
5157
},
5258
});

src/components/__tests__/__snapshots__/StateResults.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,11 @@ exports[`renders explanation if no slot is used 1`] = `
2020
"query"
2121
]
2222
</pre>
23+
<pre>
24+
status: idle
25+
</pre>
26+
<pre>
27+
error:
28+
</pre>
2329
</div>
2430
`;

src/mixins/__mocks__/widget.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ export const createWidgetMixin = jest.fn(() => ({
2222
return {
2323
state,
2424
widget,
25+
instantSearchInstance: {
26+
status: 'idle',
27+
error: undefined,
28+
addListener: () => {},
29+
removeListener: () => {},
30+
},
31+
getParentIndex: () => ({
32+
getResults: () => null,
33+
getHelper: () => null,
34+
}),
2535
};
2636
},
2737
}));

yarn.lock

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,10 +1491,10 @@ ajv@^6.5.5, ajv@^6.9.1:
14911491
json-schema-traverse "^0.4.1"
14921492
uri-js "^4.2.2"
14931493

1494-
algoliasearch-helper@3.10.0, algoliasearch-helper@^3.10.0:
1495-
version "3.10.0"
1496-
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.10.0.tgz#59a0f645dd3c7e55cf01faa568d1af50c49d36f6"
1497-
integrity sha512-4E4od8qWWDMVvQ3jaRX6Oks/k35ywD011wAA4LbYMMjOtaZV6VWaTjRr4iN2bdaXP2o1BP7SLFMBf3wvnHmd8Q==
1494+
algoliasearch-helper@^3.10.0, algoliasearch-helper@^3.11.1:
1495+
version "3.11.1"
1496+
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.11.1.tgz#d83ab7f1a2a374440686ef7a144b3c288b01188a"
1497+
integrity sha512-mvsPN3eK4E0bZG0/WlWJjeqe/bUD2KOEVOl0GyL/TGXn6wcpZU8NOuztGHCUKXkyg5gq6YzUakVTmnmSSO5Yiw==
14981498
dependencies:
14991499
"@algolia/events" "^4.0.1"
15001500

@@ -8373,22 +8373,22 @@ instantsearch.css@7.3.1:
83738373
resolved "https://registry.yarnpkg.com/instantsearch.css/-/instantsearch.css-7.3.1.tgz#7ab74a8f355091ae040947a9cf5438f379026622"
83748374
integrity sha512-/kaMDna5D+Q9mImNBHEhb9HgHATDOFKYii7N1Iwvrj+lmD9gBJLqVhUw67gftq2O0QI330pFza+CRscIwB1wQQ==
83758375

8376-
instantsearch.js@^4.45.0:
8377-
version "4.45.0"
8378-
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.45.0.tgz#52be36dffd9b6b9616184c783bb8ac1f39ff205c"
8379-
integrity sha512-gjJDnFJO2yfkmOb0X1mZumqMGHlZIxAVNusnKfNh8s4u6SsRCM9p8S3eJi5aYo3mbS9NrePR8B44/gXh4YGcQQ==
8376+
instantsearch.js@^4.47.0:
8377+
version "4.47.0"
8378+
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.47.0.tgz#eb06d4deede956dff9391e577469061e0d7e9c56"
8379+
integrity sha512-SoIVDGtqKFtGQxcEr2vKo0SxbRjAksRKUDP+kRvD/J6tqx8qVZdSArY3XwvEOmhqee/0wK4p4ZMAJS+NI8M8cg==
83808380
dependencies:
83818381
"@algolia/events" "^4.0.1"
83828382
"@algolia/ui-components-highlight-vdom" "^1.1.2"
83838383
"@algolia/ui-components-shared" "^1.1.2"
83848384
"@types/google.maps" "^3.45.3"
83858385
"@types/hogan.js" "^3.0.0"
83868386
"@types/qs" "^6.5.3"
8387-
algoliasearch-helper "^3.10.0"
8387+
algoliasearch-helper "^3.11.1"
83888388
classnames "^2.2.5"
83898389
hogan.js "^3.0.2"
83908390
htm "^3.0.0"
8391-
preact "^10.6.0"
8391+
preact "^10.10.0"
83928392
qs "^6.5.1 < 6.10"
83938393
search-insights "^2.1.0"
83948394

@@ -12311,10 +12311,10 @@ posthtml@^0.10.1:
1231112311
posthtml-parser "^0.3.0"
1231212312
posthtml-render "^1.0.5"
1231312313

12314-
preact@^10.6.0:
12315-
version "10.6.4"
12316-
resolved "https://registry.yarnpkg.com/preact/-/preact-10.6.4.tgz#ad12c409ff1b4316158486e0a7b8d43636f7ced8"
12317-
integrity sha512-WyosM7pxGcndU8hY0OQlLd54tOU+qmG45QXj2dAYrL11HoyU/EzOSTlpJsirbBr1QW7lICxSsVJJmcmUglovHQ==
12314+
preact@^10.10.0:
12315+
version "10.11.0"
12316+
resolved "https://registry.yarnpkg.com/preact/-/preact-10.11.0.tgz#26af45a0613f4e17a197cc39d7a1ea23e09b2532"
12317+
integrity sha512-Fk6+vB2kb6mSJfDgODq0YDhMfl0HNtK5+Uc9QqECO4nlyPAQwCI+BKyWO//idA7ikV7o+0Fm6LQmNuQi1wXI1w==
1231812318

1231912319
prelude-ls@~1.1.2:
1232012320
version "1.1.2"

0 commit comments

Comments
 (0)