Skip to content

Commit

Permalink
Optional ingredients (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid authored Jun 18, 2024
1 parent ecc55d1 commit ff6c559
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function IngredientAddDrawer({
// Filter out products already assigned other ingredients.
.filter(
(product) =>
!ingredients.flatMap((ingredient) => ingredient.product.id).includes(product.id)
!ingredients.flatMap((ingredient) => ingredient.product?.id).includes(product.id)
)
.map((product) => ({
image: product.thumbnailUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ function IngredientsOverviewTable({ data, onDeleteIngredient }: IngredientsOverv
accessorKey: 'product.fullName',
Cell: ({ row, renderedCellValue }) => (
<div className="flex items-center space-x-3">
<img src={row.original.product.thumbnailUrl} className="object-contain w-8 h-8" />
<Anchor href={productRoutes.detail.build({ productId: row.original.product.id })}>
{renderedCellValue}
</Anchor>
<img src={row.original.product?.thumbnailUrl} className="object-contain w-8 h-8" />
{row.original.product?.id && (
<Anchor href={productRoutes.detail.build({ productId: row.original.product.id })}>
{renderedCellValue}
</Anchor>
)}
</div>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function IngredientInput({
data = {
...data,
ingredient: ingredient || ({} as RecipeIngredientRecord),
portionQuantityUnit: ingredient?.product.unit || ({} as UnitRecord),
portionQuantityUnit: ingredient?.product?.unit || ({} as UnitRecord),
}
} else if (key === 'portionQuantityUnit') {
const unit = units?.find((unit) => unit.id.toString() === event)
Expand Down Expand Up @@ -115,8 +115,8 @@ function IngredientInput({
)
.map((ingredient) => ({
label: ingredient.title,
image: ingredient.product.thumbnailUrl,
description: ingredient.product.fullName,
image: ingredient.product?.thumbnailUrl,
description: ingredient.product?.fullName,
value: ingredient.id.toString(),
})),
[ingredients, ingredientItemGroup, ingredientItem]
Expand Down
20 changes: 15 additions & 5 deletions frontend/apps/recipes/components/RecipeForm/RecipeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,21 @@ function RecipeForm({ recipe, ingredients, onSubmit }: RecipeFormProps) {
/>
}
/>
<div className={`flex space-x-3 justify-end py-4 border-t ${classes.border}`}>
<Button variant="default" onClick={() => navigate(-1)}>
Cancel
</Button>
<Button onClick={submit}>Save</Button>
<div className={`flex space-x-3 justify-between py-4 border-t ${classes.border}`}>
<div className="flex space-x-3">
<Button variant="light" onClick={ingredientGroupActions.groupAdd}>
Add group
</Button>
<Button variant="light" onClick={stepActions.inputAdd}>
Add step
</Button>
</div>
<div className="flex space-x-3">
<Button variant="default" onClick={() => navigate(-1)}>
Cancel
</Button>
<Button onClick={submit}>Save</Button>
</div>
</div>
</Card>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/recipes/components/RecipeForm/StepInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function StepInput({
variant="light"
className="mt-5"
disabled={!canBeDeleted}
onClick={() => onAction('inputDelete', stepNumber)}
onClick={() => onAction('inputDelete', stepNumber - 1)}
>
<IconX />
</ActionIcon>
Expand Down
2 changes: 1 addition & 1 deletion frontend/types/generated/models/IngredientCreateForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

export type IngredientCreateForm = {
title: string;
product: string;
product?: string;
};

2 changes: 1 addition & 1 deletion frontend/types/generated/models/RecipeIngredientRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import type { ProductRecord } from './ProductRecord';
export type RecipeIngredientRecord = {
id: number;
title: string;
product: ProductRecord;
product?: ProductRecord;
};

4 changes: 2 additions & 2 deletions nest/recipes/ingredients/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class IngredientCreateForm(BaseModel):
title: str = FormField(
..., order=1, help_text="User friendly title. E.g. Red tomatoes."
)
product_id: str = FormField(
...,
product_id: str | None = FormField(
None,
alias="product",
order=2,
component=FrontendComponents.SELECT.value,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.7 on 2024-06-18 12:03

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('products', '0003_product_allergens_product_carbohydrates_and_more'),
('recipes_ingredients', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='recipeingredient',
name='product',
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='ingredient', to='products.product'),
),
]
2 changes: 2 additions & 0 deletions nest/recipes/ingredients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class RecipeIngredient(BaseModel):
"products.Product",
related_name="ingredient",
on_delete=models.CASCADE,
blank=True,
null=True,
)

objects = _RecipeIngredientManager()
Expand Down
6 changes: 4 additions & 2 deletions nest/recipes/ingredients/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
class RecipeIngredientRecord(BaseModel):
id: int
title: str
product: ProductRecord
product: ProductRecord | None

@classmethod
def from_db_model(cls, model: RecipeIngredient) -> RecipeIngredientRecord:
return cls(
id=model.id,
title=model.title,
product=ProductRecord.from_product(model.product),
product=ProductRecord.from_product(model.product)
if model.product
else None,
)


Expand Down
3 changes: 3 additions & 0 deletions nest/recipes/plans/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def _get_products_dataframe(self) -> pl.DataFrame:
for item in group.ingredient_items:
product = item.ingredient.product

if product is None:
continue

portion_quantity = item.portion_quantity * recipe_portion_factor

converted_quantity = convert_unit_quantity(
Expand Down
6 changes: 2 additions & 4 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2609,8 +2609,7 @@
"type": "object",
"required": [
"id",
"title",
"product"
"title"
],
"properties": {
"id": {
Expand Down Expand Up @@ -2968,8 +2967,7 @@
"x-form": true,
"x-columns": 1,
"required": [
"title",
"product"
"title"
],
"properties": {
"title": {
Expand Down

0 comments on commit ff6c559

Please sign in to comment.