2
0

feat(api): Add list results endpoint

This commit is contained in:
Baptiste Arnaud
2022-02-21 15:51:40 +01:00
parent e3704f6dd9
commit 9dfcb30365
7 changed files with 140 additions and 5 deletions

View File

@ -53,6 +53,7 @@ type HeaderCell = {
Header: JSX.Element
accessor: string
}
export const parseSubmissionsColumns = (
typebot: PublicTypebot
): HeaderCell[] => {

View File

@ -1,9 +1,9 @@
import { Result, VariableWithValue } from 'models'
import { ResultWithAnswers, Typebot, VariableWithValue } from 'models'
import useSWRInfinite from 'swr/infinite'
import { fetcher } from './utils'
import { stringify } from 'qs'
import { Answer } from 'db'
import { isDefined, sendRequest } from 'utils'
import { byId, isDefined, sendRequest } from 'utils'
const paginationLimit = 50
@ -21,7 +21,6 @@ const getKey = (
}&limit=${paginationLimit}`
}
export type ResultWithAnswers = Result & { answers: Answer[] }
export const useResults = ({
typebotId,
onError,
@ -113,3 +112,28 @@ export const convertResultsToTableData = (results?: ResultWithAnswers[]) =>
return { ...o, [variable.id]: variable.value }
}, {}),
}))
export const parseAnswers = (
result: ResultWithAnswers,
{ blocks, variables }: Pick<Typebot, 'blocks' | 'variables'>
) => ({
submittedAt: 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
if (!key) return o
return {
...o,
[key]: answer.content,
}
}
const variable = answerOrVariable as VariableWithValue
if (isDefined(o[variable.id])) return o
return { ...o, [variable.id]: variable.value }
}, {}),
})