-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(queryRules): add queryRuleContext widget
- Loading branch information
1 parent
579abe8
commit 298bafe
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/widgets/query-rule-context/__tests__/query-rule-context-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import queryRuleContext from '../query-rule-context'; | ||
|
||
describe('queryRuleContext', () => { | ||
describe('Usage', () => { | ||
test('does not throw without options', () => { | ||
expect(() => { | ||
// @ts-ignore | ||
queryRuleContext(); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('does not throw with empty options', () => { | ||
expect(() => { | ||
queryRuleContext({}); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import noop from 'lodash/noop'; | ||
import { WidgetFactory } from '../../types'; | ||
import connectQueryRules, { | ||
QueryRulesConnectorParams, | ||
} from '../../connectors/query-rules/connectQueryRules'; | ||
|
||
type QueryRulesWidgetParams = Pick< | ||
QueryRulesConnectorParams, | ||
'trackedFilters' | 'transformRuleContexts' | ||
>; | ||
|
||
type QueryRuleContext = WidgetFactory<QueryRulesWidgetParams>; | ||
|
||
const queryRuleContext: QueryRuleContext = ({ | ||
trackedFilters, | ||
transformRuleContexts, | ||
} = {}) => { | ||
return connectQueryRules(noop)({ | ||
trackedFilters, | ||
transformRuleContexts, | ||
}); | ||
}; | ||
|
||
export default queryRuleContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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({ | ||
container, | ||
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) | ||
); |