Skip to content

Commit

Permalink
Support multiple possible values in a collection filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Nov 15, 2024
1 parent 809fea0 commit 0257b88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions src/lib/services/contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,28 @@ export const getEntriesByCollection = (collectionName) => {
_i18n: { defaultLocale: locale },
} = collection;

return get(allEntries).filter(
(entry) =>
getCollectionsByEntry(entry).some((_collection) => _collection.name === collectionName) &&
(!filter ||
getPropertyValue({ entry, locale, collectionName, key: filter.field }) === filter.value),
);
const filterField = filter?.field;

// eslint-disable-next-line no-nested-ternary
const filterValues = filter?.value
? Array.isArray(filter.value)
? filter.value
: [filter.value]
: [];

return get(allEntries).filter((entry) => {
if (!getCollectionsByEntry(entry).some(({ name }) => name === collectionName)) {
return false;
}

if (!filterField) {
return true;
}

return filterValues.includes(
getPropertyValue({ entry, locale, collectionName, key: filterField }),
);
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@
* @property {string} [public_folder] - Public media folder path for a folder/entry collection.
* @property {object} [filter] - Filter for a folder/entry collection.
* @property {string} filter.field - Field name.
* @property {any} filter.value - Field value.
* @property {any | any[]} filter.value - Field value. Multiple values can be defined with an array.
* @property {object} [nested] - Nested collection config for a folder/entry collection.
* @property {boolean} [hide] - Whether to hide the collection in the UI.
* @property {boolean} [create] - Whether to allow creating items in a folder/entry collection.
Expand Down

0 comments on commit 0257b88

Please sign in to comment.