From 12052d0c0c4884792d10545a07fbcbfce60ee15c Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 19 Sep 2024 23:41:03 +0200 Subject: [PATCH] Collection subcollection fixes, including data inference and getting updated on the view when created or deleted --- .../src/types/collection_inference.ts | 2 +- .../ui/collection_editor/CollectionEditorDialog.tsx | 2 +- .../ui/collection_editor/SubcollectionsEditTab.tsx | 13 +++++++++---- .../src/schema_inference/firestore.ts | 8 ++++++++ packages/firecms_cloud/src/FireCMSCloudApp.tsx | 2 -- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/collection_editor/src/types/collection_inference.ts b/packages/collection_editor/src/types/collection_inference.ts index 873dbc594..cd6ceeb29 100644 --- a/packages/collection_editor/src/types/collection_inference.ts +++ b/packages/collection_editor/src/types/collection_inference.ts @@ -1,3 +1,3 @@ import { EntityCollection } from "@firecms/core"; -export type CollectionInference = (path: string, collectionGroup: boolean, parentCollectionIds: string[]) => Promise | null>; +export type CollectionInference = (path: string, collectionGroup: boolean, parentCollectionPaths: string[]) => Promise | null>; diff --git a/packages/collection_editor/src/ui/collection_editor/CollectionEditorDialog.tsx b/packages/collection_editor/src/ui/collection_editor/CollectionEditorDialog.tsx index 814548ca7..94cc8b5f1 100644 --- a/packages/collection_editor/src/ui/collection_editor/CollectionEditorDialog.tsx +++ b/packages/collection_editor/src/ui/collection_editor/CollectionEditorDialog.tsx @@ -322,7 +322,7 @@ function CollectionEditorInternal>({ const doCollectionInference = (collection: PersistedCollection) => { if (!collectionInference) return undefined; - return collectionInference?.(collection.path, collection.collectionGroup ?? false, parentCollectionIds ?? []); + return collectionInference?.(collection.path, collection.collectionGroup ?? false, parentPaths ?? []); }; const inferCollectionFromData = async (newCollection: PersistedCollection) => { diff --git a/packages/collection_editor/src/ui/collection_editor/SubcollectionsEditTab.tsx b/packages/collection_editor/src/ui/collection_editor/SubcollectionsEditTab.tsx index b9d2e5846..7668e8322 100644 --- a/packages/collection_editor/src/ui/collection_editor/SubcollectionsEditTab.tsx +++ b/packages/collection_editor/src/ui/collection_editor/SubcollectionsEditTab.tsx @@ -61,7 +61,7 @@ export function SubcollectionsEditTab({ setFieldValue, } = useFormex(); - const subcollections = collection.subcollections ?? []; + const [subcollections, setSubcollections] = React.useState(collection.subcollections ?? []); const resolvedEntityViews = values.entityViews?.filter(e => typeof e === "string") .map(e => resolveEntityView(e, contextEntityViews)) .filter(Boolean) as EntityCustomView[] ?? []; @@ -218,8 +218,10 @@ export function SubcollectionsEditTab({ parentCollectionIds: [...(parentCollectionIds ?? []), collection.id] }; console.debug("Deleting subcollection", props) - configController.deleteCollection(props); - setSubcollectionToDelete(undefined); + configController.deleteCollection(props).then(() => { + setSubcollectionToDelete(undefined); + setSubcollections(subcollections?.filter(e => e.id !== subcollectionToDelete)) + }); }} onCancel={() => setSubcollectionToDelete(undefined)} title={<>Delete this subcollection?} @@ -247,7 +249,10 @@ export function SubcollectionsEditTab({ isNewCollection={false} {...currentDialog} getUser={getUser} - handleClose={() => { + handleClose={(updatedCollection) => { + if (updatedCollection && !subcollections.map(e => e.id).includes(updatedCollection.id)) { + setSubcollections([...subcollections, updatedCollection]); + } setCurrentDialog(undefined); }}/> diff --git a/packages/collection_editor_firebase/src/schema_inference/firestore.ts b/packages/collection_editor_firebase/src/schema_inference/firestore.ts index 93f17d9fa..9f584e4f4 100644 --- a/packages/collection_editor_firebase/src/schema_inference/firestore.ts +++ b/packages/collection_editor_firebase/src/schema_inference/firestore.ts @@ -8,9 +8,17 @@ export async function getDocuments( limitCount = 100 ): Promise { + console.debug("Getting documents", { + collectionPath, + isCollectionGroup, + parentPathSegments, + limitCount + }); + if (parentPathSegments && (parentPathSegments ?? [])?.length > 0) { const [thisSubPath, ...restSubpaths] = parentPathSegments; const childDocs = await getDocs(query(collection(firestore, thisSubPath), limit(5))); + console.debug("Got child documents", thisSubPath, childDocs.docs); return Promise.all(childDocs.docs .map((doc) => getDocuments( firestore, diff --git a/packages/firecms_cloud/src/FireCMSCloudApp.tsx b/packages/firecms_cloud/src/FireCMSCloudApp.tsx index 320def69f..310d7aae9 100644 --- a/packages/firecms_cloud/src/FireCMSCloudApp.tsx +++ b/packages/firecms_cloud/src/FireCMSCloudApp.tsx @@ -360,10 +360,8 @@ export function FireCMSClientWithController({ }); const permissions: PermissionsBuilder = useCallback(({ - pathSegments, collection, user, - entity }) => { return resolveUserRolePermissions({ collection,