ingredients: Permit use of recipe as ingredient

This commit is contained in:
nemunaire 2022-02-05 12:47:07 +01:00
parent 1de9133374
commit 6e3bb80cc5
4 changed files with 92 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import (
type RecipeIngredient struct {
IngredientID primitive.ObjectID `json:"id"`
Quantity float32 `json:"quantity,omitempty"`
Type string `json:"type,omitempty"`
Unit string `json:"unit,omitempty"`
Info string `json:"info,omitempty"`
}

View File

@ -21,6 +21,8 @@
import IngredientCard from '../components/IngredientCard.svelte';
import { IngredientsStore } from '../stores/ingredients';
import { RecipesStore, updateRecipes } from '../stores/recipes';
updateRecipes();
export let open = false;
export const toggle = () => (open = !open);
@ -29,6 +31,7 @@
export let ingredient = { };
export let ingredient_id = 0;
let idx_i = -1;
let idx_r = -1;
function changeSearch(e) {
ingredient = { };
@ -38,15 +41,26 @@
if (ingredient && ingredient.id) {
idx_i = $IngredientsStore.list.findIndex((e) => (e.id === ingredient.id));
if (idx_i >= 0) {
idx_r = -1;
searchi = $IngredientsStore.list[idx_i].name;
} else {
searchi = "";
idx_r = $RecipesStore.list.findIndex((e) => (e.id === ingredient.id));
if (idx_r >= 0) {
idx_i = -1;
searchi = $RecipesStore.list[idx_r].title;
} else {
searchi = "";
}
}
} else {
searchi = "";
}
tick().then(() => {
document.getElementById(searchi?'iquantity':'searchi').focus();
if (idx_i == -1) {
document.getElementById(searchi?'rquantity':'searchi').focus();
} else {
document.getElementById(searchi?'iquantity':'searchi').focus();
}
})
}
@ -63,11 +77,13 @@
open = false;
return false;
} else {
// First look at the pure ingredients list
idx_i = $IngredientsStore.list.findIndex((e) => (e.name.toLowerCase() === searchi.toLowerCase().trim()));
if (idx_i >= 0) {
if (!ingredient) {
ingredient = { };
}
ingredient.type = "ingredient";
ingredient.id = $IngredientsStore.list[idx_i].id;
if ($IngredientsStore.list[idx_i].unit === 'gram') {
@ -79,6 +95,21 @@
tick().then(() => {
document.getElementById('iquantity').focus();
});
} else {
// Then search between the existing recipes
idx_r = $RecipesStore.list.findIndex((e) => (e.title.toLowerCase() === searchi.toLowerCase().trim()));
if (idx_r >= 0) {
if (!ingredient) {
ingredient = { };
}
ingredient.type = "recipe";
ingredient.id = $RecipesStore.list[idx_r].id;
ingredient.quantity = 1;
tick().then(() => {
document.getElementById('rquantity').focus();
});
}
}
}
@ -161,6 +192,35 @@
ingredient_base={$IngredientsStore.list[idx_i]}
/>
{/if}
{:else if ingredient && ingredient.id && $RecipesStore.list[idx_r]}
<FormGroup>
<Label for="rquantity">Quantité</Label>
<InputGroup>
<Input
type="number"
feedback={ingredient.quantity === null?'La quantité doit être un nombre numérique !':''}
id="rquantity"
invalid={ingredient.quantity === null}
placeholder="50"
min="0"
bind:value={ingredient.quantity}
/>
</InputGroup>
</FormGroup>
<FormGroup>
<Label for="rinfo">Information complèmentaire</Label>
<Input
id="rinfo"
bind:value={ingredient.info}
/>
</FormGroup>
{#if ingredient.quantity}
<IngredientCard
class="mx-3 mx-sm-4 mx-md-5"
{ingredient}
ingredient_base={$RecipesStore.list[idx_r]}
/>
{/if}
{/if}
</ModalBody>
<ModalFooter>
@ -195,4 +255,7 @@
{#each $IngredientsStore.list as ingredient,id}
<option value={ingredient.name}>
{/each}
{#each $RecipesStore.list as recipe,id}
<option value={recipe.title}>
{/each}
</datalist>

View File

@ -4,5 +4,5 @@
</script>
{#if ingredient.quantity}{ingredient.quantity}{#if ingredient.unit}&nbsp;{ingredient.unit}{/if}
{#if ingredient_base}{#if ingredient_base.preposition}{ingredient_base.preposition}{/if}{/if}{/if}{#if ingredient_base}{ingredient_base.name}{/if}
{#if ingredient_base}{#if ingredient_base.preposition}{ingredient_base.preposition}{/if}{/if}{/if}{#if ingredient_base}{#if ingredient_base.title}{ingredient_base.title}{:else}{ingredient_base.name}{/if}{/if}
{#if ingredient.info}({ingredient.info}){/if}

View File

@ -211,6 +211,16 @@
// recipe holds the recipe
export let recipe = { };
$: {
if (recipe.ingredients) {
for (const ingredient of recipe.ingredients) {
if (ingredient.type === 'recipe' && !$RecipesStore.recipes[ingredient.id]) {
RecipesStore.getRecipe(ingredient.id);
}
}
}
}
let open_ingredient_modal = false;
let ingredient_modal = null;
let ingredient_id_modal = -1;
@ -405,12 +415,21 @@
{#if recipe.ingredients}
{#each recipe.ingredients as ingredient, i}
<Col class="mb-2">
<IngredientCard
{ingredient}
ingredient_base={$IngredientsStore.ingredients_idx[ingredient.id]}
style="cursor:pointer"
on:click={updateIngredient(ingredient, i)}
/>
{#if ingredient.type && ingredient.type == "recipe"}
<IngredientCard
{ingredient}
ingredient_base={$RecipesStore.recipes[ingredient.id]}
style="cursor:pointer"
on:click={updateIngredient(ingredient, i)}
/>
{:else}
<IngredientCard
{ingredient}
ingredient_base={$IngredientsStore.ingredients_idx[ingredient.id]}
style="cursor:pointer"
on:click={updateIngredient(ingredient, i)}
/>
{/if}
</Col>
{/each}
{/if}