Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(queryRules): add queryRuleContext widget #3602

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/widgets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ export { default as panel } from './panel/panel';
export {
default as queryRuleCustomData,
} from './query-rule-custom-data/query-rule-custom-data';
export {
default as queryRuleContext,
} from './query-rule-context/query-rule-context';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import queryRuleContext from '../query-rule-context';

describe('queryRuleContext', () => {
describe('Usage', () => {
test('throws trackedFilters error without options', () => {
expect(() => {
// @ts-ignore
queryRuleContext();
}).toThrowErrorMatchingInlineSnapshot(`
"The \`trackedFilters\` option is required.

See documentation: https://www.algolia.com/doc/api-reference/widgets/query-rule-context/js/"
`);
});

test('throws trackedFilters error with empty options', () => {
expect(() => {
// @ts-ignore
queryRuleContext({});
}).toThrowErrorMatchingInlineSnapshot(`
"The \`trackedFilters\` option is required.

See documentation: https://www.algolia.com/doc/api-reference/widgets/query-rule-context/js/"
`);
});
});
});
33 changes: 33 additions & 0 deletions src/widgets/query-rule-context/query-rule-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import noop from 'lodash/noop';
import { WidgetFactory } from '../../types';
import { createDocumentationMessageGenerator } from '../../lib/utils';
import connectQueryRules, {
ParamTrackedFilters,
ParamTransformRuleContexts,
} from '../../connectors/query-rules/connectQueryRules';

type QueryRulesWidgetParams = {
trackedFilters: ParamTrackedFilters;
transformRuleContexts?: ParamTransformRuleContexts;
};

type QueryRuleContext = WidgetFactory<QueryRulesWidgetParams>;

const withUsage = createDocumentationMessageGenerator({
name: 'query-rule-context',
});

const queryRuleContext: QueryRuleContext = (
{ trackedFilters, transformRuleContexts } = {} as QueryRulesWidgetParams
) => {
if (!trackedFilters) {
throw new Error(withUsage('The `trackedFilters` option is required.'));
}

return connectQueryRules(noop)({
trackedFilters,
transformRuleContexts,
});
};

export default queryRuleContext;
116 changes: 116 additions & 0 deletions stories/query-rule-context.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { storiesOf } from '@storybook/html';
import { withHits } from '../.storybook/decorators';
import moviesPlayground from '../.storybook/playgrounds/movies';

type CustomDataItem = {
title: string;
banner: string;
link: string;
};

const searchOptions = {
appId: 'latency',
apiKey: 'af044fb0788d6bb15f807e4420592bc5',
indexName: 'instant_search_movies',
playground: moviesPlayground,
};

storiesOf('QueryRuleContext', module)
.add(
'default',
withHits(({ search, container, instantsearch }) => {
const widgetContainer = document.createElement('div');
const description = document.createElement('ul');
description.innerHTML = `
<li>Select the "Drama" category and The Shawshank Redemption appears</li>
<li>Select the "Thriller" category and Pulp Fiction appears</li>
<li>Type <q>music</q> and a banner will appear.</li>
`;

container.appendChild(description);
container.appendChild(widgetContainer);

search.addWidget(
instantsearch.widgets.queryRuleContext({
trackedFilters: {
genre: () => ['Thriller', 'Drama'],
},
})
);

search.addWidget(
instantsearch.widgets.queryRuleCustomData({
container: widgetContainer,
transformItems: (items: CustomDataItem[]) => items[0],
templates: {
default({ title, banner, link }: CustomDataItem) {
if (!banner) {
return '';
}

return `
<h2>${title}</h2>

<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
`;
},
},
})
);
}, searchOptions)
)
.add(
'with initial filter',
withHits(({ search, container, instantsearch }) => {
const widgetContainer = document.createElement('div');
const description = document.createElement('ul');
description.innerHTML = `
<li>Select the "Drama" category and The Shawshank Redemption appears</li>
<li>Select the "Thriller" category and Pulp Fiction appears</li>
<li>Type <q>music</q> and a banner will appear.</li>
`;

container.appendChild(description);
container.appendChild(widgetContainer);

search.addWidget(
instantsearch.widgets.configure({
disjunctiveFacetsRefinements: {
genre: ['Drama'],
},
})
);

search.addWidget(
instantsearch.widgets.queryRuleContext({
trackedFilters: {
genre: () => ['Thriller', 'Drama'],
},
})
);

search.addWidget(
instantsearch.widgets.queryRuleCustomData({
container: widgetContainer,
transformItems: (items: CustomDataItem[]) => items[0],
templates: {
default({ title, banner, link }: CustomDataItem) {
if (!banner) {
return '';
}

return `
<h2>${title}</h2>

<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
`;
},
},
})
);
}, searchOptions)
);