fetch all recipes in one request instead of multiple

This commit is contained in:
Michael Genson 2025-07-26 21:39:22 +00:00
commit ef88937336

View file

@ -242,28 +242,28 @@ export default defineNuxtComponent({
alert.success(i18n.t("events.event-deleted") as string); alert.success(i18n.t("events.event-deleted") as string);
}; };
async function getRecipe(recipeId: string): Promise<Recipe | null> { async function getRecipes(recipeIds: string[]): Promise<Recipe[]> {
const { data } = await api.recipes.getOne(recipeId); const qf = "id IN [" + recipeIds.map((id) => `"${id}"`).join(", ") + "]";
return data; const { data } = await api.recipes.getAll(1, -1, { queryFilter: qf })
return data?.items || [];
}; };
async function updateRecipes(events: RecipeTimelineEventOut[]) { async function updateRecipes(events: RecipeTimelineEventOut[]) {
const recipePromises: Promise<Recipe | null>[] = []; const recipeIds: string[] = [];
const seenRecipeIds: string[] = [];
events.forEach((event) => { events.forEach((event) => {
if (seenRecipeIds.includes(event.recipeId) || recipes.has(event.recipeId)) { if (recipeIds.includes(event.recipeId) || recipes.has(event.recipeId)) {
return; return;
} }
seenRecipeIds.push(event.recipeId); recipeIds.push(event.recipeId);
recipePromises.push(getRecipe(event.recipeId));
}); });
const results = await Promise.all(recipePromises); const results = await getRecipes(recipeIds);
results.forEach((result) => { results.forEach((result) => {
if (result && result.id) { if (!result?.id) {
recipes.set(result.id, result); return;
} }
recipes.set(result.id, result);
}); });
} }