progress: work on inner section of recipe create function
This commit is contained in:
@@ -134,6 +134,9 @@ export default function AddRecipe() {
|
|||||||
async function handleCreate() {
|
async function handleCreate() {
|
||||||
if (!user || !token) return;
|
if (!user || !token) return;
|
||||||
|
|
||||||
|
let recipeID;
|
||||||
|
let recipeName;
|
||||||
|
|
||||||
// initialize API handlers
|
// initialize API handlers
|
||||||
const recipeAPI = new API.Recipe(token);
|
const recipeAPI = new API.Recipe(token);
|
||||||
const ingredientAPI = new API.Ingredient(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<RecipeIngredient>();
|
let preparedIngredientData = new Array<RecipeIngredient>();
|
||||||
let newIngredientCount = 0;
|
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) {
|
for (let row of ingredientFieldData) {
|
||||||
if (!row) continue;
|
if (!row) continue;
|
||||||
console.log(row);
|
console.log(row);
|
||||||
@@ -180,10 +191,28 @@ export default function AddRecipe() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newID = row.ingredients.filter(ingr => ingr.name == row.ingredientSelection)[0].id;
|
/**
|
||||||
|
* 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 = {
|
const newIngredientData: RecipeIngredient = {
|
||||||
id: newID ?? row.ingredients.length + 1,
|
ingredientid: newID,
|
||||||
|
recipeid: recipeID ?? null,
|
||||||
name: row.ingredientSelection as string,
|
name: row.ingredientSelection as string,
|
||||||
quantity: row.quantity,
|
quantity: row.quantity,
|
||||||
unit: row.measurement
|
unit: row.measurement
|
||||||
@@ -191,14 +220,19 @@ export default function AddRecipe() {
|
|||||||
|
|
||||||
preparedIngredientData.push(newIngredientData);
|
preparedIngredientData.push(newIngredientData);
|
||||||
|
|
||||||
for (let ing of row.ingredients) {
|
|
||||||
// filter out recipes that already exist
|
|
||||||
if (!ingredients.filter(x => x.name == ing.name).includes(ing)) {
|
|
||||||
// post the new ingredient to the database
|
|
||||||
const newEntry = await ingredientAPI.post(ing);
|
|
||||||
messages.push(`Successfully created new ingredient: ${ing.name}!`);
|
messages.push(`Successfully created new ingredient: ${ing.name}!`);
|
||||||
console.log(newEntry);
|
console.log(newEntry);
|
||||||
newIngredientCount++;
|
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
|
// 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
|
// handle recipe post resolve/reject
|
||||||
if (result) {
|
if (result) {
|
||||||
const recipeID = result.recipe.id;
|
|
||||||
const recipeName = result.recipe.name;
|
|
||||||
let recipeIngredientCount = 0;
|
let recipeIngredientCount = 0;
|
||||||
|
|
||||||
for (let ing of preparedIngredientData) {
|
for (let ing of preparedIngredientData) {
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export default function Recipe() {
|
|||||||
(!ingredientData.length) && (async() => {
|
(!ingredientData.length) && (async() => {
|
||||||
const ingredientAPI = new API.Ingredient(token);
|
const ingredientAPI = new API.Ingredient(token);
|
||||||
const result = await ingredientAPI.getAllForRecipe(id);
|
const result = await ingredientAPI.getAllForRecipe(id);
|
||||||
if (result.length) setIngredientData(result);
|
setIngredientData(result);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const selfAuthored = (recipe.authoruserid == user.id!);
|
const selfAuthored = (recipe.authoruserid == user.id!);
|
||||||
@@ -59,7 +59,7 @@ export default function Recipe() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [recipe])
|
}, [recipe, id])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!userData) return;
|
if (!userData) return;
|
||||||
@@ -92,7 +92,18 @@ export default function Recipe() {
|
|||||||
</Protect>
|
</Protect>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}, [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
|
return view
|
||||||
}
|
}
|
||||||
@@ -116,7 +116,7 @@ export class Recipe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async addIngredientToRecipe(ingredient: RecipeIngredient, recipeid: string | number) {
|
async addIngredientToRecipe(ingredient: RecipeIngredient, recipeid: string | number) {
|
||||||
const { quantity, unit, id } = ingredient;
|
const { quantity, unit, ingredientid } = ingredient;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const statement = `
|
const statement = `
|
||||||
@@ -125,7 +125,7 @@ export class Recipe {
|
|||||||
VALUES ($1, $2, $3, $4) RETURNING *
|
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];
|
if (result.rows) return result.rows[0];
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ export interface IIngredient extends HasHistory {
|
|||||||
export interface RecipeIngredient extends Partial<IIngredient> {
|
export interface RecipeIngredient extends Partial<IIngredient> {
|
||||||
unit: string
|
unit: string
|
||||||
quantity: string | number
|
quantity: string | number
|
||||||
|
ingredientid: string | number
|
||||||
|
recipeid: string | number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ICollection extends HasHistory, CanDeactivate {
|
export interface ICollection extends HasHistory, CanDeactivate {
|
||||||
|
|||||||
Reference in New Issue
Block a user