aboutsummaryrefslogtreecommitdiffstats
path: root/public/projects/angular-small-apps/apps/recipes/src/app/components/recipe/recipe.component.ts
blob: 070220fa6c0fd3bfcfd7e58f540d91d6023bd619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, Event, Router } from '@angular/router';
import { Recipes } from 'src/app/shared/recipes';
import { LocalStorageService } from 'src/app/shared/services/local-storage.service';
import { RecipesService } from 'src/app/shared/services/recipes.service';

@Component({
  templateUrl: './recipe.component.html',
  styleUrls: ['./recipe.component.scss'],
  encapsulation: ViewEncapsulation.Emulated,
})
export class RecipeComponent implements OnInit {
  recipe: Partial<Recipes> = {};
  isEditable: boolean = false;
  savedRecipes: Recipes[] = this.storage.get('recipes');
  preview: string = this.recipe.strMealThumb!;

  constructor(
    private storage: LocalStorageService,
    private route: ActivatedRoute,
    private recipes: RecipesService,
    private router: Router
  ) {}

  ngOnInit(): void {
    const slug = this.route.snapshot.paramMap.get('slug') || '';
    this.setRecipe(slug);
    this.preview = this.recipe.strMealThumb!;
  }

  setRecipe(slug: string): void {
    const allRecipes = this.storage.get('recipes');
    const filteredRecipes = allRecipes.filter(
      (meal: Recipes) => meal.slug === slug
    );

    if (filteredRecipes.length === 0) {
      const recipeId = history.state?.id;
      if (recipeId) {
        this.recipes.getRecipeById(recipeId).subscribe((recipes: any) => {
          this.recipe = recipes.meals[0];
          this.preview = this.recipe.strMealThumb!;
        });
      } else {
        this.router.navigateByUrl('/404');
      }
    } else {
      this.recipe = filteredRecipes[0] ? filteredRecipes[0] : {};
    }
  }

  getIngredients(): string[] {
    const ingredients = [];

    for (let i = 1; i <= 20; i++) {
      const currentIngredient = `strIngredient${i}` as keyof Recipes;
      let ingredient;

      if (this.recipe[currentIngredient]) {
        const currentMeasure = `strMeasure${i}` as keyof Recipes;

        if (this.recipe[currentMeasure]) {
          ingredient = `${this.recipe[currentMeasure]} ${this.recipe[currentIngredient]}`;
        } else {
          ingredient = `${this.recipe[currentIngredient]}`;
        }
      }

      if (ingredient) ingredients.push(ingredient);
    }

    return ingredients;
  }

  isContentEditable(value: boolean): void {
    this.isEditable = value;
  }

  updateRecipe(e: any) {
    const recipeProperty = e.target.name as keyof Recipes;

    if (!recipeProperty) {
      return;
    }

    this.recipe[recipeProperty] = e.target.value;

    if (recipeProperty.startsWith('strIngredient')) {
      const ingredientNumber = recipeProperty.replace('strIngredient', '');
      const measureProperty = `strMeasure${ingredientNumber}` as keyof Recipes;
      this.recipe[measureProperty] = undefined;
    }

    const updatedRecipes = this.savedRecipes.map((recipe: Recipes) => {
      if (recipe.idMeal === this.recipe.idMeal) {
        return { ...this.recipe };
      }
      return recipe;
    });
    this.storage.set('recipes', updatedRecipes);
  }

  getPreview() {
    return this.preview;
  }

  getImage(img: any) {
    var canvas = document.createElement('canvas');
    canvas.width = img.width;
    canvas.height = img.height;

    var ctx = canvas.getContext('2d')!;
    ctx.drawImage(img, 0, 0);

    var dataURL = canvas.toDataURL('image/png');

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, '');
  }

  updatePreview(e: any) {
    const newPreview = e.target.files[0];

    if (FileReader && newPreview) {
      const fileReader = new FileReader();
      fileReader.onload = (reader) => {
        if (reader.target?.result) {
          const updatedRecipes = this.savedRecipes.map((recipe: Recipes) => {
            if (recipe.idMeal === this.recipe.idMeal) {
              return { ...recipe, strMealThumb: reader.target?.result };
            }
            return recipe;
          });
          this.preview = reader.target?.result as string;
          this.storage.set('recipes', updatedRecipes);
        }
      };
      fileReader.readAsDataURL(newPreview);
    }
  }
}