firebase (#8)

* use typescript

* fix types
This commit is contained in:
Ruidy 2021-04-03 19:17:38 +02:00 committed by GitHub
parent cb101b22ec
commit 7cde13f071
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 119 deletions

View file

@ -5,7 +5,7 @@ import { App } from "./App";
import * as serviceWorker from "./serviceWorker";
import { Auth0Provider } from "./utils/auth0-spa";
import history from "./utils/history";
import Firebase, { FirebaseContext } from "./services/Firebase";
import { FirebaseContext } from "./services/Firebase";
const onRedirectCallBack = (appState) => {
history.push(

View file

@ -1,10 +0,0 @@
{
"API_KEY": "AIzaSyCAvCuQeoPjni7HRYMHKNu6mEhUdL4C9oQ",
"AUTH_DOMAIN": "devprojects-4749c.firebaseapp.com",
"DB_URL": "https://devprojects-4749c.firebaseio.com",
"PROJECT_ID": "devprojects-4749c",
"STORAGE_BUCKET": "devprojects-4749c.appspot.com",
"MSG_SENDER_ID": "944434165278",
"APP_ID": "1:944434165278:web:51ce06178b813d9c9f200f",
"MEASUREMENT_ID": "G-BZFZL8P4TT"
}

View file

@ -1,101 +0,0 @@
import app from "firebase/app";
import "firebase/firestore";
import config from "./config.json";
const CONFIG = {
apiKey: config.API_KEY,
authDomain: config.AUTH_DOMAIN,
databaseURL: config.DB_URL,
projectId: config.PROJECT_ID,
storageBucket: config.STORAGE_BUCKET,
messagingSenderId: config.MSG_SENDER_ID,
appId: config.APP_ID,
measurementId: config.MEASUREMENT_ID,
// apiKey: process.env.API_KEY ,
// authDomain: process.env.AUTH_DOMAIN,
// databaseURL: process.env.DB_URL,
// projectId: process.env.PROJECT_ID,
// storageBucket: process.env.STORAGE_BUCKET,
// messagingSenderId: process.env.MSG_SENDER_ID,
// appId: process.env.APP_ID,
// measurementId: process.env.MEASUREMENT_ID,
};
const FAVS = "favs";
/**
* Firebase initializes the Application and provides method to interact with
* Firebase services as auth and firestore.
*/
export default class Firebase {
constructor() {
app.initializeApp(CONFIG);
this.db = app.firestore();
this.collection = this.db.collection("mealPlannerUsers");
}
/**
* Get infos for user 'email'.
*/
getByEmail = async (email) => {
const infos = await this.collection
.where("email", "==", email)
.limit(1)
.get();
const favs = await this.getFavsByEmail(email);
return { infos, favs };
};
/**
* Get user's favourite recipes
* */
getFavsByEmail = async (email) => {
let favs = [];
const query = await this.collection
.doc(email)
.collection(FAVS)
.where("isFav", "==", true)
// .orderBy("timestamp", "desc")
.limit(10)
.get();
query.forEach((doc) => favs.push(doc.data()));
return favs;
};
isFav = async (email, idMeal) => {
const query = await this.collection
.doc(email)
.collection(FAVS)
.doc(idMeal)
.get();
const obj = query.data();
return obj && !!obj.isFav;
// .where("isFav", "==", true);
// return !!query;
};
/**
* Create or update favourite status for an authenticated user.
*/
addToFavs = async (email, idMeal, strMeal, strMealThumb, isFav) => {
this.collection
.doc(email)
.collection(FAVS)
.doc(idMeal)
.set({
email,
idMeal,
strMeal,
strMealThumb,
isFav,
// timestamp: app.FieldValue.serverTimestamp(),
})
// .then(() => console.log("Fav object created.", isFav))
.catch((err) => console.error("Error adding favs to database", err));
};
}

View file

@ -0,0 +1,96 @@
import app from "firebase/app";
import "firebase/firestore";
const CONFIG = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DB_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MSG_SENDER_ID,
appId: process.env.REACT_APP_APP_ID,
measurementId: process.env.REACT_APP_MEASUREMENT_ID,
};
const FAVS = "favs";
/**
* Firebase initializes the Application and provides method to interact with
* Firebase services as auth and firestore.
*/
export default class Firebase {
#db: any;
#collection: any;
constructor() {
app.initializeApp(CONFIG);
this.#db = app.firestore();
this.#collection = this.#db.collection("mealPlannerUsers");
}
/**
* Get infos for user 'email'.
*/
getByEmail = async (email: string) => {
const infos = await this.#collection
.where("email", "==", email)
.limit(1)
.get();
const favs = await this.getFavsByEmail(email);
return { infos, favs };
};
/**
* Get user's favourite recipes
* */
getFavsByEmail = async (email: string) => {
let favs = [] as any[];
const query = await this.#collection
.doc(email)
.collection(FAVS)
.where("isFav", "==", true)
.limit(10)
.get();
query.forEach((doc: any) => favs.push(doc.data()));
return favs;
};
isFav = async (email: string, idMeal: string) => {
const query = await this.#collection
.doc(email)
.collection(FAVS)
.doc(idMeal)
.get();
const obj = query.data();
return obj?.isFav;
};
/**
* Create or update favourite status for an authenticated user.
*/
addToFavs = async (
email: string,
idMeal: string,
strMeal: string,
strMealThumb: string,
isFav: boolean
) => {
this.#collection
.doc(email)
.collection(FAVS)
.doc(idMeal)
.set({
email,
idMeal,
strMeal,
strMealThumb,
isFav,
})
.catch((err: any) => console.error("Error adding favs to database", err));
};
}

View file

@ -1,4 +1,4 @@
// This file centralize all Firebase related exports
// This file centralizes all Firebase related exports
import Firebase from "./firebase";
import FirebaseContext, { useFirebase } from "./context";

View file

@ -1,5 +0,0 @@
/* This is a dummy auth0 config file. Use https://auth0.com/ to create your own set of keys */
{
"domain": "{DOMAIN}",
"clientId": "{CLIENT_ID}"
}

View file

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es6",
"lib": [
"dom",
"dom.iterable",