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

fix: deduplicate filters #38

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,37 @@ const DevelopersResourcesFilters = dynamic(

const mapItemsIntoFilters = (itemFilters) => (item) => {
// handle the `category` field
if (!!item?.category)
if (!!item?.category) {
if (!itemFilters.category.items.includes(item?.category)) {
itemFilters.category.items.push(item?.category);
}
}

// handle the `difficulty` field
if (!!item?.difficulty)
if (!!item?.difficulty) {
if (!itemFilters.difficulty.items.includes(item?.difficulty)) {
console.log(item.difficulty);
itemFilters.difficulty.items.push(item?.difficulty);
}
}

// handle the `labels` field
if (!!item?.labels)
if (!!item?.labels) {
item.labels?.forEach((label) => {
if (!itemFilters?.labels.items.includes(label)) {
itemFilters.labels.items.push(label);
}
});
}

// handle the `tags` field
if (!!item?.tags)
if (!!item?.tags) {
item.tags.forEach((tag) => {
if (!itemFilters.tags.items.includes(tag)) {
itemFilters.tags.items.push(tag);
}
});
}

return item;
};
Expand Down Expand Up @@ -73,6 +78,20 @@ export default memo(function DevelopersResources({ items, title = "" }) {
// map all the filterable items to populate the filters
items.map(mapItemsIntoFilters(filters));

// deduplicate all filters
filters.difficulty.items = Array.from(
new Set(filters.difficulty.items.map((item) => item.toLowerCase())),
);
filters.category.items = Array.from(
new Set(filters.category.items.map((item) => item.toLowerCase())),
);
filters.tags.items = Array.from(
new Set(filters.tags.items.map((item) => item.toLowerCase())),
);
filters.labels.items = Array.from(
new Set(filters.labels.items.map((item) => item.toLowerCase())),
);

// force sort all filters to the same, always
for (const key in filters) filters[key].items.sort();

Expand Down
Loading