Skip to content

Commit

Permalink
fix(results): 🐛 Export header valid name
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 1, 2022
1 parent 205c347 commit 71b2b84
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
10 changes: 8 additions & 2 deletions apps/builder/layouts/results/SubmissionContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +26,7 @@ export const SubmissionsContent = ({
totalHiddenResults,
onDeleteResults,
}: Props) => {
const { publishedTypebot } = useTypebot()
const [selectedIndices, setSelectedIndices] = useState<number[]>([])
const [isDeleteLoading, setIsDeleteLoading] = useState(false)
const [isExportLoading, setIsExportLoading] = useState(false)
Expand Down Expand Up @@ -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]
)
Expand Down
14 changes: 9 additions & 5 deletions apps/builder/services/publicTypebot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const parseSubmissionsColumns = (
<Text>Submitted at</Text>
</HStack>
),
accessor: 'createdAt',
accessor: 'Submitted at',
},
...parsedBlocks,
...parseVariablesHeaders(typebot, parsedBlocks),
Expand All @@ -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 =
Expand All @@ -113,7 +117,7 @@ const parseBlocksHeaders = (typebot: PublicTypebot) =>
<Text>{matchedVariableName ?? block.title}</Text>
</HStack>
),
accessor: inputStep.options.variableId ?? block.id,
accessor: matchedVariableName ?? block.title,
},
]
}, [])
Expand All @@ -126,7 +130,7 @@ const parseVariablesHeaders = (
}[]
) =>
typebot.variables.reduce<HeaderCell[]>((headers, v) => {
if (parsedBlocks.find((b) => b.accessor === v.id)) return headers
if (parsedBlocks.find((b) => b.accessor === v.name)) return headers
return [
...headers,
{
Expand All @@ -136,7 +140,7 @@ const parseVariablesHeaders = (
<Text>{v.name}</Text>
</HStack>
),
accessor: v.id,
accessor: v.name,
},
]
}, [])
45 changes: 26 additions & 19 deletions apps/builder/services/typebots/results.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 }
}, {}),
}))

2 comments on commit 71b2b84

@vercel
Copy link

@vercel vercel bot commented on 71b2b84 Mar 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 71b2b84 Mar 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.