blob: 710b9675857167d899bba00929cf8d2d1d3e91af (
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
|
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Router } from '@angular/router';
import { LocalStorageService } from 'src/app/shared/services/local-storage.service';
import { RecipesService } from 'src/app/shared/services/recipes.service';
import { slugify } from 'src/app/shared/utilities/slugify';
@Component({
selector: 'toolbar',
templateUrl: './toolbar.component.html',
styleUrls: ['./toolbar.component.scss'],
})
export class ToolbarComponent {
@Output() isEditionMode = new EventEmitter<boolean>();
isEditionEnabled: boolean = false;
recipeId = history.state?.id;
savedRecipes: object[] = this.storage.get('recipes');
toggleLabel: string = 'Edition disabled';
constructor(
private storage: LocalStorageService,
private recipes: RecipesService,
private router: Router
) {}
isSavedRecipe(): boolean {
let recipeIndex;
if (this.recipeId) {
recipeIndex = this.savedRecipes.findIndex(
(recipe: any) => recipe.idMeal === this.recipeId
);
} else {
const slug = this.router.url.replace('/recipe/', '');
recipeIndex = this.savedRecipes.findIndex(
(recipe: any) => recipe.slug === slug
);
}
return recipeIndex === -1 ? false : true;
}
shouldDisplaySave(): boolean {
return !this.isSavedRecipe();
}
saveRecipe(): void {
this.recipes.getRecipeById(this.recipeId).subscribe((recipe: any) => {
const currentRecipe = recipe.meals[0];
const newRecipe = {
...currentRecipe,
slug: slugify(currentRecipe.strMeal),
};
this.savedRecipes.push(newRecipe);
this.storage.set('recipes', this.savedRecipes);
});
}
toggleEdition() {
this.isEditionEnabled = !this.isEditionEnabled;
this.toggleLabel = this.isEditionEnabled
? 'Edition enabled'
: 'Edition disabled';
this.isEditionMode.emit(this.isEditionEnabled);
}
}
|