Skip to content

Commit

Permalink
Collection subcollection fixes, including data inference and getting …
Browse files Browse the repository at this point in the history
…updated on the view when created or deleted
  • Loading branch information
fgatti675 committed Sep 19, 2024
1 parent a1c600a commit 12052d0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { EntityCollection } from "@firecms/core";

export type CollectionInference = (path: string, collectionGroup: boolean, parentCollectionIds: string[]) => Promise<Partial<EntityCollection> | null>;
export type CollectionInference = (path: string, collectionGroup: boolean, parentCollectionPaths: string[]) => Promise<Partial<EntityCollection> | null>;
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function CollectionEditorInternal<M extends Record<string, any>>({

const doCollectionInference = (collection: PersistedCollection<any>) => {
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<M>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function SubcollectionsEditTab({
setFieldValue,
} = useFormex<EntityCollection>();

const subcollections = collection.subcollections ?? [];
const [subcollections, setSubcollections] = React.useState<EntityCollection[]>(collection.subcollections ?? []);
const resolvedEntityViews = values.entityViews?.filter(e => typeof e === "string")
.map(e => resolveEntityView(e, contextEntityViews))
.filter(Boolean) as EntityCustomView[] ?? [];
Expand Down Expand Up @@ -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?</>}
Expand Down Expand Up @@ -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);
}}/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ export async function getDocuments(
limitCount = 100
): Promise<DocumentSnapshot[]> {

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,
Expand Down
2 changes: 0 additions & 2 deletions packages/firecms_cloud/src/FireCMSCloudApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,8 @@ export function FireCMSClientWithController({
});

const permissions: PermissionsBuilder<PersistedCollection, FireCMSCloudUserWithRoles> = useCallback(({
pathSegments,
collection,
user,
entity
}) => {
return resolveUserRolePermissions<FireCMSCloudUserWithRoles>({
collection,
Expand Down

0 comments on commit 12052d0

Please sign in to comment.