mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
ingredient from string API
This commit is contained in:
@@ -2098,4 +2098,13 @@ def ingredient_from_string(request):
|
|||||||
ingredient_parser = IngredientParser(request, False)
|
ingredient_parser = IngredientParser(request, False)
|
||||||
amount, unit, food, note = ingredient_parser.parse(text)
|
amount, unit, food, note = ingredient_parser.parse(text)
|
||||||
|
|
||||||
return JsonResponse({'amount': amount, 'unit': unit, 'food': food, 'note': note}, status=200)
|
ingredient = {'amount': amount, 'unit': None, 'food': None, 'note': note}
|
||||||
|
if food:
|
||||||
|
food, created = Food.objects.get_or_create(space=request.space, name=food)
|
||||||
|
ingredient['food'] = {'name': food.name, 'id': food.id}
|
||||||
|
|
||||||
|
if unit:
|
||||||
|
unit, created = Unit.objects.get_or_create(space=request.space, name=unit)
|
||||||
|
ingredient['unit'] = {'name': unit.name, 'id': unit.id}
|
||||||
|
|
||||||
|
return JsonResponse(ingredient, status=200)
|
||||||
|
|||||||
@@ -269,8 +269,8 @@ function addIngredient() {
|
|||||||
api.apiIngredientFromStringCreate({ingredientString: {text: ingredientInput.value} as IngredientString}).then(r => {
|
api.apiIngredientFromStringCreate({ingredientString: {text: ingredientInput.value} as IngredientString}).then(r => {
|
||||||
useShoppingStore().createObject({
|
useShoppingStore().createObject({
|
||||||
amount: Math.max(r.amount, 1),
|
amount: Math.max(r.amount, 1),
|
||||||
unit: (r.unit != null) ? {name: r.unit} as Unit : null,
|
unit: r.unit,
|
||||||
food: {name: r.food} as Food,
|
food: r.food,
|
||||||
} as ShoppingListEntry, true)
|
} as ShoppingListEntry, true)
|
||||||
ingredientInput.value = ''
|
ingredientInput.value = ''
|
||||||
|
|
||||||
|
|||||||
@@ -110,7 +110,7 @@
|
|||||||
<v-textarea v-model="ingredientTextInput"></v-textarea>
|
<v-textarea v-model="ingredientTextInput"></v-textarea>
|
||||||
</v-card-text>
|
</v-card-text>
|
||||||
<v-card-actions>
|
<v-card-actions>
|
||||||
<v-btn @click="parseAndInsertIngredients()" color="save">{{$t('Add')}}</v-btn>
|
<v-btn @click="parseAndInsertIngredients()" color="save">{{ $t('Add') }}</v-btn>
|
||||||
</v-card-actions>
|
</v-card-actions>
|
||||||
</v-card>
|
</v-card>
|
||||||
</v-dialog>
|
</v-dialog>
|
||||||
@@ -118,7 +118,7 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {nextTick, ref} from 'vue'
|
import {nextTick, ref} from 'vue'
|
||||||
import {Ingredient, Step} from "@/openapi";
|
import {ApiApi, Food, Ingredient, ParsedIngredient, Step} from "@/openapi";
|
||||||
import StepMarkdownEditor from "@/components/inputs/StepMarkdownEditor.vue";
|
import StepMarkdownEditor from "@/components/inputs/StepMarkdownEditor.vue";
|
||||||
import {VNumberInput} from 'vuetify/labs/VNumberInput' //TODO remove once component is out of labs
|
import {VNumberInput} from 'vuetify/labs/VNumberInput' //TODO remove once component is out of labs
|
||||||
import IngredientsTableRow from "@/components/display/IngredientsTableRow.vue";
|
import IngredientsTableRow from "@/components/display/IngredientsTableRow.vue";
|
||||||
@@ -156,8 +156,27 @@ function sortIngredients() {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function parseAndInsertIngredients(){
|
function parseAndInsertIngredients() {
|
||||||
let ingredientStrings = ingredientTextInput.value.split(/\r?\n/)
|
let api = new ApiApi()
|
||||||
|
let promises: Promise<ParsedIngredient>[] = []
|
||||||
|
let ingredientList = ingredientTextInput.value.split(/\r?\n/)
|
||||||
|
ingredientList.forEach(ingredientString => {
|
||||||
|
if (ingredientString.trim() != "") {
|
||||||
|
promises.push(api.apiIngredientFromStringCreate({ingredientString: {text: ingredientString}}))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
Promise.allSettled(promises).then(r => {
|
||||||
|
r.forEach(i => {
|
||||||
|
step.value.ingredients.push({
|
||||||
|
amount: i.value.amount,
|
||||||
|
food: i.value.food,
|
||||||
|
unit: i.value.unit,
|
||||||
|
note: i.value.note
|
||||||
|
} as Ingredient)
|
||||||
|
})
|
||||||
|
ingredientTextInput.value = ""
|
||||||
|
dialogIngredientParser.value = false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user