-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
16,490 additions
and
16,589 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { initializeApp } from "firebase/app"; | ||
import { getAuth, GoogleAuthProvider, onAuthStateChanged } from "firebase/auth"; | ||
import { getFirestore, doc, collection, getDocs } from "firebase/firestore"; | ||
|
||
const config = { | ||
apiKey: "AIzaSyAb9UlJHKZQkqFaA3vJ25mgqsIOIl1CEQY", | ||
authDomain: "ecommerceztom.firebaseapp.com", | ||
databaseURL: "https://ecommerceztom.firebaseio.com", | ||
projectId: "ecommerceztom", | ||
name: "EcommerceZtoM", | ||
storageBucket: "ecommerceztom.appspot.com", | ||
messagingSenderId: "745149943574", | ||
appId: "1:745149943574:web:5e4d9e56554d82c35f8a37", | ||
}; | ||
|
||
const firebase = initializeApp(config); | ||
export const firestore = getFirestore(firebase); | ||
// export const firestore = firebase.firestore(); | ||
|
||
export const createUserProfileDocument = async (userAuth, additionalData) => { | ||
if (!userAuth) { | ||
return; | ||
} | ||
const userRef = doc(`users/${userAuth.uid}`); //Query Reference | ||
const snapShot = await userRef.get(); //Document Reference, getting snapshot (Document Snapshot) | ||
if (!snapShot.exists) { | ||
//Return true if document exists | ||
const { displayName, email } = userAuth; | ||
const createdAt = new Date(); | ||
try { | ||
await userRef.set({ | ||
displayName, | ||
email, | ||
createdAt, | ||
...additionalData, | ||
}); | ||
} catch (ex) { | ||
console.log("error", ex.message); | ||
} | ||
} | ||
return userRef; | ||
}; | ||
|
||
export const addCollectionAndItems = async (collectionKey, objectsToAdd) => { | ||
const collectionRef = await getDocs(collection(firestore, collectionKey)); | ||
const batchFirestore = firestore.batch(); | ||
objectsToAdd.forEach((object) => { | ||
const newDocRef = collectionRef.doc(); | ||
batchFirestore.set(newDocRef, object); | ||
}); | ||
return await batchFirestore.commit(); | ||
}; | ||
|
||
export const convertCollectionsSnapshotToMap = (collections) => { | ||
const transformedCollection = collections.docs.map((doc) => { | ||
const { title, items } = doc.data(); | ||
return { | ||
routeName: encodeURI(title.toLowerCase()), | ||
id: doc.id, | ||
title, | ||
items, | ||
}; | ||
}); | ||
return transformedCollection.reduce((accumulator, collection) => { | ||
accumulator[collection.title.toLowerCase()] = collection; | ||
return accumulator; | ||
}, {}); | ||
}; | ||
export const auth = getAuth(firebase); | ||
|
||
export const getCurrentUser = () => { | ||
return new Promise((resolve, reject) => { | ||
const unsubscribe = onAuthStateChanged( | ||
auth, | ||
(userAuth) => { | ||
unsubscribe(); | ||
resolve(userAuth); | ||
}, | ||
reject | ||
); | ||
}); | ||
}; | ||
|
||
export const googleProvider = new GoogleAuthProvider(); | ||
googleProvider.setCustomParameters({ promp: "select_account" }); | ||
export const signInWithGoogle = () => auth.signInWithPopup(googleProvider); | ||
|
||
export default firebase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
import ShopActionTypes from './shop.type'; | ||
import {firestore, convertCollectionsSnapshotToMap} from '../../fireabase/firebase.utils'; | ||
import ShopActionTypes from "./shop.type"; | ||
import { | ||
convertCollectionsSnapshotToMap, | ||
firestore, | ||
} from "../../firebase/firebase.utils"; | ||
import { collection, getDocs } from "firebase/firestore"; | ||
|
||
export const fetchCollectionsStart = () => ({ | ||
type: ShopActionTypes.FETCH_COLLECTIONS_START | ||
type: ShopActionTypes.FETCH_COLLECTIONS_START, | ||
}); | ||
|
||
export const fetchCollectionsSuccess = collectionMap => ({ | ||
type: ShopActionTypes.FETCH_COLLECTIONS_SUCCESS, | ||
payload: collectionMap | ||
}) | ||
export const fetchCollectionsFailure = errorMessage => ({ | ||
type: ShopActionTypes.FETCH_COLLECTIONS_FAILURE, | ||
payload: errorMessage | ||
}) | ||
export const fetchCollectionsSuccess = (collectionMap) => ({ | ||
type: ShopActionTypes.FETCH_COLLECTIONS_SUCCESS, | ||
payload: collectionMap, | ||
}); | ||
export const fetchCollectionsFailure = (errorMessage) => ({ | ||
type: ShopActionTypes.FETCH_COLLECTIONS_FAILURE, | ||
payload: errorMessage, | ||
}); | ||
|
||
export const fetchCollectionsStartAsync = () => { | ||
return dispatch => { | ||
const collectionRef = firestore.collection('collections'); | ||
dispatch(fetchCollectionsStart()); | ||
export const fetchCollectionsStartAsync = () => { | ||
return (dispatch) => { | ||
const collectionRef = getDocs(collection(firestore, "collections")); | ||
dispatch(fetchCollectionsStart()); | ||
|
||
collectionRef.get().then( | ||
snapshot=>{ | ||
const collectionsMap = convertCollectionsSnapshotToMap(snapshot); | ||
dispatch(fetchCollectionsSuccess(collectionsMap)); | ||
} | ||
).catch(error=> dispatch(fetchCollectionsFailure(error))); | ||
} | ||
} | ||
collectionRef | ||
.get() | ||
.then((snapshot) => { | ||
// const collectionsMap = convertCollectionsSnapshotToMap(snapshot); | ||
const collectionsMap = collectionRef; | ||
dispatch(fetchCollectionsSuccess(collectionsMap)); | ||
}) | ||
.catch((error) => dispatch(fetchCollectionsFailure(error))); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
import { takeLatest, call, put, all} from 'redux-saga/effects'; | ||
import ShopActionTypes from './shop.type'; | ||
|
||
import {firestore, convertCollectionsSnapshotToMap} from '../../fireabase/firebase.utils'; | ||
import { takeLatest, call, put, all } from "redux-saga/effects"; | ||
import ShopActionTypes from "./shop.type"; | ||
import { collection, getDocs } from "firebase/firestore"; | ||
|
||
import { | ||
fetchCollectionsSuccess, | ||
fetchCollectionsFailure | ||
} from './shop.actions'; | ||
firestore, | ||
convertCollectionsSnapshotToMap, | ||
} from "../../firebase/firebase.utils"; | ||
|
||
import { | ||
fetchCollectionsSuccess, | ||
fetchCollectionsFailure, | ||
} from "./shop.actions"; | ||
|
||
export function* fetchCollections() { | ||
try { | ||
const collectionRef = firestore.collection('collections'); | ||
const snapshot = yield collectionRef.get(); | ||
const collectionsMap = yield call( | ||
convertCollectionsSnapshotToMap, | ||
snapshot | ||
); | ||
yield put(fetchCollectionsSuccess(collectionsMap)); | ||
} catch (error) { | ||
yield put(fetchCollectionsFailure(error.message)); | ||
} | ||
try { | ||
const snapshot = yield getDocs(collection(firestore, "collections")); | ||
const collectionsMap = yield call( | ||
convertCollectionsSnapshotToMap, | ||
snapshot | ||
); | ||
yield put(fetchCollectionsSuccess(collectionsMap)); | ||
} catch (error) { | ||
yield put(fetchCollectionsFailure(error.message)); | ||
} | ||
} | ||
|
||
export function* fetchCollectionsStart() { | ||
yield takeLatest(ShopActionTypes.FETCH_COLLECTIONS_START, fetchCollections); | ||
yield takeLatest(ShopActionTypes.FETCH_COLLECTIONS_START, fetchCollections); | ||
} | ||
|
||
export function* shopSagas(){ | ||
export function* shopSagas() { | ||
yield all([call(fetchCollectionsStart)]); | ||
} | ||
|
Oops, something went wrong.