Skip to content

Commit a4f9079

Browse files
committed
Fix for multiple collections with same path not resolving correctly
1 parent cc784d1 commit a4f9079

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

examples/example_pro/src/FirestoreApp/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { firebaseConfig, secondaryFirebaseConfig } from "../firebase_config";
4949
import { ExampleCMSView } from "./views/ExampleCMSView";
5050
import { testCollection } from "./collections/test_collection";
5151
import { usersCollection } from "./collections/users_collection";
52-
import { localeCollectionGroup, productsCollection } from "./collections/products_collection";
52+
import { localeCollectionGroup, pppCollection, productsCollection } from "./collections/products_collection";
5353
import { blogCollection } from "./collections/blog_collection";
5454
import { showcaseCollection } from "./collections/showcase_collection";
5555

@@ -233,6 +233,7 @@ export function App() {
233233
showcaseCollection,
234234
cryptoCollection,
235235
localeCollectionGroup,
236+
pppCollection,
236237
// carsCollection(secondaryFirestoreDelegate)
237238
];
238239
if (process.env.NODE_ENV !== "production") {

examples/example_pro/src/FirestoreApp/collections/products_collection.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,39 @@ export const productsSimpleCollection = buildCollection<any>({
374374
}
375375

376376
});
377+
378+
379+
export const pppCollection = buildCollection<Product>({
380+
path: "products",
381+
id: "ppp",
382+
callbacks: productCallbacks,
383+
name: "PPP",
384+
group: "Demo collections",
385+
icon: "shopping_cart",
386+
openEntityMode: "full_screen",
387+
description: "List of the products currently sold in our shop",
388+
textSearchEnabled: true,
389+
permissions: ({ authController }) => ({
390+
edit: true,
391+
create: true,
392+
delete: true
393+
}),
394+
Actions: [SampleCollectionActions],
395+
subcollections: [localeCollection],
396+
// defaultSelectedView: "sample_custom_view",
397+
additionalFields: [productAdditionalField],
398+
// @ts-ignore
399+
properties: {
400+
name: {
401+
dataType: "string",
402+
name: "Name",
403+
description: "Name of this product",
404+
clearable: true,
405+
validation: {
406+
required: true
407+
}
408+
}
409+
}
410+
411+
});
412+

packages/firecms_core/src/hooks/useBuildNavigationController.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ export function useBuildNavigationController<EC extends EntityCollection, USER e
291291

292292
}, [userConfigPersistence]);
293293

294+
const getCollectionById = useCallback((id: string): EC | undefined => {
295+
const collections = collectionsRef.current;
296+
if (collections === undefined)
297+
throw Error("getCollectionById: Collections have not been initialised yet");
298+
const collection: EntityCollection | undefined = collections.find(c => c.id === id);
299+
if (!collection)
300+
return undefined;
301+
return collection as EC;
302+
}, []);
303+
294304
const getCollectionFromPaths = useCallback(<EC extends EntityCollection>(pathSegments: string[]): EC | undefined => {
295305

296306
const collections = collectionsRef.current;
@@ -398,6 +408,7 @@ export function useBuildNavigationController<EC extends EntityCollection, USER e
398408
baseCollectionPath,
399409
initialised,
400410
getCollection,
411+
getCollectionById,
401412
getCollectionFromPaths,
402413
getCollectionFromIds,
403414
isUrlCollectionPath,

packages/firecms_core/src/routes/FireCMSRoute.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useBreadcrumbsController } from "../hooks/useBreadcrumbsController";
1313
import { toArray } from "../util/arrays";
1414
import { EntityCollectionView, NotFoundPage } from "../components";
1515
import { UnsavedChangesDialog } from "../components/UnsavedChangesDialog";
16+
import { EntityCollection } from "../types";
1617

1718
export function FireCMSRoute() {
1819

@@ -68,7 +69,10 @@ export function FireCMSRoute() {
6869
}
6970

7071
if (navigationEntries.length === 1 && navigationEntries[0].type === "collection") {
71-
const collection = navigation.getCollection(navigationEntries[0].path);
72+
let collection: EntityCollection<any> | undefined;
73+
collection = navigation.getCollectionById(navigationEntries[0].id);
74+
if (!collection)
75+
collection = navigation.getCollection(navigationEntries[0].path);
7276
if (!collection)
7377
return null;
7478
return <EntityCollectionView
@@ -84,7 +88,11 @@ export function FireCMSRoute() {
8488
if (isSidePanel) {
8589
const lastCollectionEntry = navigationEntries.findLast((entry) => entry.type === "collection");
8690
if (lastCollectionEntry) {
87-
const collection = navigation.getCollection(lastCollectionEntry.path);
91+
let collection: EntityCollection<any> | undefined;
92+
const firstEntry = navigationEntries[0] as NavigationViewCollectionInternal<any>;
93+
collection = navigation.getCollectionById(firstEntry.id);
94+
if (!collection)
95+
collection = navigation.getCollection(firstEntry.path);
8896
if (!collection)
8997
return null;
9098
return <EntityCollectionView

packages/firecms_core/src/types/navigation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export type NavigationController<EC extends EntityCollection = EntityCollection<
5959
*/
6060
getCollection: (pathOrId: string, includeUserOverride?: boolean) => EC | undefined;
6161

62+
/**
63+
* Get the top level collection configuration for a given id
64+
*/
65+
getCollectionById: (id: string) => EC | undefined;
66+
6267
/**
6368
* Get the collection configuration from its parent ids.
6469
*/

packages/firecms_core/src/util/navigation_from_path.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ export function getNavigationEntriesFromPath(props: {
5151
for (let i = 0; i < subpathCombinations.length; i++) {
5252
const subpathCombination = subpathCombinations[i];
5353

54-
const collection: EntityCollection<any> | undefined = collections && collections.find((entry) => entry.id === subpathCombination || entry.path === subpathCombination);
54+
let collection: EntityCollection<any> | undefined;
55+
collection = collections && collections.find((entry) => entry.id === subpathCombination);
56+
if (!collection) {
57+
collection = collections && collections.find((entry) => entry.path === subpathCombination);
58+
}
5559

5660
if (collection) {
5761
const pathOrAlias = collection.id ?? collection.path;

0 commit comments

Comments
 (0)