fix(results): 🐛 Collect prefilled variables in db
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import { Result } from 'models'
|
||||
import { Result, VariableWithValue } from 'models'
|
||||
import useSWRInfinite from 'swr/infinite'
|
||||
import { fetcher } from './utils'
|
||||
import { stringify } from 'qs'
|
||||
import { Answer } from 'db'
|
||||
import { sendRequest } from 'utils'
|
||||
import { isDefined, sendRequest } from 'utils'
|
||||
|
||||
const paginationLimit = 50
|
||||
|
||||
@ -98,8 +98,18 @@ export const parseDateToReadable = (dateStr: string): string => {
|
||||
export const convertResultsToTableData = (results?: ResultWithAnswers[]) =>
|
||||
(results ?? []).map((result) => ({
|
||||
createdAt: parseDateToReadable(result.createdAt),
|
||||
...result.answers.reduce(
|
||||
(o, answer) => ({ ...o, [answer.blockId]: answer.content }),
|
||||
{}
|
||||
),
|
||||
...[...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,
|
||||
}
|
||||
}
|
||||
const variable = answerOrVariable as VariableWithValue
|
||||
if (isDefined(o[variable.id])) return o
|
||||
return { ...o, [variable.id]: variable.value }
|
||||
}, {}),
|
||||
}))
|
||||
|
Reference in New Issue
Block a user