Skip to content

Commit d5aaf82

Browse files
committed
pushing our data into a firebase collection
1 parent 2d61851 commit d5aaf82

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/App.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ import { selectCurrentUser } from "./redux/user/user.selectors";
1818
import { createStructuredSelector } from "reselect";
1919

2020

21+
22+
2123
class App extends React.Component{
2224
unsubscribeFromAuth = null
23-
componentDidMount() {
25+
26+
componentDidMount() {
2427
const { setCurrentUser } = this.props;
28+
29+
2530
this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
2631
createUserProfileDocument(userAuth)
2732
if (userAuth){
@@ -35,9 +40,10 @@ import { createStructuredSelector } from "reselect";
3540

3641
})
3742
}
38-
else {
39-
setCurrentUser(userAuth)
40-
}
43+
44+
setCurrentUser(userAuth);
45+
46+
4147
})
4248
this.unsubscribeFromAuth = auth.onAuthStateChanged(user => {
4349
this.setState({ currentUser:user});

src/firebase/firebase.utils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export const createUserProfileDocument = async (userAuth, additionalData) => {
1818
//console.log(firestore.doc('users/128rigolux'))4firebase.firestore().enablePersistence()
1919

2020
const userRef = firestore.doc(`users/${userAuth.uid}`)
21+
const collectionRef = firestore.collection('users')
2122
const snapShot = await userRef.get()
2223

23-
console.log(snapShot)
24+
2425

2526
if (!snapShot.exists){
2627
const { displayName, email } = userAuth;
@@ -42,6 +43,20 @@ export const createUserProfileDocument = async (userAuth, additionalData) => {
4243
return userRef;
4344
}
4445

46+
export const addCollectionsAndDocuments = async (collectionKey, objectsToAdd) => {
47+
48+
const collectionRef = firestore.collection(collectionKey)
49+
50+
const batch = firestore.batch();
51+
objectsToAdd.forEach(obj => {
52+
const newDocRef = collectionRef.doc()
53+
batch.set(newDocRef, obj)
54+
})
55+
56+
return await batch.commit()
57+
}
58+
59+
4560
firebase.initializeApp(firebaseConfig);
4661
export const auth = firebase.auth()
4762
export const firestore = firebase.firestore()

0 commit comments

Comments
 (0)