From 2754fe6c098cbbccc782f854bb877ce7e963c441 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 25 Feb 2023 21:13:43 -0600 Subject: [PATCH] progress: work on inner section of recipe create function --- client/src/components/pages/AddRecipe.tsx | 63 +++++++++++++++++------ client/src/components/pages/Recipe.tsx | 17 ++++-- server/models/recipe.ts | 4 +- server/schemas/index.ts | 2 + 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/client/src/components/pages/AddRecipe.tsx b/client/src/components/pages/AddRecipe.tsx index ab9097a..e1f320a 100644 --- a/client/src/components/pages/AddRecipe.tsx +++ b/client/src/components/pages/AddRecipe.tsx @@ -134,6 +134,9 @@ export default function AddRecipe() { async function handleCreate() { if (!user || !token) return; + let recipeID; + let recipeName; + // initialize API handlers const recipeAPI = new API.Recipe(token); const ingredientAPI = new API.Ingredient(token); @@ -157,10 +160,18 @@ export default function AddRecipe() { } } + // post recipe entry + const result = await recipeAPI.post(input); + + if (result) { + recipeID = result.recipe.id; + recipeName = result.recipe.name; + } + let preparedIngredientData = new Array(); let newIngredientCount = 0; - // handle ingredient row data + // check ingredient row for null values; normalize each row's data and insert into array above for (let row of ingredientFieldData) { if (!row) continue; console.log(row); @@ -180,25 +191,48 @@ export default function AddRecipe() { continue; } - const newID = row.ingredients.filter(ingr => ingr.name == row.ingredientSelection)[0].id; - - const newIngredientData: RecipeIngredient = { - id: newID ?? row.ingredients.length + 1, - name: row.ingredientSelection as string, - quantity: row.quantity, - unit: row.measurement - } - - preparedIngredientData.push(newIngredientData); - + /** + * TO DO: + * + * this inner row isn't working correctly just yet + * once the inputs for each row have been validated: + * + * 1. create new ingredient entries for new ingredients + * 2. create ingredient recipe links for all recipes + */ + for (let ing of row.ingredients) { // filter out recipes that already exist if (!ingredients.filter(x => x.name == ing.name).includes(ing)) { + console.log(ing.name); + // post the new ingredient to the database const newEntry = await ingredientAPI.post(ing); + const newID = newEntry.id; + + const newIngredientData: RecipeIngredient = { + ingredientid: newID, + recipeid: recipeID ?? null, + name: row.ingredientSelection as string, + quantity: row.quantity, + unit: row.measurement + } + + preparedIngredientData.push(newIngredientData); + messages.push(`Successfully created new ingredient: ${ing.name}!`); console.log(newEntry); newIngredientCount++; + } else { + const newIngredientData: RecipeIngredient = { + ingredientid: (ingredients.filter(x => x.name == ing.name)[0].id as number), + recipeid: recipeID ?? null, + name: row.ingredientSelection as string, + quantity: row.quantity, + unit: row.measurement + } + + preparedIngredientData.push(newIngredientData); } // update the ingredient list @@ -206,13 +240,8 @@ export default function AddRecipe() { } } - // post recipe entry - const result = await recipeAPI.post(input); - // handle recipe post resolve/reject if (result) { - const recipeID = result.recipe.id; - const recipeName = result.recipe.name; let recipeIngredientCount = 0; for (let ing of preparedIngredientData) { diff --git a/client/src/components/pages/Recipe.tsx b/client/src/components/pages/Recipe.tsx index e0b7974..f20c31e 100644 --- a/client/src/components/pages/Recipe.tsx +++ b/client/src/components/pages/Recipe.tsx @@ -44,7 +44,7 @@ export default function Recipe() { (!ingredientData.length) && (async() => { const ingredientAPI = new API.Ingredient(token); const result = await ingredientAPI.getAllForRecipe(id); - if (result.length) setIngredientData(result); + setIngredientData(result); })(); const selfAuthored = (recipe.authoruserid == user.id!); @@ -59,7 +59,7 @@ export default function Recipe() { } } } - }, [recipe]) + }, [recipe, id]) useEffect(() => { if (!userData) return; @@ -92,7 +92,18 @@ export default function Recipe() { ); } - }, [userData, recipe, ingredientData]); + }, [userData, recipe, ingredientData, id]); + + useEffect(() => { + if (!ingredientData.length || !token) return; + for (let each of ingredientData) { + (async() => { + const ingredientAPI = new API.Ingredient(token); + const result = await ingredientAPI.getByID(each.ingredientid!.toString()); + console.log(result); + })(); + } + }, [ingredientData, token]) return view } \ No newline at end of file diff --git a/server/models/recipe.ts b/server/models/recipe.ts index c057b31..72c06d0 100644 --- a/server/models/recipe.ts +++ b/server/models/recipe.ts @@ -116,7 +116,7 @@ export class Recipe { } async addIngredientToRecipe(ingredient: RecipeIngredient, recipeid: string | number) { - const { quantity, unit, id } = ingredient; + const { quantity, unit, ingredientid } = ingredient; try { const statement = ` @@ -125,7 +125,7 @@ export class Recipe { VALUES ($1, $2, $3, $4) RETURNING * ` - const result = await pool.query(statement, [quantity, unit, id, recipeid]); + const result = await pool.query(statement, [quantity, unit, ingredientid, recipeid]); if (result.rows) return result.rows[0]; return []; diff --git a/server/schemas/index.ts b/server/schemas/index.ts index bfb7c42..030fb68 100644 --- a/server/schemas/index.ts +++ b/server/schemas/index.ts @@ -47,6 +47,8 @@ export interface IIngredient extends HasHistory { export interface RecipeIngredient extends Partial { unit: string quantity: string | number + ingredientid: string | number + recipeid: string | number } export interface ICollection extends HasHistory, CanDeactivate {