Skip to content

Commit

Permalink
STUFF (and viggo broke recirect)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiceygyLive committed Feb 8, 2024
1 parent 4f20a24 commit d9a4703
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
19 changes: 9 additions & 10 deletions src/api/recipe/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { collection, doc, getDocs, setDoc } from "firebase/firestore";
import { collection, doc, getDocs, setDoc, QuerySnapshot } from "firebase/firestore";
import { db } from "../firebase";
import log from "../log";
export const runtime = 'edge';
Expand All @@ -11,7 +11,7 @@ export interface ingredient {
quantity: number
ID: number
inDir: boolean

}; //for later

/*
Expand Down Expand Up @@ -42,7 +42,7 @@ export default interface Recipe {
* Uses Date.Now() to generate a unique ID.
* @example "163456789"
*/
// ID: string; //we can get this from the doc ID
ID: string; //we can get this from the doc ID
/**
* The name of the recipe.
* @example "Pasta Sauce"
Expand Down Expand Up @@ -83,7 +83,7 @@ export default interface Recipe {
*/
export async function newRecipe(recipe: Recipe, UserID: string | null, privateRecipe: boolean) {
log(`Adding recipe "${recipe.name}"`, "newRecipe");
if (UserID == null) { return false;}
if (UserID == null) { return false; }
let collectionRef = collection(db, "users", UserID, "recipes");
let docRef = doc(collectionRef); // generate a new document reference with an auto-generated ID
await setDoc(docRef, recipe, { merge: true });
Expand All @@ -97,13 +97,12 @@ export async function newRecipe(recipe: Recipe, UserID: string | null, privateRe
* @param ID The ID of the recipe to get. See {@link Recipe.ID}.
* @returns The JSON data of the recipe. See {@link Recipe}.
*/
export async function getRecipe(ID: string) {
export async function getRecipe(ID: string, UserID: string) {
log(`Getting recipe with ID ${ID}`, "getRecipe");
const querySnapshot = await getDocs(collection(db, "recipes"));
const recipes = querySnapshot.docs.map((doc) => doc.data() as Recipe);
const recipe = recipes.find((recipe) => recipe.name === ID);
log(`Got recipe with ID ${ID}`, "getRecipe");
return recipe;
let querySnapshot = await getDocs(collection(db, "users", UserID, "recipes"));
let recipes = querySnapshot.docs.map((doc:any) => doc.data() as Recipe);
console.log(JSON.stringify(recipes[0]))
return recipes[0];
}


Expand Down
17 changes: 15 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from '@/api/log';
import BouncyButton from '@/components/bouncyButton';
import { User } from 'firebase/auth';
import Head from 'next/head';
Expand All @@ -8,8 +9,20 @@ import { useState } from 'react';

export default function Home() {
const [localUser, setLocalUser] = useState<User | null>(null);

const [authBtnUrl, setAuthBtnUrl] = useState<string>('/login');
const router = useRouter();

function authBtnPush(router:any){
if (localUser) {
setAuthBtnUrl('/login');
log("Auth button push set to /login", "index/authBtnPush")
} else {
setAuthBtnUrl('/logout');
log("Auth button push set to /logout", "index/authBtnPush")
}
router.push(authBtnUrl)

}
function push(path: string) { router.push(path) }
return (
<>
Expand All @@ -26,7 +39,7 @@ export default function Home() {
{/* <BouncyButton shouldBounceEval={() => { return true }} className='primary'>
Start cooking
</BouncyButton> */}
<button className='primary' onClick={() => { push('/settings') }}>Settings</button>
<button className='primary' onClick={() => { authBtnPush(router) }}>Settings</button>
</main>
</>
)
Expand Down
11 changes: 8 additions & 3 deletions src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export default function Page({ Component, pageProps }: AppProps) {
redirectURL: '/'
})
const router = useRouter();
const {redirect} = router.query;
const then = router.query.then
useEffect(() => {
if (router.query.then && router.query.thenDisplayName) {
log("Redirect data: " + router.query.then + " " + router.query.thenDisplayName, "login")
console.log(then)
if (router.query.then && router.query.thenDisplayText) {
log("Redirect data: " + router.query.then + " " + router.query.thenDisplayText, "login")
setRedirectData({
redirectDisplayText: <span>To continue to <strong>{router.query.thenDisplayName.toString()}</strong></span>,
redirectDisplayText: <span>To continue to <strong>{router.query.thenDisplayText.toString()}</strong></span>,
redirectURL: router.query.then.toString()
})
} else {
Expand Down Expand Up @@ -49,6 +52,8 @@ export default function Page({ Component, pageProps }: AppProps) {
await authUser(email, password);
log("Logged in! Redirecting to desired page or index if not specified", "login/handleSubmit")
toggleLoggedIn()
if (!redirect) { log("No redirect specified! Redirecting to index", "login/handleSubmit"); router.push('/'); return false }
else {log("Redirect specified! Redirecting to " + redirect, "login/handleSubmit"); router.push("/"+redirect.toString()); return false}
router.push(redirectData.redirectURL);

return false;
Expand Down
28 changes: 28 additions & 0 deletions src/pages/recipe/view.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { getRecipe } from "@/api/recipe/functions";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { Recipe } from "@/api/recipe/functions";


export default function View() {
const router = useRouter();
const { id } = router.query;
if (!id) {
return <h1>Invalid recipe ID</h1>;
} else {
let rid = id.toString().split("-")[0];
let uid = id.toString().split("-")[1];

const [recipe, setRecipe] = useState<Recipe | null>(null);
useEffect(() => {
if (id) {
getRecipe(rid.toString(), uid).then((recipe) => {
setRecipe(recipe);
});
}
}, [id]);
}
return (
<div>
<h1>View</h1>
<p>Recipe ID: {id}</p>
<p>Recipe: {Recipe?.name}</p>
<p>Ingredents:{Recipe?.ingredents}</p>
<p>Instructions:{Recipe?.instructions[0]}</p>

</div>
);
}
1 change: 1 addition & 0 deletions src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function UserSettings() {
"User is null! Redirecting to login page",
"settings/listenForUser"
);
router.push("/login?redirect=settings");
} else {
setLocalUser(user);
log(`User signed in as ${JSON.stringify(user.displayName)}. Continuing...`, `settings/listenForUser`);
Expand Down

0 comments on commit d9a4703

Please sign in to comment.