Ability to create collections from another collection and list them on the Home page #438
Answered
by
fgatti675
kmatsuleva
asked this question in
Q&A
Answered by
fgatti675
Dec 1, 2022
Replies: 3 comments
|
Hi @kmatsuleva import React from "react";
import {
buildCollection,
EntityCollectionsBuilder,
FirebaseCMSApp
} from "@camberi/firecms";
import "typeface-rubik";
import "@fontsource/ibm-plex-mono";
// TODO: Replace with your config
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
type Unit = {
name: string;
price: number;
}
const unitsCollection = buildCollection<Unit>({
name: "Units",
singularName: "Unit",
group: "Main",
path: "units",
customId: true,
callbacks: {
onSaveSuccess: ({ context }) => {
context.navigation.refreshNavigation();
},
onDelete: ({ context }) => {
context.navigation.refreshNavigation();
}
},
properties: {
name: {
name: "Name",
validation: { required: true },
dataType: "string"
},
price: {
name: "Price",
validation: {
required: true,
requiredMessage: "You must set a price between 0 and 1000",
min: 0,
max: 1000
},
description: "Price with range validation",
dataType: "number"
}
}
});
export default function App() {
const collectionBuilder: EntityCollectionsBuilder = async ({ dataSource }) => {
const units = await dataSource.fetchCollection<Unit>({
path: "units",
collection: unitsCollection
});
const lessonCollections = units.map(unit => buildCollection({
name: unit.values.name,
path: `units/${unit.id}/lessons`,
group: "Units",
properties: {
name: {
name: "Name",
dataType: "string"
}
}
}));
return [
unitsCollection,
...lessonCollections
]
};
return <FirebaseCMSApp
name={"My learning app"}
collections={collectionBuilder}
firebaseConfig={firebaseConfig}
/>;
} |
0 replies
Answer selected by
kmatsuleva
|
Full explanation in https://firecms.co/docs/recipes/documents_as_subcollections ;) |
0 replies
|
This helped me a lot! Thank you very much @fgatti675 ! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment






Hi @kmatsuleva
I have tried implementing this on my side and realised there were a couple of small pieces missing.
You can now access the datasource in the
EntityCollectionBuilder, that will allow you to read data from Firestore upon creation of the CMS. I have also added a function that allows you to refresh the navigation data on demand.I am writing an article on how to put all this together, but in the meantime, you can use this code as a template.
Remember to update to the latest
2.0.0-alpha.47version!