From 281fddc8efa39f3ca8a29583689ef264c7cc41dc Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 14 Apr 2022 16:33:13 -0700 Subject: [PATCH] =?UTF-8?q?fix(results):=20=F0=9F=90=9B=20Exporting=20many?= =?UTF-8?q?=20results?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../layouts/results/SubmissionContent.tsx | 5 ++--- apps/builder/services/typebots/results.tsx | 21 ++++++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/apps/builder/layouts/results/SubmissionContent.tsx b/apps/builder/layouts/results/SubmissionContent.tsx index f054687bf..9a9985506 100644 --- a/apps/builder/layouts/results/SubmissionContent.tsx +++ b/apps/builder/layouts/results/SubmissionContent.tsx @@ -126,9 +126,8 @@ export const SubmissionsContent = ({ const getAllTableData = async () => { if (!publishedTypebot) return [] - const { data, error } = await getAllResults(typebotId) - if (error) toast({ description: error.message, title: error.name }) - return convertResultsToTableData(data?.results, resultHeader) + const results = await getAllResults(typebotId) + return convertResultsToTableData(results, resultHeader) } const tableData: { [key: string]: string }[] = useMemo( diff --git a/apps/builder/services/typebots/results.tsx b/apps/builder/services/typebots/results.tsx index b56b07f51..f679e26fd 100644 --- a/apps/builder/services/typebots/results.tsx +++ b/apps/builder/services/typebots/results.tsx @@ -80,11 +80,22 @@ export const deleteAllResults = async (typebotId: string) => method: 'DELETE', }) -export const getAllResults = async (typebotId: string) => - sendRequest<{ results: ResultWithAnswers[] }>({ - url: `/api/typebots/${typebotId}/results`, - method: 'GET', - }) +export const getAllResults = async (typebotId: string) => { + const results = [] + let hasMore = true + let lastResultId: string | undefined = undefined + do { + const query = stringify({ limit: 200, lastResultId }) + const { data } = await sendRequest<{ results: ResultWithAnswers[] }>({ + url: `/api/typebots/${typebotId}/results?${query}`, + method: 'GET', + }) + results.push(...(data?.results ?? [])) + lastResultId = results[results.length - 1]?.id as string | undefined + if (data?.results.length === 0) hasMore = false + } while (hasMore) + return results +} export const parseDateToReadable = (dateStr: string): string => { const date = new Date(dateStr)