Skip to content

Commit

Permalink
migrating to firebase 10 from 8
Browse files Browse the repository at this point in the history
  • Loading branch information
samilabud committed Mar 5, 2024
1 parent ce62c53 commit 5d25670
Show file tree
Hide file tree
Showing 8 changed files with 16,490 additions and 16,589 deletions.
32,614 changes: 16,245 additions & 16,369 deletions client/package-lock.json

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "0.21.1",
"firebase": "^8.0.2",
"axios": "^1.6.7",
"firebase": "^10.8.1",
"lodash.memoize": "^4.1.2",
"node-sass": "^9.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.0",
"react-scripts": "5.0.1",
"react-stripe-checkout": "^2.6.3",
"redux": "^4.0.5",
"redux-logger": "^3.0.6",
"redux-persist": "^6.0.0",
"redux-saga": "^1.1.3",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"sass": "^1.68.0",
"styled-components": "^5.2.3",
"web-vitals": "^0.2.4",
"workbox-background-sync": "^5.1.3",
Expand All @@ -40,8 +40,8 @@
"workbox-streams": "^5.1.3"
},
"scripts": {
"start": "react-scripts --openssl-legacy-provider --max-old-space-size=400 start",
"build": "react-scripts --openssl-legacy-provider build",
"start": "react-scripts --max-old-space-size=400 start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Expand All @@ -62,5 +62,11 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"sass": "^1.71.1",
"sass-loader": "^14.1.1",
"webpack": "^5.90.3"
}
}
88 changes: 0 additions & 88 deletions client/src/fireabase/firebase.utils.js

This file was deleted.

88 changes: 88 additions & 0 deletions client/src/firebase/firebase.utils.js
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;
1 change: 0 additions & 1 deletion client/src/pages/homepage/homepage.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Directory from '../../components/directory/directory.component';
import { HomePageContainer } from './homepage.styles'

const HomePage = () =>{
//throw Error;
return (
<HomePageContainer>
<Directory/>
Expand Down
52 changes: 29 additions & 23 deletions client/src/redux/shop/shop.actions.js
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)));
};
};
43 changes: 22 additions & 21 deletions client/src/redux/shop/shop.saga.js
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)]);
}

Loading

0 comments on commit 5d25670

Please sign in to comment.