Skip to content

Commit

Permalink
combine recipes before analysing menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkaterina committed Apr 26, 2024
1 parent e118f76 commit 43fe80c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/app/analysis/components/form/menu-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MenuCard from '@/app/components/cards/menu-cards/menu-card';
import RecipeSelect from './recipe-select';
import SmallSpinner from '@/app/components/utilities/loading/small-spinner';
import removeID from './utils/removeID';
import { combineRecipes } from './utils/combine-recipes';
import { AuthContext } from '@/app/context/auth-context';
import { CardOpenContext } from '@/app/context/card-context';
import { CurrentMenuContext } from '@/app/context/menu-context';
Expand Down Expand Up @@ -93,15 +94,16 @@ const MenuForm = ({ searchCleared, setClearSearch }: MenuFormProps): JSX.Element
const handleSubmit = async(e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

const recipesArray = combineRecipes(currentRecipes);
const ingredientsArray = ArrayfromString(ingredientsString);
const nutrients: Nutrients | null = await fetchMenuNutrients(ingredientsArray, currentRecipes);
const nutrients: Nutrients | null = await fetchMenuNutrients(ingredientsArray, recipesArray);

if(nutrients) {
const newMenu = {
name,
nutrients: nutrients,
nutrients,
ingredients: ingredientsArray,
recipes: currentRecipes
recipes: recipesArray
};
setCardOpen(CardState.OPEN);
setCurrentMenu({
Expand Down
4 changes: 2 additions & 2 deletions src/app/analysis/components/form/recipe-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const RecipeSelect = ({ inputs, currentRecipes, setCurrentRecipes, loadedRecipes
const newRecipes: RecipeWithServings[] = Array(inputs - currentRecipes.length).fill({
selectedRecipeId: loadedRecipes[0].id,
selectedRecipe: loadedRecipes[0].recipe,
selectedServings: 1
selectedServings: 0
});
const newRecipesArray: RecipeWithServings[] = [...currentRecipes, ...newRecipes];
setCurrentRecipes(newRecipesArray);
Expand Down Expand Up @@ -69,7 +69,7 @@ const RecipeSelect = ({ inputs, currentRecipes, setCurrentRecipes, loadedRecipes

let numberInputs = [];
for(let i = 0; i < inputs; i++) {
numberInputs.push( <input type="number" id="servings" name="servings" value={currentRecipes[i] ? currentRecipes[i].selectedServings : 1} required min={1} key={i} onChange={(e) => handleInputChange(i, Number(e.target.value))}/>)
numberInputs.push( <input type="number" id="servings" name="servings" value={currentRecipes[i] ? currentRecipes[i].selectedServings : 0} required min={0} key={i} onChange={(e) => handleInputChange(i, Number(e.target.value))}/>)
}
return numberInputs;
}
Expand Down
35 changes: 35 additions & 0 deletions src/app/analysis/components/form/utils/combile-recipes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { combineRecipes } from "./combine-recipes";
import recipe from '@/app/test_objects/loaded-recipe.json';

describe('combine recipes', () => {

it('should remove recipes with zero servings and combine recipes with the same id', () => {

const recipes = [
{
selectedRecipeId: recipe.id,
selectedRecipe: recipe.recipe,
selectedServings: 0
},
{
selectedRecipeId: recipe.id,
selectedRecipe: recipe.recipe,
selectedServings: 3
},
{
selectedRecipeId: recipe.id,
selectedRecipe: recipe.recipe,
selectedServings: 2
},
];

const combinedRecipes = [
{
selectedRecipeId: recipe.id,
selectedRecipe: recipe.recipe,
selectedServings: 5
}
]
expect(combineRecipes(recipes)).toEqual(combinedRecipes);
})
})
21 changes: 21 additions & 0 deletions src/app/analysis/components/form/utils/combine-recipes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { RecipeWithServings } from "@/app/types/types";

export const combineRecipes = (currentRecipes: RecipeWithServings[]): RecipeWithServings[] => {

const recipes = currentRecipes.filter(recipe => recipe.selectedServings != 0);
const combinedRecipes = [];

for (const recipe of recipes) {
const { selectedRecipeId: id, selectedServings, selectedRecipe } = recipe;

const existingRecipeIndex = combinedRecipes.findIndex(r => r.selectedRecipeId === id);

if (existingRecipeIndex !== -1) {
combinedRecipes[existingRecipeIndex].selectedServings += selectedServings;
} else {
combinedRecipes.push({ selectedRecipeId: id, selectedServings, selectedRecipe });
}
}

return combinedRecipes;
}
2 changes: 1 addition & 1 deletion src/app/analysis/recipe-analysis/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const RecipeAnalysis = ({ }: RecipeAnalysisProps): JSX.Element => {

const { cardOpen } = useContext(CardOpenContext);
const [clearSearch, setClearSearch] = useState<boolean>(false);
const [file, setFile] = useState<FormData | null>(null);
const [file, setFile] = useState<Blob | null>(null);

const secondaryColor: string = "var(--secondary-color)";

Expand Down

0 comments on commit 43fe80c

Please sign in to comment.