• Resolved kmarcink2

    (@kmarcink2)


    Hi,

    I need to customise a lot the default behaviour of some widgets. I read about for example connectRefinementList. Does your plugin already provide support for it or do I need to figure other way to use it?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    With potential exception to features not available in the Instantsearch version we ship with (we do try to stay current as able), all of the code we ship with at https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.8.2/templates/instantsearch.php should be overall fully ready to match up with Algolia’s documentation around Instantsearch, including customization with connectors.

    That file is essentially a custom PHP file used to load vanilla JS InstantSearch code, outside of a couple of custom PHP spots. The hit template usage can totally be converted to JS template literals if you prefer, as an extra example instead of the data.* version found on lines 47-72.

    Thread Starter kmarcink2

    (@kmarcink2)

    Great to hear.

    I see that to access for example “connectRefinementList”, I nee dto use the following code in my js file.

    const { connectRefinementList } = instantsearch.connectors;

    Source: https://www.algolia.com/doc/api-reference/widgets/refinement-list/js/?client=With+a+CDN

    Thank you.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yep, most likely. I haven’t done much with connectors personally, but I can try and help best i can on that front. However that’s also slightly out of the realm of support specifically for our plugin as well.

    Thread Starter kmarcink2

    (@kmarcink2)

    Thank you, that’s very kind of you.

    Algolia code examples are a great starting point. It works our of the box after adding it to the project. Then I just need to customize it for my needs. So it should not be a challenge. Example.

    // 1. Create a render function
    const renderRefinementList = (renderOptions, isFirstRender) => {
    const {
    items,
    isFromSearch,
    refine,
    createURL,
    isShowingMore,
    canToggleShowMore,
    searchForItems,
    toggleShowMore,
    widgetParams,
    } = renderOptions;

    if (isFirstRender) {
    const input = document.createElement('input');
    const ul = document.createElement('ul');
    const button = document.createElement('button');
    button.textContent = 'Show more';

    input.addEventListener('input', event => {
    searchForItems(event.currentTarget.value);
    });

    button.addEventListener('click', () => {
    toggleShowMore();
    });

    widgetParams.container.appendChild(input);
    widgetParams.container.appendChild(ul);
    widgetParams.container.appendChild(button);
    }

    const input = widgetParams.container.querySelector('input');

    if (!isFromSearch && input.value) {
    input.value = '';
    }

    widgetParams.container.querySelector('ul').innerHTML = items
    .map(
    item =>
    <br> <li><br> <a<br> href="${createURL(item.value)}"<br> data-value="${item.value}"<br> style="font-weight: ${item.isRefined ? 'bold' : ''}"<br> ><br> ${item.label} (${item.count})<br> </a><br> </li><br>
    )
    .join('');

    [...widgetParams.container.querySelectorAll('a')].forEach(element => {
    element.addEventListener('click', event => {
    event.preventDefault();
    refine(event.currentTarget.dataset.value);
    });
    });

    const button = widgetParams.container.querySelector('button');

    button.disabled = !canToggleShowMore;
    button.textContent = isShowingMore ? 'Show less' : 'Show more';
    };

    // 2. Create the custom widget
    const customRefinementList = connectRefinementList(
    renderRefinementList
    );

    // 3. Instantiate
    search.addWidgets([
    customRefinementList({
    container: document.querySelector('#refinement-list'),
    attribute: 'brand',
    showMoreLimit: 20,
    })
    ]);

    Source: https://www.algolia.com/doc/api-reference/widgets/refinement-list/js/?client=With+a+CDN#full-example

    • This reply was modified 1 year, 3 months ago by kmarcink2.
    Thread Starter kmarcink2

    (@kmarcink2)

    My current challenge is adding a proxy server that caches Algolia responses, to not go bankrupt when Algolia sends a bill.

    I have came across an example of Algolia cache proxy – Cloudflare worker, but having difficulties make it work.
    Source: https://gist.github.com/etdev/bb49f0f60ea5ec9deb5977b1fbfb4046

    So you know of any solutions to this challenge?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can’t say I have, but I wonder if this would help you a bit https://www.algolia.com/doc/ui-libraries/autocomplete/guides/debouncing-sources/

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom Algolia widgets’ is closed to new replies.