fix: search all collections (#594)

This commit is contained in:
Daniel Lautzenheiser
2023-02-26 17:29:07 -05:00
committed by GitHub
parent 2f04788a92
commit 7369c99a19
6 changed files with 65 additions and 98 deletions

View File

@ -83,6 +83,10 @@ const CollectionView = ({
}, [collection]);
const newEntryUrl = useMemo(() => {
if (!collectionName || !collection) {
return undefined;
}
let url = 'fields' in collection && collection.create ? getNewEntryUrl(collectionName) : '';
if (url && filterTerm) {
url = getNewEntryUrl(collectionName);
@ -90,6 +94,7 @@ const CollectionView = ({
url = `${newEntryUrl}?path=${filterTerm}`;
}
}
return url;
}, [collection, collectionName, filterTerm]);
@ -120,6 +125,10 @@ const CollectionView = ({
);
}
if (!collection) {
return null;
}
return (
<EntriesCollection
collection={collection}
@ -142,21 +151,21 @@ const CollectionView = ({
const onSortClick = useCallback(
async (key: string, direction?: SortDirection) => {
await sortByField(collection, key, direction);
collection && (await sortByField(collection, key, direction));
},
[collection, sortByField],
);
const onFilterClick = useCallback(
async (filter: ViewFilter) => {
await filterByField(collection, filter);
collection && (await filterByField(collection, filter));
},
[collection, filterByField],
);
const onGroupClick = useCallback(
async (group: ViewGroup) => {
await groupByField(collection, group);
collection && (await groupByField(collection, group));
},
[collection, groupByField],
);
@ -176,7 +185,7 @@ const CollectionView = ({
return;
}
const defaultSort = collection.sortable_fields?.default;
const defaultSort = collection?.sortable_fields?.default;
if (!defaultSort || !defaultSort.field) {
if (!readyToLoad) {
setReadyToLoad(true);
@ -220,7 +229,7 @@ const CollectionView = ({
<>
<SearchResultContainer>
<SearchResultHeading>
{t(searchResultKey, { searchTerm, collection: collection.label })}
{t(searchResultKey, { searchTerm, collection: collection?.label })}
</SearchResultHeading>
</SearchResultContainer>
<CollectionControls viewStyle={viewStyle} onChangeViewStyle={changeViewStyle} t={t} />
@ -254,7 +263,7 @@ const CollectionView = ({
interface CollectionViewOwnProps {
isSearchResults?: boolean;
isSingleSearchResult?: boolean;
name: string;
name?: string;
searchTerm?: string;
filterTerm?: string;
}
@ -270,13 +279,13 @@ function mapStateToProps(state: RootState, ownProps: TranslatedProps<CollectionV
filterTerm = '',
t,
} = ownProps;
const collection: Collection = name ? collections[name] : collections[0];
const sort = selectEntriesSort(state, collection.name);
const collection = (name ? collections[name] : collections[0]) as Collection | undefined;
const sort = selectEntriesSort(state, collection?.name);
const sortableFields = selectSortableFields(collection, t);
const viewFilters = selectViewFilters(collection);
const viewGroups = selectViewGroups(collection);
const filter = selectEntriesFilter(state, collection.name);
const group = selectEntriesGroup(state, collection.name);
const filter = selectEntriesFilter(state, collection?.name);
const group = selectEntriesGroup(state, collection?.name);
const viewStyle = selectViewStyle(state);
return {

View File

@ -28,11 +28,11 @@ const CollectionRoute = ({
const defaultPath = useMemo(() => getDefaultPath(collections), [collections]);
if (!name || !collection) {
if (!searchTerm && (!name || !collection)) {
return <Navigate to={defaultPath} />;
}
if ('files' in collection && collection.files?.length === 1) {
if (collection && 'files' in collection && collection.files?.length === 1) {
return <Navigate to={`/collections/${collection.name}/entries/${collection.files[0].name}`} />;
}

View File

@ -25,6 +25,7 @@ const EntriesSearch = ({
searchEntries,
clearSearch,
}: EntriesSearchProps) => {
console.log('collections', collections);
const collectionNames = useMemo(() => Object.keys(collections), [collections]);
const getCursor = useCallback(() => {
@ -80,6 +81,7 @@ function mapStateToProps(state: RootState, ownProps: EntriesSearchOwnProps) {
const isFetching = state.search.isFetching;
const page = state.search.page;
const entries = selectSearchedEntries(state, collectionNames);
console.log('searched entries', entries);
return { isFetching, page, collections, viewStyle, entries, searchTerm };
}

View File

@ -156,9 +156,13 @@ export function selectDefaultSortableFields(collection: Collection, backend: Bac
}
export function selectSortableFields(
collection: Collection,
collection: Collection | undefined,
t: (key: string) => string,
): SortableField[] {
if (!collection) {
return [];
}
const fields = (collection.sortable_fields?.fields ?? [])
.map(key => {
if (key === COMMIT_DATE) {
@ -177,12 +181,12 @@ export function selectSortableFields(
return fields;
}
export function selectViewFilters(collection: Collection) {
return collection.view_filters;
export function selectViewFilters(collection?: Collection) {
return collection?.view_filters;
}
export function selectViewGroups(collection: Collection) {
return collection.view_groups;
export function selectViewGroups(collection?: Collection) {
return collection?.view_groups;
}
export function selectFieldsComments(collection: Collection, entryMap: Entry) {

View File

@ -17,17 +17,29 @@ import type {
} from '@staticcms/core/interface';
import type { RootState } from '@staticcms/core/store';
export function selectEntriesSort(entries: RootState, collection: string) {
export function selectEntriesSort(entries: RootState, collection?: string) {
if (!collection) {
return undefined;
}
const sort = entries.entries.sort as Sort | undefined;
return sort?.[collection];
}
export function selectEntriesFilter(entries: RootState, collection: string) {
export function selectEntriesFilter(entries: RootState, collection?: string) {
if (!collection) {
return {};
}
const filter = entries.entries.filter as Filter | undefined;
return filter?.[collection] || {};
}
export function selectEntriesGroup(entries: RootState, collection: string) {
export function selectEntriesGroup(entries: RootState, collection?: string) {
if (!collection) {
return {};
}
const group = entries.entries.group as Group | undefined;
return group?.[collection] || {};
}