From 09097d16b434d340a4dd8ffefe9c14cbfaf80cc2 Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Mon, 15 Jun 2026 19:29:50 +0200 Subject: [PATCH] fixed merge flavor listings --- frontend/src/api/queries/useFlavorsList.ts | 25 +++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/frontend/src/api/queries/useFlavorsList.ts b/frontend/src/api/queries/useFlavorsList.ts index 928b85c..0a23dae 100644 --- a/frontend/src/api/queries/useFlavorsList.ts +++ b/frontend/src/api/queries/useFlavorsList.ts @@ -30,16 +30,31 @@ export interface CategoryItem { slug: string; } -const unwrap = (data: T[] | { results?: T[] }): T[] => +type Paginated = { results?: T[]; next?: string | null }; + +const unwrap = (data: T[] | Paginated): T[] => Array.isArray(data) ? data : (data.results ?? []); +// Backend paginates flavors/ (50/page). Follow `next` to collect every flavor, +// otherwise consumers (e.g. the merge tool) only see the first page. +const fetchAllFlavors = async (): Promise => { + const all: FlavorListItem[] = []; + let url: string | null = 'flavors/'; + while (url) { + const res: { data: FlavorListItem[] | Paginated } = + await api.get>(url); + all.push(...unwrap(res.data)); + const next = Array.isArray(res.data) ? null : (res.data.next ?? null); + // `next` is an absolute URL; api baseURL already covers the prefix, so use the relative tail. + url = next ? next.replace(/^.*\/api\//, '') : null; + } + return all; +}; + export const useFlavorsList = () => useQuery({ queryKey: queryKeys.flavors({ scope: 'all' }), - queryFn: async () => { - const res = await api.get('flavors/'); - return unwrap(res.data); - }, + queryFn: fetchAllFlavors, }); export const useCategories = () =>