Skip to content

Commit

Permalink
made frontend get data from firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
Aromiii committed Oct 9, 2022
1 parent b7e616c commit 1400344
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
1 change: 0 additions & 1 deletion components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const NavBar = () => {
<img src={localStorage.getItem("profilePic")!} className="rounded-3xl"/>
</Link>
)}

</li>
</ul>
)
Expand Down
18 changes: 11 additions & 7 deletions config/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
// Import the functions you need from the SDKs you need
import { getAuth, GoogleAuthProvider, signInWithPopup, onAuthStateChanged } from 'firebase/auth';
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from "firebase/firestore";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyAjlx36rihQ_vS1KpU2WeCjTArR1Y_rh_M",
authDomain: "food-helper-a73b7.firebaseapp.com",
projectId: "food-helper-a73b7",
storageBucket: "food-helper-a73b7.appspot.com",
messagingSenderId: "404309027827",
appId: "1:404309027827:web:47b25e11243eb625ed8086",
measurementId: "G-9FCCSBNYT0"
apiKey: "AIzaSyDQlWrPV7vL1gPY3cjLIIt-1UUJaANGrEM",
authDomain: "food-helper-63fd0.firebaseapp.com",
projectId: "food-helper-63fd0",
storageBucket: "food-helper-63fd0.appspot.com",
messagingSenderId: "833087637499",
appId: "1:833087637499:web:b588bc7700c47a35266d5e"
};


// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app)

// Initialize Firestore
export const db = getFirestore()

//Initialize provider
const provider = new GoogleAuthProvider()

Expand Down
1 change: 1 addition & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {NextPage} from 'next'
import Head from 'next/head'
import React from "react";


const Home: NextPage = () => {
return (
<div className="h-[calc(100vh-4rem)]">
Expand Down
6 changes: 3 additions & 3 deletions pages/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {getAuth, signOut} from "firebase/auth";
import {router} from "next/client";


const ShoppingList = () => {
const Profile = () => {

const auth = getAuth();
const signOutHandler = () => {
signOut(auth).then(() => {
router.push("/")
router.replace("/")
}).catch((error) => {
console.log(error)
});
Expand All @@ -26,4 +26,4 @@ const ShoppingList = () => {

)
}
export default ShoppingList;
export default Profile;
2 changes: 2 additions & 0 deletions pages/recipes/[recipeId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Head from "next/head";
import {useRouter} from "next/router";
import {useEffect, useState} from "react";
import axios from "axios";
import {getAuth, onAuthStateChanged} from "firebase/auth";


const RecipeSiteBody = (props: { data: { image: string; name: string; recipe: string; desc: string } }) => {
return <>
Expand Down
30 changes: 20 additions & 10 deletions pages/recipes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import Head from "next/head";
import React, {useEffect, useState} from "react";
import axios from "axios";
import Link from "next/link";
import { collection, getDocs } from "@firebase/firestore";
import { db } from '../../config/firebase'
import {any} from "prop-types";

const RecipeContainer = (props: { data: { image: string; name: string; desc: string; id: string } }) => {

return (
<Link href={"/recipes/" + props.data.id}>
<div className="recipeContainer">
Expand All @@ -19,17 +23,23 @@ const RecipeContainer = (props: { data: { image: string; name: string; desc: str


const Recipes = () => {
const [data, setData] = useState([""])
const [recipes, setRecipes] = useState([])

const getRecipes = () => {
//TODO
axios.get("/api/recipes/---placeholder---")
.then((res) => {
setData(res.data);
})
}
// Collection refs
const colRef = collection(db, 'recipes')

useEffect(() => getRecipes(), [])
// Get collection data
useEffect(() => {
getDocs(colRef)
.then((snapshot) => {
snapshot.docs.forEach((doc) => {
setRecipes(recipes => [...recipes, { ...doc.data(), id: doc.id }])
})
console.log(recipes)
}).catch(error => {
console.log(error.message)
})
}, [])


return (
Expand All @@ -40,7 +50,7 @@ const Recipes = () => {
</Head>
{
// @ts-ignore
data.map((recipe) => <RecipeContainer data={recipe} key={recipe.id}/>)
recipes.map((recipe) => <RecipeContainer data={recipe} key={recipe.id}/>)
}
</div>

Expand Down

0 comments on commit 1400344

Please sign in to comment.