Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 10 additions & 9 deletions app/(builder)/ycode/api/collections/[id]/items/filter/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export async function POST(
localeCode,
collectionLayerClasses,
collectionLayerTag,
published: isPublished = true,
} = body;

if (!layerTemplate || !Array.isArray(layerTemplate)) {
Expand All @@ -508,7 +509,7 @@ export async function POST(

const { matchingIds, total: filteredTotal } = await getFilteredItemIds(
collectionId,
true,
isPublished,
filterGroups,
);

Expand All @@ -525,7 +526,7 @@ export async function POST(

if (!sortBy || sortBy === 'none' || sortBy === 'manual') {
// Let DB do ordering and pagination for cheap paths.
const { items } = await getItemsByCollectionId(collectionId, true, {
const { items } = await getItemsByCollectionId(collectionId, isPublished, {
itemIds: matchingIds,
limit: pageLimit,
offset: pageOffset,
Expand All @@ -536,15 +537,15 @@ export async function POST(
const randomizedIds = [...matchingIds].sort(() => Math.random() - 0.5);
pageItemIds = randomizedIds.slice(pageOffset, pageOffset + pageLimit);
if (pageItemIds.length > 0) {
const { items } = await getItemsByCollectionId(collectionId, true, {
const { items } = await getItemsByCollectionId(collectionId, isPublished, {
itemIds: pageItemIds,
});
pageRawItems = reorderItemsById(items, pageItemIds);
}
} else {
// For field-based sort, sort IDs using just the sort field values first,
// then hydrate only the requested page window.
const sortValueByItem = await getFieldValuesForItems(sortBy, true, matchingIds);
const sortValueByItem = await getFieldValuesForItems(sortBy, isPublished, matchingIds);
const sortedIds = [...matchingIds].sort((a, b) => {
const aStr = String(sortValueByItem.get(a) || '');
const bStr = String(sortValueByItem.get(b) || '');
Expand All @@ -559,7 +560,7 @@ export async function POST(
});
pageItemIds = sortedIds.slice(pageOffset, pageOffset + pageLimit);
if (pageItemIds.length > 0) {
const { items } = await getItemsByCollectionId(collectionId, true, {
const { items } = await getItemsByCollectionId(collectionId, isPublished, {
itemIds: pageItemIds,
});
pageRawItems = reorderItemsById(items, pageItemIds);
Expand All @@ -568,15 +569,15 @@ export async function POST(

const valuesByItem = await getValuesByItemIds(
pageRawItems.map(i => i.id),
true,
isPublished,
);
const paginatedItems: CollectionItemWithValues[] = pageRawItems.map(item => ({
...item,
values: valuesByItem[item.id] || {},
}));
const hasMore = pageOffset + paginatedItems.length < filteredTotal;

const collectionFields = await getFieldsByCollectionId(collectionId, true, { excludeComputed: true });
const collectionFields = await getFieldsByCollectionId(collectionId, isPublished, { excludeComputed: true });
const slugField = collectionFields.find(f => f.key === 'slug');
const collectionItemSlugs: Record<string, string> = {};
if (slugField) {
Expand All @@ -595,7 +596,7 @@ export async function POST(
let locale = null;
let translations: Record<string, any> | undefined;
if (localeCode) {
const localeData = await loadTranslationsForLocale(localeCode, true);
const localeData = await loadTranslationsForLocale(localeCode, isPublished);
locale = localeData.locale;
translations = localeData.translations;
}
Expand All @@ -605,7 +606,7 @@ export async function POST(
layerTemplate as Layer[],
collectionId,
collectionLayerId,
true,
isPublished,
pages,
folders,
collectionItemSlugs,
Expand Down
9 changes: 6 additions & 3 deletions components/FilterableCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface FilterableCollectionProps {
layerTemplate: Layer[];
collectionLayerClasses?: string[];
collectionLayerTag?: string;
isPublished?: boolean;
}

const FC_FILTERED_ATTR = 'data-fc-filtered';
Expand All @@ -37,6 +38,7 @@ export default function FilterableCollection({
layerTemplate,
collectionLayerClasses,
collectionLayerTag,
isPublished = true,
}: FilterableCollectionProps) {
const markerRef = useRef<HTMLSpanElement>(null);
const ssrChildrenRef = useRef<Element[]>([]);
Expand Down Expand Up @@ -185,9 +187,10 @@ export default function FilterableCollection({
if (inputValue && inputValue.includes(',')) {
const checkedValues = inputValue.split(',').filter(Boolean);
if (checkedValues.length > 0) {
const arrayOperators = ['is_one_of', 'is_not_one_of', 'contains_all_of', 'contains_exactly'];
activeInGroup.push({
fieldId: condition.fieldId,
operator: 'is_one_of',
operator: arrayOperators.includes(condition.operator) ? condition.operator : 'is_one_of',
value: JSON.stringify(checkedValues),
fieldType: condition.fieldType,
});
Expand Down Expand Up @@ -529,7 +532,7 @@ export default function FilterableCollection({
sortOrder: effectiveSortOrder,
limit,
offset,
published: true,
published: isPublished,
collectionLayerClasses,
collectionLayerTag,
}),
Expand Down Expand Up @@ -582,7 +585,7 @@ export default function FilterableCollection({
abortRef.current = null;
}
});
}, [collectionId, collectionLayerId, layerTemplate, effectiveSortBy, effectiveSortOrder, limit, paginationMode, updateEmptyStateElements, injectFilteredHTML, collectionLayerClasses, collectionLayerTag]);
}, [collectionId, collectionLayerId, layerTemplate, effectiveSortBy, effectiveSortOrder, limit, paginationMode, updateEmptyStateElements, injectFilteredHTML, collectionLayerClasses, collectionLayerTag, isPublished]);

const fetchFilteredRef = useRef(fetchFiltered);
useEffect(() => { fetchFilteredRef.current = fetchFiltered; }, [fetchFiltered]);
Expand Down
3 changes: 2 additions & 1 deletion components/LayerRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ const LayerRenderer: React.FC<LayerRendererProps> = ({
layerTemplate={layer._filterConfig!.layerTemplate}
collectionLayerClasses={layer._filterConfig!.collectionLayerClasses}
collectionLayerTag={layer._filterConfig!.collectionLayerTag}
isPublished={layer._filterConfig!.isPublished}
>
{content}
</FilterableCollection>
Expand Down Expand Up @@ -803,7 +804,7 @@ const LayerItem: React.FC<{
if (!inputLayerId) return;
const nameAttr = inputEl.getAttribute('name');
if (nameAttr) nameMap[inputLayerId] = nameAttr;
if (inputEl.type === 'checkbox') {
if (inputEl.type === 'checkbox' || inputEl.type === 'radio') {
const checked = (inputEl as HTMLInputElement).checked;
const val = checked ? ((inputEl as HTMLInputElement).value || 'true') : '';
inputValues[inputLayerId] = val;
Expand Down
1 change: 1 addition & 0 deletions lib/page-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,7 @@ export async function resolveCollectionLayers(
layerTemplate: layer.children || [],
collectionLayerClasses: Array.isArray(layer.classes) ? layer.classes : (layer.classes ? [layer.classes] : []),
collectionLayerTag: layer.name || 'div',
isPublished,
} : undefined,
};
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ export interface Layer {
layerTemplate: Layer[];
collectionLayerClasses?: string[];
collectionLayerTag?: string;
isPublished?: boolean;
};
}

Expand Down