Skip to content

Commit bd31d06

Browse files
committed
Remove functionality to keep sample as simple as possible
1 parent 2e0d1fc commit bd31d06

3 files changed

Lines changed: 31 additions & 57 deletions

File tree

samples/vuejs/typescript/00 Custom Validator/src/api/recipe/index.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ const fetchRecipeById = (id: number): Promise<RecipeEntity> => {
88
return Promise.resolve(recipe);
99
}
1010

11-
const save = (recipe: RecipeEntity): Promise<string> => {
12-
const index = recipes.findIndex(r => r.id === recipe.id);
13-
14-
return index >= 0 ?
15-
saveRecipeByIndex(index, recipe) :
16-
Promise.reject<string>('Something was wrong saving recipe :(');
17-
}
18-
19-
const saveRecipeByIndex = (index, recipe) => {
20-
recipes = [
21-
...recipes.slice(0, index),
22-
recipe,
23-
...recipes.slice(index + 1),
24-
];
25-
26-
return Promise.resolve('Save recipe sucess');
27-
}
28-
2911
export const recipeAPI = {
3012
fetchRecipeById,
31-
save,
3213
}

samples/vuejs/typescript/00 Custom Validator/src/pages/recipe/edit/components/form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const classNames: any = require('./formStyles');
1010
interface FormComponentProperties extends Vue {
1111
recipe: RecipeEntity;
1212
recipeError: RecipeError;
13-
updateRecipe: (field, value) => void;
13+
updateRecipe: (name) => void;
1414
addIngredient: (ingredient) => void;
1515
removeIngredient: (ingredient) => void;
1616
save: () => void;
@@ -53,7 +53,7 @@ export const FormComponent = Vue.extend({
5353
label="Name"
5454
name="name"
5555
value={this.recipe.name}
56-
inputHandler={(e) => { this.updateRecipe('name', e.target.value)}}
56+
inputHandler={(e) => { this.updateRecipe(e.target.value)}}
5757
/>
5858
</ValidationComponent>
5959
</div>
Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import Vue, {ComponentOptions} from 'vue';
22
import {RecipeEntity, RecipeError} from '../../../model';
33
import {recipeAPI} from '../../../api/recipe';
4-
import {EditRecipePage} from './page';
5-
import {router} from '../../../router';
64
import {editFormValidation} from './validations/editFormValidation';
5+
import {EditRecipePage} from './page';
76

8-
interface State extends Vue {
7+
interface EditRecipeContainerOptions extends Vue {
98
recipe: RecipeEntity;
109
recipeError: RecipeError;
11-
updateRecipe: (field, value) => void;
10+
updateRecipe: (name) => void;
1211
addIngredient: (ingredient) => void;
1312
removeIngredient: (ingredient) => void;
13+
validateRecipeField: (field, value) => void;
14+
updateRecipeError: (field, result) => void;
1415
save: () => void;
1516
}
1617

@@ -41,60 +42,52 @@ export const EditRecipeContainer = Vue.extend({
4142
recipeAPI.fetchRecipeById(id)
4243
.then((recipe) => {
4344
this.recipe = recipe;
44-
})
45-
.catch((error) => console.log(error));
45+
});
4646
},
4747
methods: {
48-
updateRecipe: function(field: string, value) {
48+
updateRecipe: function(name) {
4949
this.recipe = {
5050
...this.recipe,
51-
[field]: value,
51+
name,
5252
};
5353

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);
6255
},
6356
addIngredient: function(ingredient: string) {
6457
this.recipe = {
6558
...this.recipe,
6659
ingredients: [...this.recipe.ingredients, ingredient],
67-
}
60+
};
61+
62+
this.validateRecipeField('ingredients', this.recipe.ingredients);
6863
},
6964
removeIngredient: function(ingredient: string) {
7065
this.recipe = {
7166
...this.recipe,
7267
ingredients: this.recipe.ingredients.filter((i) => {
7368
return i !== ingredient;
7469
}),
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+
};
7683
},
7784
save: function() {
7885
editFormValidation.validateForm(this.recipe)
7986
.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+
});
9790
},
9891
}
99-
} as ComponentOptions<State>);
92+
} as ComponentOptions<EditRecipeContainerOptions>);
10093

0 commit comments

Comments
 (0)