Advertisement
didkoslawow

component

Feb 24th, 2024
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CommonModule } from '@angular/common';
  2. import {
  3.   Component,
  4.   EventEmitter,
  5.   Input,
  6.   OnInit,
  7.   Output,
  8.   SimpleChanges,
  9. } from '@angular/core';
  10. import { FormControl, ReactiveFormsModule } from '@angular/forms';
  11. import { GenericFormData, GenericFormModel } from './generic-form.model';
  12. import { RouterLink } from '@angular/router';
  13. import { faListOl, faPlus, faSpoon } from '@fortawesome/free-solid-svg-icons';
  14. import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
  15. import {
  16.   Ingredient,
  17.   IngredientWithId,
  18.   Recipe,
  19.   RecipeData,
  20. } from '../../recipes/recipe.model';
  21.  
  22. @Component({
  23.   selector: 'app-generic-form',
  24.   standalone: true,
  25.   imports: [CommonModule, ReactiveFormsModule, RouterLink, FontAwesomeModule],
  26.   templateUrl: './generic-form.component.html',
  27.   styleUrl: './generic-form.component.scss',
  28. })
  29. export class GenericFormComponent implements OnInit {
  30.   @Input() formData!: GenericFormData;
  31.   @Input() formType!:
  32.     | 'registration'
  33.     | 'login'
  34.     | 'create recipe'
  35.     | 'edit recipe';
  36.   @Input() recipe: Recipe = {};
  37.   @Input() ingredients: Ingredient[] = [];
  38.   @Input() steps: string[] = [];
  39.   @Input() units: string[] = [];
  40.  
  41.   @Output() formSubmit = new EventEmitter<GenericFormData>();
  42.   @Output() addIngredient = new EventEmitter<Ingredient[]>();
  43.   @Output() updateIngredients = new EventEmitter<Ingredient[]>();
  44.   @Output() addStep = new EventEmitter<string>();
  45.   @Output() updateSteps = new EventEmitter<string[]>();
  46.  
  47.   buttonText!: string;
  48.   headingText!: string;
  49.   formModel!: GenericFormModel;
  50.   ingredientToEdit!: IngredientWithId | undefined;
  51.  
  52.   faBtn = faPlus;
  53.   faSpoon = faSpoon;
  54.   faList = faListOl;
  55.  
  56.   ngOnChanges(changes: SimpleChanges): void {
  57.     if ('recipe' in changes && this.isRecipeEditForm()) {
  58.       this.populateFormWithRecipeData();
  59.     }
  60.   }
  61.  
  62.   populateFormWithRecipeData(): void {
  63.     this.formModel.form.patchValue({
  64.       name: this.recipe.name,
  65.       prepTime: this.recipe.prepTime,
  66.       cookTime: this.recipe.cookTime,
  67.       img: this.recipe.img,
  68.       description: this.recipe.description,
  69.     });
  70.   }
  71.  
  72.   ngOnInit(): void {
  73.     this.formModel = new GenericFormModel(this.formData);
  74.     this.formModel.form.addControl('ingredients', new FormControl(''));
  75.  
  76.     this.headingText = this.isRegistrationForm()
  77.       ? 'Sign Up for a Delicious Journey'
  78.       : this.isLoginForm()
  79.       ? "Let's Get Cooking"
  80.       : this.isRecipeCreateForm()
  81.       ? 'Design your recipe'
  82.       : this.isRecipeEditForm()
  83.       ? 'Redesign your recipe'
  84.       : '';
  85.  
  86.     this.buttonText = this.isRegistrationForm()
  87.       ? 'Sign Up'
  88.       : this.isLoginForm()
  89.       ? 'Sign In'
  90.       : this.isRecipeCreateForm()
  91.       ? 'Create'
  92.       : this.isRecipeEditForm()
  93.       ? 'Edit'
  94.       : '';
  95.   }
  96.  
  97.   isRegistrationForm(): boolean {
  98.     return this.formType === 'registration';
  99.   }
  100.  
  101.   isLoginForm(): boolean {
  102.     return this.formType === 'login';
  103.   }
  104.  
  105.   isRecipeEditForm(): boolean {
  106.     return this.formType === 'edit recipe';
  107.   }
  108.  
  109.   isRecipeCreateForm(): boolean {
  110.     return this.formType === 'create recipe';
  111.   }
  112.  
  113.   onAddIngredient(): void {
  114.     const ingredientControl = this.formModel.form.get('ingredient');
  115.     const quantityControl = this.formModel.form.get('quantity');
  116.     const unitControl = this.formModel.form.get('unit');
  117.  
  118.     if (
  119.       ingredientControl &&
  120.       ingredientControl.value &&
  121.       quantityControl &&
  122.       quantityControl.value &&
  123.       unitControl &&
  124.       unitControl.value
  125.     ) {
  126.       const ingredient = {
  127.         id: this.ingredientToEdit?.id,
  128.         name: ingredientControl.value,
  129.         ProductRecipe: {
  130.           quantity: quantityControl.value,
  131.           unit: unitControl.value,
  132.         },
  133.       };
  134.  
  135.       this.ingredients = this.ingredients.filter(
  136.         (i) => i.name !== ingredient.name
  137.       );
  138.  
  139.       this.addIngredient.emit([...this.ingredients, ingredient]);
  140.  
  141.       ingredientControl?.setValue('');
  142.       quantityControl?.setValue('');
  143.       unitControl?.setValue('');
  144.     }
  145.   }
  146.  
  147.   onEditIngredient(ingredient: Ingredient): void {
  148.     const ingredientControl = this.formModel.form.get('ingredient');
  149.     const quantityControl = this.formModel.form.get('quantity');
  150.     const unitControl = this.formModel.form.get('unit');
  151.  
  152.     this.ingredientToEdit = this.ingredients.find((i) => {
  153.       return i.name === ingredient.name;
  154.     });
  155.  
  156.     this.ingredients = this.ingredients.filter(
  157.       (i) => i.name !== ingredient.name
  158.     );
  159.  
  160.     this.updateIngredients.emit([...this.ingredients]);
  161.  
  162.     ingredientControl?.setValue(ingredient.name);
  163.     quantityControl?.setValue(ingredient.ProductRecipe.quantity);
  164.     unitControl?.setValue(ingredient.ProductRecipe.unit);
  165.   }
  166.  
  167.   onAddStep(): void {
  168.     const stepsControl = this.formModel.form.get('steps');
  169.  
  170.     if (stepsControl && stepsControl.value) {
  171.       this.addStep.emit(stepsControl.value);
  172.       stepsControl.setValue('');
  173.     }
  174.   }
  175.  
  176.   onEditSteps(item: string): void {
  177.     const stepsControl = this.formModel.form.get('steps');
  178.  
  179.     this.steps = this.steps.filter((i) => i !== item);
  180.     this.updateSteps.emit([...this.steps]);
  181.  
  182.     stepsControl?.setValue(item);
  183.   }
  184.  
  185.   onSubmit(): void {
  186.     const formData = this.formModel.form.value;
  187.  
  188.     if (this.isRecipeEditForm()) {
  189.       const updatedRecipe: Recipe = {
  190.         name: formData.name,
  191.         prepTime: formData.prepTime,
  192.         cookTime: formData.cookTime,
  193.         img: formData.img,
  194.         ingredients: this.ingredients,
  195.         steps: this.steps,
  196.         description: formData.description,
  197.       };
  198.  
  199.       this.formSubmit.emit(updatedRecipe);
  200.     } else {
  201.       this.formSubmit.emit(formData);
  202.     }
  203.   }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement