2
0

fix(results): 🐛 Export header valid name

This commit is contained in:
Baptiste Arnaud
2022-03-01 14:01:44 +01:00
parent 205c347720
commit 71b2b84cdf
3 changed files with 43 additions and 26 deletions

View File

@@ -12,6 +12,7 @@ import {
import { unparse } from 'papaparse' import { unparse } from 'papaparse'
import { UnlockProPlanInfo } from 'components/shared/Info' import { UnlockProPlanInfo } from 'components/shared/Info'
import { LogsModal } from './LogsModal' import { LogsModal } from './LogsModal'
import { useTypebot } from 'contexts/TypebotContext'
type Props = { type Props = {
typebotId: string typebotId: string
@@ -25,6 +26,7 @@ export const SubmissionsContent = ({
totalHiddenResults, totalHiddenResults,
onDeleteResults, onDeleteResults,
}: Props) => { }: Props) => {
const { publishedTypebot } = useTypebot()
const [selectedIndices, setSelectedIndices] = useState<number[]>([]) const [selectedIndices, setSelectedIndices] = useState<number[]>([])
const [isDeleteLoading, setIsDeleteLoading] = useState(false) const [isDeleteLoading, setIsDeleteLoading] = useState(false)
const [isExportLoading, setIsExportLoading] = useState(false) const [isExportLoading, setIsExportLoading] = useState(false)
@@ -100,13 +102,17 @@ export const SubmissionsContent = ({
} }
const getAllTableData = async () => { const getAllTableData = async () => {
if (!publishedTypebot) return []
const { data, error } = await getAllResults(typebotId) const { data, error } = await getAllResults(typebotId)
if (error) toast({ description: error.message, title: error.name }) if (error) toast({ description: error.message, title: error.name })
return convertResultsToTableData(data?.results) return convertResultsToTableData(publishedTypebot)(data?.results)
} }
const tableData: { [key: string]: string }[] = useMemo( const tableData: { [key: string]: string }[] = useMemo(
() => convertResultsToTableData(results), () =>
publishedTypebot
? convertResultsToTableData(publishedTypebot)(results)
: [],
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[results] [results]
) )

View File

@@ -76,7 +76,7 @@ export const parseSubmissionsColumns = (
<Text>Submitted at</Text> <Text>Submitted at</Text>
</HStack> </HStack>
), ),
accessor: 'createdAt', accessor: 'Submitted at',
}, },
...parsedBlocks, ...parsedBlocks,
...parseVariablesHeaders(typebot, parsedBlocks), ...parseVariablesHeaders(typebot, parsedBlocks),
@@ -91,7 +91,11 @@ const parseBlocksHeaders = (typebot: PublicTypebot) =>
if ( if (
!inputStep || !inputStep ||
!isInputStep(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 return headers
const matchedVariableName = const matchedVariableName =
@@ -113,7 +117,7 @@ const parseBlocksHeaders = (typebot: PublicTypebot) =>
<Text>{matchedVariableName ?? block.title}</Text> <Text>{matchedVariableName ?? block.title}</Text>
</HStack> </HStack>
), ),
accessor: inputStep.options.variableId ?? block.id, accessor: matchedVariableName ?? block.title,
}, },
] ]
}, []) }, [])
@@ -126,7 +130,7 @@ const parseVariablesHeaders = (
}[] }[]
) => ) =>
typebot.variables.reduce<HeaderCell[]>((headers, v) => { 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 [ return [
...headers, ...headers,
{ {
@@ -136,7 +140,7 @@ const parseVariablesHeaders = (
<Text>{v.name}</Text> <Text>{v.name}</Text>
</HStack> </HStack>
), ),
accessor: v.id, accessor: v.name,
}, },
] ]
}, []) }, [])

View File

@@ -1,8 +1,8 @@
import { ResultWithAnswers, VariableWithValue } from 'models' import { PublicTypebot, ResultWithAnswers, VariableWithValue } from 'models'
import useSWRInfinite from 'swr/infinite' import useSWRInfinite from 'swr/infinite'
import { stringify } from 'qs' import { stringify } from 'qs'
import { Answer } from 'db' import { Answer } from 'db'
import { isDefined, sendRequest } from 'utils' import { byId, isDefined, sendRequest } from 'utils'
import { fetcher } from 'services/utils' import { fetcher } from 'services/utils'
const paginationLimit = 50 const paginationLimit = 50
@@ -95,21 +95,28 @@ export const parseDateToReadable = (dateStr: string): string => {
) )
} }
export const convertResultsToTableData = (results?: ResultWithAnswers[]) => export const convertResultsToTableData =
(results ?? []).map((result) => ({ ({ variables, blocks }: PublicTypebot) =>
createdAt: parseDateToReadable(result.createdAt), (results: ResultWithAnswers[] | undefined) =>
...[...result.answers, ...result.prefilledVariables].reduce<{ (results ?? []).map((result) => ({
[key: string]: string 'Submitted at': parseDateToReadable(result.createdAt),
}>((o, answerOrVariable) => { ...[...result.answers, ...result.prefilledVariables].reduce<{
if ('blockId' in answerOrVariable) { [key: string]: string
const answer = answerOrVariable as Answer }>((o, answerOrVariable) => {
return { if ('blockId' in answerOrVariable) {
...o, const answer = answerOrVariable as Answer
[answer.variableId ?? answer.blockId]: answer.content, 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
const variable = answerOrVariable as VariableWithValue if (isDefined(o[variable.id])) return o
if (isDefined(o[variable.id])) return o const key = variables.find(byId(variable.id))?.name ?? ''
return { ...o, [variable.id]: variable.value } return { ...o, [key]: variable.value }
}, {}), }, {}),
})) }))