From babbed323c746336480dae973f223461e7ff798f Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Thu, 11 Jun 2026 03:15:40 -0700 Subject: [PATCH] fixed question type definition listing drop down --- src/api/questionTypeDefinitions.api.ts | 52 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/api/questionTypeDefinitions.api.ts b/src/api/questionTypeDefinitions.api.ts index 699db6c..b596952 100644 --- a/src/api/questionTypeDefinitions.api.ts +++ b/src/api/questionTypeDefinitions.api.ts @@ -273,6 +273,9 @@ export interface QuestionTypeDefinitionsListResult { total_count?: number } +/** Page size when auto-fetching every definition (dropdowns, editors). */ +const DEFINITIONS_FETCH_ALL_PAGE_SIZE = 100 + function parseListTotalCount(body: unknown): number | undefined { if (!body || typeof body !== "object" || Array.isArray(body)) return undefined const o = body as Record @@ -322,7 +325,7 @@ export function parseDefinitionsList(payload: unknown): QuestionTypeDefinition[] return [] } -export async function getQuestionTypeDefinitions( +async function fetchQuestionTypeDefinitionsPage( params?: QuestionTypeDefinitionsListParams, ): Promise { const res = await http.get>("/questions/type-definitions", { params }) @@ -332,6 +335,53 @@ export async function getQuestionTypeDefinitions( return total_count != null ? { definitions, total_count } : { definitions } } +/** + * Lists question type definitions. When `limit` / `offset` are omitted, fetches every page + * so dropdowns receive the full catalog (the API defaults to a capped page size). + */ +export async function getQuestionTypeDefinitions( + params?: QuestionTypeDefinitionsListParams, +): Promise { + const hasExplicitPagination = params?.limit !== undefined || params?.offset !== undefined + if (hasExplicitPagination) { + return fetchQuestionTypeDefinitionsPage(params) + } + + const allDefinitions: QuestionTypeDefinition[] = [] + const seenIds = new Set() + let offset = 0 + let total_count: number | undefined + + while (true) { + const page = await fetchQuestionTypeDefinitionsPage({ + ...params, + limit: DEFINITIONS_FETCH_ALL_PAGE_SIZE, + offset, + }) + + if (total_count === undefined && page.total_count != null) { + total_count = page.total_count + } + + for (const def of page.definitions) { + if (seenIds.has(def.id)) continue + seenIds.add(def.id) + allDefinitions.push(def) + } + + if (page.definitions.length === 0) break + if (total_count != null && allDefinitions.length >= total_count) break + if (page.definitions.length < DEFINITIONS_FETCH_ALL_PAGE_SIZE) break + + offset += page.definitions.length + } + + return { + definitions: allDefinitions, + total_count: total_count ?? allDefinitions.length, + } +} + /** * GET /questions/type-definitions/:id *