From 531e4f057f9bba4e3b12901ec5a749a061a79485 Mon Sep 17 00:00:00 2001 From: jableader Date: Sat, 28 Sep 2024 14:57:32 +1000 Subject: [PATCH] Squashed commit of the following: commit c5461420e7f1022019b79517d853a26301add9e9 Author: jableader Date: Sat Sep 28 14:55:18 2024 +1000 meal scaling improvements commit 37a444ff1fb5cbd1942161a8097a44fa7483b14c Author: jableader Date: Sat Sep 28 13:13:06 2024 +1000 Implementation --- package.json | 2 +- src/components/meals/EditMealPage.vue | 77 +++++++++++++++++-- src/components/meals/MealCard.vue | 8 +- src/components/shopping/MealSelectionList.vue | 3 +- src/components/shopping/shopping.js | 16 ++-- 5 files changed, 85 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index ae944eb..13f6c60 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "doof-front", + "name": "doof", "version": "0.1.0", "private": true, "scripts": { diff --git a/src/components/meals/EditMealPage.vue b/src/components/meals/EditMealPage.vue index 4b4ab94..3da8b61 100644 --- a/src/components/meals/EditMealPage.vue +++ b/src/components/meals/EditMealPage.vue @@ -11,11 +11,17 @@

Recipes

    -
  • +
  • - +

    -
  • @@ -31,7 +37,10 @@

    Sides & Additional Ingredients

- + + +

Purchased {{ formatDate(meal.purchase_date) }}

+ @@ -90,13 +99,36 @@ export default { } } - this.meal.recipes.push(recipe); + this.meal.recipes.push({ recipe, recipe_id: recipe.id, meal_id: this.meal.id, servings: recipe.serves }); }, selectDate(date) { this.meal.suggested_date = date; }, - removeRecipe(recipe) { - this.meal.recipes = this.meal.recipes.filter(r => r.id !== recipe.id); + formatDate(date) { + // If it's today, show 'x hours ago' + // If it's within the last week, show 'x days ago' + // If it's within the last month weeks, show 'x weeks ago' + // Otherwise, show the date as dd/mm + const now = new Date(); + const diff = now - date; + const days = Math.floor(diff / (1000 * 60 * 60 * 24)); + if (days === 0) { + const hours = Math.floor(diff / (1000 * 60 * 60)); + return `${hours} hour${hours === 1 ? '' : 's'} ago`; + } + + if (days < 7) { + return `${days} day${days === 1 ? '' : 's'} ago`; + } + + if (days < 30) { + return `${Math.floor(days / 7)} week${days === 7 ? '' : 's'} ago`; + } + + return date.toLocaleDateString('en-au', { day: 'numeric', month: 'numeric' }); + }, + removeRecipe(mealRecipe) { + this.meal.recipes = this.meal.recipes.filter(r => r != mealRecipe); }, addIngredient() { this.meal.extra_ingredients = [{ line: '', product: null }, ...this.meal.extra_ingredients]; @@ -198,4 +230,35 @@ li { margin: 0; } +.recipe-card { + flex: 1; +} + +.servings { + display: inline-block; + margin-right: 1em; +} + +.servings input { + width: 3em; + border: none; + border-bottom: 1px solid #000; + text-align: center; + font-size: large; + font-style: italic; +} + +.delete-button { + background: none; + border: none; + cursor: pointer; + border-radius: 1em; + padding: 0.5em; +} + +.delete-button:hover { + /* Invert the colors of the trash icon */ + filter: invert(1); +} + \ No newline at end of file diff --git a/src/components/meals/MealCard.vue b/src/components/meals/MealCard.vue index b052955..ce72a01 100644 --- a/src/components/meals/MealCard.vue +++ b/src/components/meals/MealCard.vue @@ -3,8 +3,8 @@

{{ dayOfWeek }} {{ date }}

- - {{ recipe.name }}{{ seperator(index, meal.recipes) }} + + {{ meal_recipe.recipe.name }}{{ seperator(index, meal.recipes) }} with @@ -12,8 +12,8 @@

- - {{ recipe.name }}{{ seperator(index, meal.recipes) }} + + {{ meal_recipe.recipe.name }}{{ seperator(index, meal.recipes) }}

diff --git a/src/components/shopping/MealSelectionList.vue b/src/components/shopping/MealSelectionList.vue index 634cbd3..729c76e 100644 --- a/src/components/shopping/MealSelectionList.vue +++ b/src/components/shopping/MealSelectionList.vue @@ -93,7 +93,8 @@ const formatDate = (date) => { const getUrl = (meal) => { // First non empty value in meal.recipe/image_urls - for (const recipe of meal.recipes) { + for (const mr of meal.recipes) { + const recipe = mr.recipe; if (recipe.image_urls.length && recipe.image_urls[0]) { return recipe.image_urls[0]; } diff --git a/src/components/shopping/shopping.js b/src/components/shopping/shopping.js index b1d7b5a..6bc8ac7 100644 --- a/src/components/shopping/shopping.js +++ b/src/components/shopping/shopping.js @@ -1,19 +1,19 @@ import units from '@/units.js'; -function scaleFactor(meal, recipe) { - const numYield = recipe.serves; - const numConsumers = meal.consumers.length; +function scaleFactor(mealRecipe) { + const numRecipe = mealRecipe.recipe.serves; + const numRequested = mealRecipe.servings; - return numConsumers / numYield; + return numRequested / numRecipe; } export function mealToShoppingListSources(meal) { const sources = []; - for (const recipe of meal.recipes) { - const scale = scaleFactor(meal, recipe); - for (const ingredient of recipe.ingredients) { + for (const mealRecipe of meal.recipes) { + const scale = scaleFactor(mealRecipe); + for (const ingredient of mealRecipe.recipe.ingredients) { const quantity = ingredient.quantity * scale; - sources.push({ meal, recipe, ingredient: { ...ingredient, quantity, }, }); + sources.push({ meal, mealRecipe, recipe: mealRecipe.recipe, ingredient: { ...ingredient, quantity, }, }); } }