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

proposal: add selectedfilters getter to the FilterManager #267

Closed
Youhan opened this issue May 26, 2023 · 1 comment · Fixed by #279
Closed

proposal: add selectedfilters getter to the FilterManager #267

Youhan opened this issue May 26, 2023 · 1 comment · Fixed by #279
Labels
enhancement New feature or request

Comments

@Youhan
Copy link

Youhan commented May 26, 2023

I thought it would be nice to add this feature to the filter manager to keep track of selected filters.

One use case is to be able to add a list of selected filters somewhere on the page.
It could also be used to refactor some other methods that are doing calculations to see which filters are selected.

I am doing it this way, as a sample

// add a getter method for selected filters to manager prototype
Object.defineProperty(manager, 'selectedFilters', {
    get: function() {
        const selectedFilters = [];
        this.filters.forEach(filter => {
            if (
                FilterManager.isKlevuFilterResultOptions(filter) ||
                FilterManager.isKlevuFilterResultRating(filter)
            ) {
                const selected = filter.options.filter(
                    subOption => subOption.selected === true
                );
                if (selected.length === 0) {
                    return;
                }
                for (const option of selected) {
                    selectedFilters.push({
                        key: filter.key,
                        label: filter.label,
                        type: filter.type,
                        value: option.value,
                    });
                }
            }
            else if (FilterManager.isKlevuFilterResultSlider(filter)) {
                if (!filter.start || !filter.end) {
                    return;
                }
                selectedFilters.push({
                    key: filter.key,
                    label: filter.label,
                    type: filter.type,
                    value: `${filter.start}-${filter.end}`
                });
            }
        });

        return selectedFilters;
    }
});

Then we can use it like

const manager = new FilterManager()
console.log(manager.selectedFilters)
@rallu
Copy link
Contributor

rallu commented May 29, 2023

Looks excellent and very handy! Would you have time to create a PR our of this? You would get your name in the credits... :)

Here is the filterManager to add it to: https://github.com/klevultd/frontend-sdk/blob/master/packages/klevu-core/src/store/filterManager.ts

@rallu rallu added the enhancement New feature or request label May 29, 2023
@rallu rallu closed this as completed in #279 Jun 5, 2023
rallu added a commit that referenced this issue Jun 5, 2023
Creates new feature to filter manager that lists all currently selected
filters as simple array.

Implements and closes #267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants