summaryrefslogtreecommitdiff
path: root/recipeBuddy/src/app/edit-recipe/edit-recipe.component.ts
blob: e7966b3850a21ef6b61a92b6507188fec9a6f99f (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Component, OnInit } from '@angular/core';

import { FormControl } from '@angular/forms';

import { FormBuilder } from '@angular/forms';
import { FormArray } from '@angular/forms';

import { Validators } from '@angular/forms';

import { Router } from '@angular/router';

import { Recipe } from '../DataModels/recipe';
import { Ingredient } from '../DataModels/ingredient'
import { Step } from '../DataModels/step';
import { BackendService } from '../REST_service/backend.service';
import { RecipePassService } from '../recipePass/recipe-pass.service';

@Component({
  selector: 'app-edit-recipe',
  templateUrl: './edit-recipe.component.html',
  styleUrls: ['./edit-recipe.component.css']
})
export class EditRecipeComponent implements OnInit {

  baseRecipe: Recipe = this.passService.getRecipe();

  recipeForm = this.fb.group({
    recipeName: ['', Validators.required],
    desc: [''],
    ingredients: this.fb.array([
      this.fb.group({
        name: [''],
        amount: ['', Validators.pattern('^[0-9]*(\.[0-9]*)?$')],
        unit: ['']
        })
    ]),
    steps: this.fb.array([
      this.fb.group({
        instruction: [''],
        timer: ['']
        })
    ]),
    servingSize: ['', Validators.pattern('^[0-9]*(\.[0-9]*)?$')],
    cookTime: ['', Validators.pattern('^[0-9]*$')],
    tags: [''],
    photos: ['']
  });

  constructor(private fb: FormBuilder,
              private restService: BackendService,
              private router: Router,
              private passService: RecipePassService,
              )
  {
    restService.getRecipe(this.baseRecipe.id).subscribe(
      res=> this.updateRecipe(res),
      err=> console.log('EditRecipeComponent:restService:getRecipes: id='+ this.baseRecipe.id + 'err=' + err),
      () => console.log('EditRecipe:restService:getRecipes: completed')
    )

    this.rmIngredient(0);
    this.rmStep(0);
  }

  updateRecipe(r: Recipe)
  {
    var i: number;

    this.recipeForm.patchValue(
      {
        recipeName:  r.name,
        desc:        r.description,
        servingSize: r.servingSize,
        cookTime:    r.cookTime,
        tags:        r.tags.join(','),
        photos:      r.photos.join(','),
      }
    )

    console.log(r.tags.join(','));

    for(i = 0; i < r.ingredients.length; i++) {
      this.addIngredient();
      this.ingredients.controls[0].setValue(r.ingredients[i]);
    }
    for(i = 0; i < r.steps.length; i++) {
      this.addStep();
      console.log(r.steps[i])
      this.steps.controls[0].setValue(r.steps[i]);
    }
  }

  ngOnInit() {
  }

  get ingredients() {
    return this.recipeForm.get('ingredients') as FormArray;
  }

  addIngredient() {
    this.ingredients.push(
    this.fb.group({
      name: [''],
      amount: ['', Validators.pattern('^[0-9]*(\.[0-9]*)?$')],
      unit: ['']
      })
  );
  }

  rmIngredient(i) {
    this.ingredients.removeAt(i);
  }

  get steps() {
    return this.recipeForm.get('steps') as FormArray;
  }

  addStep() {
    this.steps.push(
    this.fb.group({
      instruction: [''],
      timer: ['', Validators.pattern('^[0-9]*$')]
      })
  );
  }

  rmStep(i) {
    this.steps.removeAt(i);
  }

  onSubmit() {
    var formData = this.recipeForm.value;

    var ingredients = []
    var i;
    for  (i = 0; i < formData.ingredients.length; i++) {
    var tmp_amount = parseFloat(formData.ingredients[0].amount)
      ingredients.push(new Ingredient(formData.ingredients[0].name,
                                      (isNaN(tmp_amount) ? 0 : tmp_amount),
                                      formData.ingredients[0].unit,
                                      ""
                       ));
    }

    var steps = []
    for  (i = 0; i < formData.steps.length; i++) {
      var tmp_timer = parseInt(formData.steps[0].timer)
      steps.push(new Step(formData.steps[0].instruction,
                          (isNaN(tmp_timer) ? 0 : tmp_timer)
                       ));
    }

    console.log(steps);

    var servingsTmp = parseFloat(formData.servingSize)
    var cookTimeTmp = parseInt(formData.cookTime)

    var recipe = new Recipe (this.baseRecipe.id,                    //id
                             formData.recipeName,  //name
                             formData.desc,        //description
                             ingredients,          //ingredients
                             steps,                //steps
                             (isNaN(servingsTmp) ? 0 :servingsTmp), //servingSize
                             (isNaN(cookTimeTmp) ? 0 :cookTimeTmp),    //cookTime
                             0,                    //timesCooked
                             0,                    //rating
                             formData.tags.split(',').filter(word => !(word==="")),        //tags
                             formData.photos.split(',').filter(word => !(word===""))       //photos
                             );
    console.log(recipe)
    this.restService.updateRecipe(recipe).subscribe()
    this.router.navigate(['/']);
  }

  onCancel() {
    this.router.navigate(['/']);
  }
}