From 2a2892c71c6313974ef42c1b6e45e98e6676531e Mon Sep 17 00:00:00 2001 From: christophka Date: Mon, 16 Nov 2020 00:51:14 +0100 Subject: [PATCH] feat: display ingredients and preparation steps for recipe groups --- .../preparation/preparation.component.html | 31 +++++---- .../preparation/preparation.component.ts | 66 +++++++++++++++++-- .../components/recipe/recipe.component.html | 4 +- .../components/recipe/recipe.component.ts | 26 +++++++- 4 files changed, 106 insertions(+), 21 deletions(-) diff --git a/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.html b/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.html index 2dc642c..f9352e2 100644 --- a/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.html +++ b/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.html @@ -1,13 +1,20 @@ -
+
+
{{ group.label }}
- -
    -
  1. - {{ step }} -
  2. -
-
+
+ + +
    +
  1. + {{ step }} +
  2. +
+
+
diff --git a/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.ts b/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.ts index 8b300d2..453f0de 100644 --- a/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.ts +++ b/packages/frontend/src/app/modules/domain/recipe/components/preparation/preparation.component.ts @@ -1,7 +1,19 @@ import { Component, OnInit, Input } from '@angular/core'; import { Recipe } from '@overckd/domain'; +import { BehaviorSubject, Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +interface PreparationGroup { + label?: string; + steps: Recipe['steps']; + stepsEnumerated?: Recipe['stepsEnumerated']; + start?: number; +} + +/** + * Component for displaying recipe preparation steps + */ @Component({ selector: 'overckd-preparation', templateUrl: './preparation.component.html', @@ -10,15 +22,59 @@ import { Recipe } from '@overckd/domain'; export class PreparationComponent implements OnInit { @Input() recipe: Recipe; + private recipe$: BehaviorSubject; + public preparationGroups$: Observable; + constructor() {} - ngOnInit() {} + ngOnInit() { + this.recipe$ = new BehaviorSubject(this.recipe); + + this.preparationGroups$ = this.recipe$ + .asObservable() + .pipe(map(recipe => this.getPreparationGroups(recipe))); + } - public get steps() { - return this.recipe.steps; + public isArray(obj: any) { + return Array.isArray(obj); } - public get asList() { - return Array.isArray(this.steps); + private getPreparationGroups(recipe: Recipe): PreparationGroup[] { + const { groups } = recipe; + + if (!groups || groups.length === 0) { + return [ + { + steps: recipe.steps, + stepsEnumerated: recipe.stepsEnumerated, + }, + ]; + } + + let start = 1; + const stepsFromGroups = groups.map( + ({ label, steps, stepsEnumerated }) => { + const group = { + start, + label, + steps, + stepsEnumerated, + }; + + start += this.isArray(steps) ? steps.length : 1; + + return group; + }, + ); + + return [ + ...stepsFromGroups, + { + label: 'Weitere Zubereitung', + start, + steps: recipe.steps, + stepsEnumerated: recipe.stepsEnumerated, + }, + ]; } } diff --git a/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.html b/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.html index cec3b7c..bd16b77 100644 --- a/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.html +++ b/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.html @@ -24,10 +24,10 @@ > -
+

Zutaten

diff --git a/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.ts b/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.ts index 9c4f73c..b1dc611 100644 --- a/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.ts +++ b/packages/frontend/src/app/modules/domain/recipe/components/recipe/recipe.component.ts @@ -1,6 +1,8 @@ import { Component, OnInit, Input, HostBinding } from '@angular/core'; +import { BehaviorSubject, Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; -import { Recipe } from '@overckd/domain'; +import { Recipe, IngredientGroup } from '@overckd/domain'; /** * Component to display a recipe @@ -15,6 +17,9 @@ export class RecipeComponent implements OnInit { @Input() recipe: Recipe; @Input() numberOfLines = 5; + public recipe$: BehaviorSubject; + public ingredients$: Observable; + /** * This is the target amount for the portion size */ @@ -35,7 +40,24 @@ export class RecipeComponent implements OnInit { constructor() {} - ngOnInit() {} + ngOnInit() { + this.recipe$ = new BehaviorSubject(this.recipe); + + this.ingredients$ = this.recipe$.pipe( + map(recipe => { + const { groups } = recipe; + const ingredientGroups = groups + ? groups.map(g => ({ + label: g.label, + group: g.name, + ingredients: g.ingredients, + })) + : []; + + return [...ingredientGroups, ...recipe.ingredients]; + }), + ); + } get primaryImage() { return this.recipe.images[0];