|
1 | 1 | import Vue, {ComponentOptions} from 'vue'; |
2 | 2 | import {RecipeEntity, RecipeError} from '../../../model'; |
3 | 3 | import {recipeAPI} from '../../../api/recipe'; |
4 | | -import {EditRecipePage} from './page'; |
5 | | -import {router} from '../../../router'; |
6 | 4 | import {editFormValidation} from './validations/editFormValidation'; |
| 5 | +import {EditRecipePage} from './page'; |
7 | 6 |
|
8 | | -interface State extends Vue { |
| 7 | +interface EditRecipeContainerOptions extends Vue { |
9 | 8 | recipe: RecipeEntity; |
10 | 9 | recipeError: RecipeError; |
11 | | - updateRecipe: (field, value) => void; |
| 10 | + updateRecipe: (name) => void; |
12 | 11 | addIngredient: (ingredient) => void; |
13 | 12 | removeIngredient: (ingredient) => void; |
| 13 | + validateRecipeField: (field, value) => void; |
| 14 | + updateRecipeError: (field, result) => void; |
14 | 15 | save: () => void; |
15 | 16 | } |
16 | 17 |
|
@@ -41,60 +42,52 @@ export const EditRecipeContainer = Vue.extend({ |
41 | 42 | recipeAPI.fetchRecipeById(id) |
42 | 43 | .then((recipe) => { |
43 | 44 | this.recipe = recipe; |
44 | | - }) |
45 | | - .catch((error) => console.log(error)); |
| 45 | + }); |
46 | 46 | }, |
47 | 47 | methods: { |
48 | | - updateRecipe: function(field: string, value) { |
| 48 | + updateRecipe: function(name) { |
49 | 49 | this.recipe = { |
50 | 50 | ...this.recipe, |
51 | | - [field]: value, |
| 51 | + name, |
52 | 52 | }; |
53 | 53 |
|
54 | | - editFormValidation.validateField(this.recipe, field, value) |
55 | | - .then((result) => { |
56 | | - this.recipeError = { |
57 | | - ...this.recipeError, |
58 | | - [field]: result, |
59 | | - }; |
60 | | - }) |
61 | | - .catch((error) => console.log(error)); |
| 54 | + this.validateRecipeField('name', name); |
62 | 55 | }, |
63 | 56 | addIngredient: function(ingredient: string) { |
64 | 57 | this.recipe = { |
65 | 58 | ...this.recipe, |
66 | 59 | ingredients: [...this.recipe.ingredients, ingredient], |
67 | | - } |
| 60 | + }; |
| 61 | + |
| 62 | + this.validateRecipeField('ingredients', this.recipe.ingredients); |
68 | 63 | }, |
69 | 64 | removeIngredient: function(ingredient: string) { |
70 | 65 | this.recipe = { |
71 | 66 | ...this.recipe, |
72 | 67 | ingredients: this.recipe.ingredients.filter((i) => { |
73 | 68 | return i !== ingredient; |
74 | 69 | }), |
75 | | - } |
| 70 | + }; |
| 71 | + |
| 72 | + this.validateRecipeField('ingredients', this.recipe.ingredients); |
| 73 | + }, |
| 74 | + validateRecipeField: function(field, value) { |
| 75 | + editFormValidation.validateField(this.recipe, field, value) |
| 76 | + .then((result) => this.updateRecipeError(field, result)); |
| 77 | + }, |
| 78 | + updateRecipeError: function(field, result) { |
| 79 | + this.recipeError = { |
| 80 | + ...this.recipeError, |
| 81 | + [field]: result, |
| 82 | + }; |
76 | 83 | }, |
77 | 84 | save: function() { |
78 | 85 | editFormValidation.validateForm(this.recipe) |
79 | 86 | .then((result) => { |
80 | | - result.fieldErrors.map((error) => { |
81 | | - this.recipeError = { |
82 | | - ...this.recipeError, |
83 | | - [error.key]: error, |
84 | | - } |
85 | | - }); |
86 | | - |
87 | | - if(result.succeeded) { |
88 | | - recipeAPI.save(this.recipe) |
89 | | - .then((message) => { |
90 | | - console.log(message); |
91 | | - router.back(); |
92 | | - }) |
93 | | - .catch((error) => console.log(error)); |
94 | | - } |
95 | | - }) |
96 | | - .catch((error) => console.log(error)); |
| 87 | + result.fieldErrors |
| 88 | + .map((error) => this.updateRecipeError(error.key, error)); |
| 89 | + }); |
97 | 90 | }, |
98 | 91 | } |
99 | | -} as ComponentOptions<State>); |
| 92 | +} as ComponentOptions<EditRecipeContainerOptions>); |
100 | 93 |
|
0 commit comments