UNPKG

firebase-admin-panel

Version:

A customizable React admin panel for managing Firebase collections.

99 lines (98 loc) 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Timestamp", { enumerable: true, get: function () { return _firestore.Timestamp; } }); exports.useDocuments = exports.useCollections = exports.updateDocument = exports.getCollectionDocuments = exports.deleteDocument = exports.addDocument = void 0; var _react = require("react"); var _firebase = require("../firebase"); var _firestore = require("firebase/firestore"); const useCollections = () => { const [collections, setCollections] = (0, _react.useState)([]); const [schemas, setSchemas] = (0, _react.useState)({}); const db = (0, _firebase.getFirestoreDb)(); (0, _react.useEffect)(() => { const fetchCollectionsAndSchemas = async () => { try { const collectionNamesDocRef = (0, _firestore.doc)(db, "metadata", "collectionNames"); const collectionNamesDocSnap = await (0, _firestore.getDoc)(collectionNamesDocRef); if (collectionNamesDocSnap.exists()) { const data = collectionNamesDocSnap.data(); setCollections(data.collections); const schemas = {}; for (const collectionName of data.collections) { const schemaDocRef = (0, _firestore.doc)((0, _firestore.collection)(db, "metadata", "schemas"), collectionName); const schemaDocSnap = await (0, _firestore.getDoc)(schemaDocRef); if (schemaDocSnap.exists()) { schemas[collectionName] = schemaDocSnap.data(); } } setSchemas(schemas); } else { console.log("No such document!"); } } catch (error) { console.error("Error fetching collections and schemas: ", error); } }; fetchCollectionsAndSchemas(); }, [db]); return { collections, schemas }; }; exports.useCollections = useCollections; const useDocuments = collectionName => { const [documents, setDocuments] = (0, _react.useState)([]); const db = (0, _firebase.getFirestoreDb)(); (0, _react.useEffect)(() => { if (collectionName) { const q = (0, _firestore.query)((0, _firestore.collection)(db, collectionName)); const unsubscribe = (0, _firestore.onSnapshot)(q, snapshot => { const docs = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); setDocuments(docs); }); return () => unsubscribe(); } }, [collectionName, db]); return { documents }; }; exports.useDocuments = useDocuments; const addDocument = async (collectionName, data) => { const db = (0, _firebase.getFirestoreDb)(); return await (0, _firestore.addDoc)((0, _firestore.collection)(db, collectionName), data); }; exports.addDocument = addDocument; const updateDocument = async (collectionName, docId, data) => { const db = (0, _firebase.getFirestoreDb)(); const docRef = (0, _firestore.doc)(db, collectionName, docId); return await (0, _firestore.updateDoc)(docRef, data); }; exports.updateDocument = updateDocument; const deleteDocument = async (collectionName, docId) => { const db = (0, _firebase.getFirestoreDb)(); const docRef = (0, _firestore.doc)(db, collectionName, docId); return await (0, _firestore.deleteDoc)(docRef); }; exports.deleteDocument = deleteDocument; const getCollectionDocuments = async collectionName => { const db = (0, _firebase.getFirestoreDb)(); const q = (0, _firestore.query)((0, _firestore.collection)(db, collectionName)); const querySnapshot = await (0, _firestore.getDocs)(q); return querySnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); }; exports.getCollectionDocuments = getCollectionDocuments;