-
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.
Merge pull request #17 from Aromiii/newDatamodel
New datamodel
- Loading branch information
Showing
7 changed files
with
95 additions
and
67 deletions.
There are no files selected for viewing
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
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
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,55 +1,70 @@ | ||
import Head from "next/head"; | ||
import React, {useEffect, useState} from "react"; | ||
import React, {useState} from "react"; | ||
import Link from "next/link"; | ||
import {collection, DocumentData, getDocs} from "@firebase/firestore"; | ||
import {db} from '../../config/firebase' | ||
import {db, auth} from '../../config/firebase' | ||
import {onAuthStateChanged} from "firebase/auth"; | ||
|
||
const RecipeContainer = (props: { data: DocumentData }) => { | ||
|
||
return ( | ||
<Link href={"/recipes/recipe/" + props.data.id}> | ||
<div className="recipeContainer"> | ||
<img src={props.data.image} className="recipeContainerImage"/> | ||
<div className="p-2"> | ||
<h1>{props.data.name}</h1> | ||
<p>{props.data.desc}</p> | ||
</div> | ||
<Link href={"/recipes/recipe/" + props.data.id}> | ||
<div className="bg-gray-500 m-2 rounded-2xl h-96 object-cover overflow-hidden"> | ||
<img src={props.data.image} className="rounded-2xl max-h-[60%] w-screen object-cover"/> | ||
<div className="p-2"> | ||
<h1>{props.data.name}</h1> | ||
<p>{props.data.desc}</p> | ||
</div> | ||
</Link> | ||
</div> | ||
</Link> | ||
); | ||
} | ||
|
||
const Recipes = () => { | ||
//TODO migrate to getServerSideProps to make flash go away | ||
const [recipes, setRecipes] = useState<DocumentData[]>([]) | ||
|
||
// Collection refs | ||
const colRef = collection(db, 'recipes') | ||
onAuthStateChanged(auth, (user) => { | ||
if (user) { | ||
//Collection reference to recipes that current user owns | ||
const colRef = collection(db, 'users', user.uid, 'recipes') | ||
|
||
// Get collection data | ||
useEffect(() => { | ||
getDocs(colRef) | ||
//Getting recipes from the database | ||
getDocs(colRef) | ||
.then((snapshot) => { | ||
setRecipes([]) | ||
snapshot.docs.forEach((doc) => { | ||
setRecipes(recipes => [...recipes, {...doc.data(), id: doc.id}]) | ||
}) | ||
}).catch(error => { | ||
console.log(error.message) | ||
}) | ||
}, []) | ||
|
||
console.log(error.message) | ||
}) | ||
} | ||
}) | ||
|
||
return ( | ||
<main className="grid grid-cols-recipe3 justify-center place-content-start | ||
<main className="grid grid-cols-recipe3 justify-center place-content-start | ||
bg-gradient-to-bl from-blue-800 to-green-400 h-[100%] min-h-[calc(100vh-4rem)]"> | ||
<Head> | ||
<title>Recipes</title> | ||
<meta name="viewport" content="initial-scale=1.0, width=device-width"/> | ||
</Head> | ||
{ | ||
recipes.map((recipe: DocumentData) => <RecipeContainer data={recipe} key={recipe.id}/>) | ||
} | ||
</main> | ||
|
||
<Head> | ||
<title>Recipes</title> | ||
<meta name="viewport" content="initial-scale=1.0, width=device-width"/> | ||
</Head> | ||
{auth.currentUser === null ? ( | ||
<div className="h-[calc(100vh-4rem)] place-content-center place-items-center flex"> | ||
<Link href="/recipes/addnew"> | ||
<h1 className="text-center"> | ||
Create your first recipe by clicking here | ||
</h1> | ||
</Link> | ||
</div> | ||
) : ( | ||
<div> | ||
{ | ||
recipes.map((recipe: DocumentData) => <RecipeContainer data={recipe} key={recipe.id}/>) | ||
} | ||
</div> | ||
)} | ||
</main> | ||
) | ||
} | ||
export default Recipes; |
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
cb6f652
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
food-helper – ./
food-helper-rho.vercel.app
food-helper-git-main-aromiii.vercel.app
food-helper-aromiii.vercel.app