/* global instantsearch algoliasearch */ const search = instantsearch({ indexName: 'instant_search', searchClient: algoliasearch('latency', '6be0576ff61c053d5f9a3225e2a90f76'), }); search.addWidget( instantsearch.widgets.searchBox({ container: '#searchbox', placeholder: 'Search for products', }) ); /******** Clear refinements Start ******/ // 1. Create a render function const renderClearRefinements = (renderOptions, isFirstRender) => { const { hasRefinements, refine } = renderOptions; document.querySelector('#clear-refinements').innerHTML = ` `; const button = document.querySelector('#clear-result'); button.addEventListener('click', () => { refine(); }); }; const customClearRefinements = instantsearch.connectors.connectClearRefinements( renderClearRefinements ); search.addWidgets([ customClearRefinements({ container: document.querySelector('#clear-refinements'), }) ]); /******** Clear refinements End ******/ // Brand List const renderRefinementList = (renderOptions, isFirstRender) => { const { items, refine } = renderOptions; const container = document.querySelector('#brand-list'); container.innerHTML = ` `; [...container.querySelectorAll('.brand-checkbox')].forEach(element => { element.addEventListener('change', event => { event.preventDefault(); refine(event.currentTarget.dataset.value); }); }); }; const customRefinementList = instantsearch.connectors.connectRefinementList( renderRefinementList ); search.addWidgets([ customRefinementList({ container: document.querySelector('#brand-list'), attribute: 'brand' }) ]); // Rating Menu search.addWidget( instantsearch.widgets.ratingMenu({ container: '#rating-menu', attribute: 'rating', templates: { item(data) { return `
${data.stars .map( isFilled => isFilled ? `` : `` ).join('')} & Up
${data.count}
`; }, }, }) ); search.addWidget( instantsearch.widgets.stats({ container: '#stats', }) ); search.addWidget( instantsearch.widgets.poweredBy({ container: '#powered-by', }) ); // Free Shiping const renderToggleRefinement = (renderOptions, isFirstRender) => { const { value, refine } = renderOptions; document.querySelector('#toggle-refinement').innerHTML = `
`; const input = document.getElementById('toggle-refinement'); input.addEventListener('change', event => { refine({ isRefined: !event.target.checked }); }); }; const customToggleRefinement = instantsearch.connectors.connectToggleRefinement( renderToggleRefinement ); search.addWidgets([ customToggleRefinement({ attribute: 'free_shipping', container: document.querySelector('#toggle-refinement'), // instance params }) ]); search.addWidget( instantsearch.widgets.sortBy({ container: '#sort-by', cssClasses: { root: 'iq-algolia-sort' }, items: [ { label: 'Featured', value: 'instant_search' }, { label: 'Price (asc)', value: 'instant_search_price_asc' }, { label: 'Price (desc)', value: 'instant_search_price_desc' }, ], }) ); search.addWidget( instantsearch.widgets.numericMenu({ container: '#numeric-menu', attribute: 'price', items: [ { label: 'All' }, { label: 'Less than 500$', end: 500 }, { label: 'Between 500$ - 1000$', start: 500, end: 1000 }, { label: 'More than 1000$', start: 1000 }, ], }) ); /************ Create Product Item Custom Widget Start *************/ const renderHits = (renderOptions, isFirstRender) => { const { hits, widgetParams } = renderOptions; widgetParams.container.innerHTML = `
`; $(document).on('click','.addToCart',function (e) { let id = $(this).attr('id') let item = hits.find(product => id === product.objectID) addToCart(item) }) $(document).on('click','.addToWish',function (e) { let id = $(this).attr('id') let item = hits.find(product => id === product.objectID) addToWish(item) }) }; const customHits = instantsearch.connectors.connectHits(renderHits); search.addWidgets([ customHits({ container: document.querySelector('#hits') }) ]); /************ Create Product Item Custom Widget End *************/ search.addWidget( instantsearch.widgets.pagination({ container: '#pagination', }) ); search.start();