fix: Bulk Add Recipes to Shopping List (#5054)

This commit is contained in:
Michael Genson 2025-02-27 07:58:40 -06:00 committed by GitHub
parent 3d1b76bcad
commit 716c85cc3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 306 additions and 77 deletions

View file

@ -138,8 +138,8 @@ import RecipeIngredientListItem from "./RecipeIngredientListItem.vue";
import { useUserApi } from "~/composables/api";
import { alert } from "~/composables/use-toast";
import { useShoppingListPreferences } from "~/composables/use-users/preferences";
import { ShoppingListSummary } from "~/lib/api/types/household";
import { Recipe, RecipeIngredient } from "~/lib/api/types/recipe";
import { RecipeIngredient, ShoppingListAddRecipeParamsBulk, ShoppingListSummary } from "~/lib/api/types/household";
import { Recipe } from "~/lib/api/types/recipe";
export interface RecipeWithScale extends Recipe {
scale: number;
@ -342,12 +342,12 @@ export default defineComponent({
}
async function addRecipesToList() {
const promises: Promise<any>[] = [];
recipeIngredientSections.value.forEach((section) => {
if (!selectedShoppingList.value) {
return;
}
if (!selectedShoppingList.value) {
return;
}
const recipeData: ShoppingListAddRecipeParamsBulk[] = [];
recipeIngredientSections.value.forEach((section) => {
const ingredients: RecipeIngredient[] = [];
section.ingredientSections.forEach((ingSection) => {
ingSection.ingredients.forEach((ing) => {
@ -361,24 +361,18 @@ export default defineComponent({
return;
}
promises.push(api.shopping.lists.addRecipe(
selectedShoppingList.value.id,
section.recipeId,
section.recipeScale,
ingredients,
));
recipeData.push(
{
recipeId: section.recipeId,
recipeIncrementQuantity: section.recipeScale,
recipeIngredients: ingredients,
}
);
});
let success = true;
const results = await Promise.allSettled(promises);
results.forEach((result) => {
if (result.status === "rejected") {
success = false;
}
})
success ? alert.success(i18n.tc("recipe.successfully-added-to-list"))
: alert.error(i18n.tc("failed-to-add-recipes-to-list"))
const { error } = await api.shopping.lists.addRecipes(selectedShoppingList.value.id, recipeData);
error ? alert.error(i18n.tc("recipe.failed-to-add-recipes-to-list"))
: alert.success(i18n.tc("recipe.successfully-added-to-list"));
state.shoppingListDialog = false;
state.shoppingListIngredientDialog = false;