diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 492a2904e..274004fa6 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -1,4 +1,5 @@ import re +from random import random from django.http import JsonResponse from django.utils.dateparse import parse_duration @@ -58,7 +59,7 @@ def find_recipe_json(ld_json, url): ingredient = " ".join(ingredient_split) if ingredient: - ingredients.append({'amount': amount, 'unit': {'text': unit, 'id': 'null'}, 'ingredient': {'text': ingredient, 'id': 'null'}, 'original': x}) + ingredients.append({'amount': amount, 'unit': {'text': unit, 'id': round(random() * 1000)}, 'ingredient': {'text': ingredient, 'id': round(random() * 1000)}, 'original': x}) ld_json['recipeIngredient'] = ingredients else: diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index b6d17e34c..a71d3c874 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -82,12 +82,14 @@
- +
+ +
+ ref="ingredient" + v-model="i.ingredient" + :options="ingredients" + :taggable="true" + @tag="addIngredientType" + placeholder="{% trans 'Select one' %}" + tag-placeholder="{% trans 'Select' %}" + :close-on-select="true" + :clear-on-select="true" + :allow-empty="false" + :preserve-search="true" + label="text" + :id="'ingredient_' + index" + track-by="id" + :multiple="false" + :loading="ingredients_loading" + @search-change="searchIngredients" + @open="openIngredientSelect"> @@ -293,14 +295,6 @@ console.log(err) }) }, - addTag(newTag) { - const tag = { - name: newTag, - code: newTag.substring(0, 2) + Math.floor((Math.random() * 10000000)) - } - this.options.push(tag) - this.value.push(tag) - }, importRecipe: function () { this.$set(this.recipe_data, 'all_keywords', this.all_keywords) this.$http.post(`{% url 'data_import_url' %}`, this.recipe_data).then((response) => { @@ -314,22 +308,22 @@ }, addIngredient: function (i) { this.recipe_data.recipeIngredient.push({ - unit: {id: Math.random() * 100, text: '{{ request.user.userpreference.default_unit }}'}, + unit: {id: Math.random() * 1000, text: '{{ request.user.userpreference.default_unit }}'}, amount: 0, - ingredient: {id: Math.random() * 100, text: ''} + ingredient: {id: Math.random() * 1000, text: ''} }) }, addIngredientType: function (tag, index) { index = index.replace('ingredient_', '') let new_ingredient = this.recipe_data.recipeIngredient[index] - new_ingredient.ingredient = {'id': Math.random() * 100, 'text': tag} + new_ingredient.ingredient = {'id': Math.random() * 1000, 'text': tag} this.ingredients.push(new_ingredient.ingredient) this.recipe_data.recipeIngredient[index] = new_ingredient }, addUnitType: function (tag, index) { index = index.replace('unit_', '') let new_unit = this.recipe_data.recipeIngredient[index] - new_unit.unit = {'id': Math.random() * 100, 'text': tag} + new_unit.unit = {'id': Math.random() * 1000, 'text': tag} this.units.push(new_unit.unit) this.recipe_data.recipeIngredient[index] = new_unit }, @@ -356,7 +350,8 @@ this.units = response.data.results; if (this.recipe_data !== undefined) { for (let x of Array.from(this.recipe_data.recipeIngredient)) { - if (x.ingredient.text !== '') { + if (x.unit.text !== '') { + this.units = this.units.filter(item => item.text !== x.unit.text) this.units.push(x.unit) } } @@ -369,7 +364,16 @@ searchIngredients: function (query) { this.ingredients_loading = true this.$http.get("{% url 'dal_ingredient' %}" + '?q=' + query).then((response) => { - this.ingredients = this.ingredients.concat(response.data.results); + this.ingredients = response.data.results + if (this.recipe_data !== undefined) { + for (let x of Array.from(this.recipe_data.recipeIngredient)) { + if (x.ingredient.text !== '') { + this.ingredients = this.ingredients.filter(item => item.text !== x.ingredient.text) + this.ingredients.push(x.ingredient) + } + } + } + this.ingredients_loading = false }).catch((err) => { console.log(err)