From 71b2b84cdf609da9ac391526c7fb43965a2ac111 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Tue, 1 Mar 2022 14:01:44 +0100 Subject: [PATCH] =?UTF-8?q?fix(results):=20=F0=9F=90=9B=20Export=20header?= =?UTF-8?q?=20valid=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../layouts/results/SubmissionContent.tsx | 10 ++++- apps/builder/services/publicTypebot.tsx | 14 +++--- apps/builder/services/typebots/results.ts | 45 +++++++++++-------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/apps/builder/layouts/results/SubmissionContent.tsx b/apps/builder/layouts/results/SubmissionContent.tsx index b6557896c1b..8ed73db0871 100644 --- a/apps/builder/layouts/results/SubmissionContent.tsx +++ b/apps/builder/layouts/results/SubmissionContent.tsx @@ -12,6 +12,7 @@ import { import { unparse } from 'papaparse' import { UnlockProPlanInfo } from 'components/shared/Info' import { LogsModal } from './LogsModal' +import { useTypebot } from 'contexts/TypebotContext' type Props = { typebotId: string @@ -25,6 +26,7 @@ export const SubmissionsContent = ({ totalHiddenResults, onDeleteResults, }: Props) => { + const { publishedTypebot } = useTypebot() const [selectedIndices, setSelectedIndices] = useState([]) const [isDeleteLoading, setIsDeleteLoading] = useState(false) const [isExportLoading, setIsExportLoading] = useState(false) @@ -100,13 +102,17 @@ 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) + return convertResultsToTableData(publishedTypebot)(data?.results) } const tableData: { [key: string]: string }[] = useMemo( - () => convertResultsToTableData(results), + () => + publishedTypebot + ? convertResultsToTableData(publishedTypebot)(results) + : [], // eslint-disable-next-line react-hooks/exhaustive-deps [results] ) diff --git a/apps/builder/services/publicTypebot.tsx b/apps/builder/services/publicTypebot.tsx index 4e410c679c5..95780bd1edb 100644 --- a/apps/builder/services/publicTypebot.tsx +++ b/apps/builder/services/publicTypebot.tsx @@ -76,7 +76,7 @@ export const parseSubmissionsColumns = ( Submitted at ), - accessor: 'createdAt', + accessor: 'Submitted at', }, ...parsedBlocks, ...parseVariablesHeaders(typebot, parsedBlocks), @@ -91,7 +91,11 @@ const parseBlocksHeaders = (typebot: PublicTypebot) => if ( !inputStep || !isInputStep(inputStep) || - headers.find((h) => h.accessor === inputStep.options.variableId) + headers.find( + (h) => + h.accessor === + typebot.variables.find(byId(inputStep.options.variableId))?.name + ) ) return headers const matchedVariableName = @@ -113,7 +117,7 @@ const parseBlocksHeaders = (typebot: PublicTypebot) => {matchedVariableName ?? block.title} ), - accessor: inputStep.options.variableId ?? block.id, + accessor: matchedVariableName ?? block.title, }, ] }, []) @@ -126,7 +130,7 @@ const parseVariablesHeaders = ( }[] ) => typebot.variables.reduce((headers, v) => { - if (parsedBlocks.find((b) => b.accessor === v.id)) return headers + if (parsedBlocks.find((b) => b.accessor === v.name)) return headers return [ ...headers, { @@ -136,7 +140,7 @@ const parseVariablesHeaders = ( {v.name} ), - accessor: v.id, + accessor: v.name, }, ] }, []) diff --git a/apps/builder/services/typebots/results.ts b/apps/builder/services/typebots/results.ts index 6cd3d7924ba..b0e46d40bf5 100644 --- a/apps/builder/services/typebots/results.ts +++ b/apps/builder/services/typebots/results.ts @@ -1,8 +1,8 @@ -import { ResultWithAnswers, VariableWithValue } from 'models' +import { PublicTypebot, ResultWithAnswers, VariableWithValue } from 'models' import useSWRInfinite from 'swr/infinite' import { stringify } from 'qs' import { Answer } from 'db' -import { isDefined, sendRequest } from 'utils' +import { byId, isDefined, sendRequest } from 'utils' import { fetcher } from 'services/utils' const paginationLimit = 50 @@ -95,21 +95,28 @@ export const parseDateToReadable = (dateStr: string): string => { ) } -export const convertResultsToTableData = (results?: ResultWithAnswers[]) => - (results ?? []).map((result) => ({ - createdAt: parseDateToReadable(result.createdAt), - ...[...result.answers, ...result.prefilledVariables].reduce<{ - [key: string]: string - }>((o, answerOrVariable) => { - if ('blockId' in answerOrVariable) { - const answer = answerOrVariable as Answer - return { - ...o, - [answer.variableId ?? answer.blockId]: answer.content, +export const convertResultsToTableData = + ({ variables, blocks }: PublicTypebot) => + (results: ResultWithAnswers[] | undefined) => + (results ?? []).map((result) => ({ + 'Submitted at': parseDateToReadable(result.createdAt), + ...[...result.answers, ...result.prefilledVariables].reduce<{ + [key: string]: string + }>((o, answerOrVariable) => { + if ('blockId' in answerOrVariable) { + const answer = answerOrVariable as Answer + const key = + (answer.variableId + ? variables.find(byId(answer.variableId))?.name + : blocks.find(byId(answer.blockId))?.title) ?? '' + return { + ...o, + [key]: answer.content, + } } - } - const variable = answerOrVariable as VariableWithValue - if (isDefined(o[variable.id])) return o - return { ...o, [variable.id]: variable.value } - }, {}), - })) + const variable = answerOrVariable as VariableWithValue + if (isDefined(o[variable.id])) return o + const key = variables.find(byId(variable.id))?.name ?? '' + return { ...o, [key]: variable.value } + }, {}), + }))