2
0
Files
bot/apps/builder/services/results.ts

116 lines
3.0 KiB
TypeScript
Raw Normal View History

import { Result, VariableWithValue } from 'models'
import useSWRInfinite from 'swr/infinite'
import { fetcher } from './utils'
2021-12-30 10:24:16 +01:00
import { stringify } from 'qs'
import { Answer } from 'db'
import { isDefined, sendRequest } from 'utils'
2021-12-30 10:24:16 +01:00
2022-02-11 15:30:02 +01:00
const paginationLimit = 50
const getKey = (
typebotId: string,
pageIndex: number,
previousPageData: {
results: ResultWithAnswers[]
}
) => {
if (previousPageData && previousPageData.results.length === 0) return null
2022-01-04 15:50:56 +01:00
if (pageIndex === 0) return `/api/typebots/${typebotId}/results?limit=50`
return `/api/typebots/${typebotId}/results?lastResultId=${
previousPageData.results[previousPageData.results.length - 1].id
2022-02-11 15:30:02 +01:00
}&limit=${paginationLimit}`
}
2022-01-04 15:50:56 +01:00
export type ResultWithAnswers = Result & { answers: Answer[] }
2021-12-30 10:24:16 +01:00
export const useResults = ({
typebotId,
onError,
}: {
typebotId: string
onError: (error: Error) => void
}) => {
const { data, error, mutate, setSize, size } = useSWRInfinite<
{ results: ResultWithAnswers[] },
2021-12-30 10:24:16 +01:00
Error
>(
(
pageIndex: number,
previousPageData: {
results: ResultWithAnswers[]
}
) => getKey(typebotId, pageIndex, previousPageData),
fetcher,
{ revalidateAll: true }
)
2021-12-30 10:24:16 +01:00
if (error) onError(error)
return {
data,
2021-12-30 10:24:16 +01:00
isLoading: !error && !data,
mutate,
setSize,
size,
hasMore:
2022-02-11 15:30:02 +01:00
data &&
data.length > 0 &&
data[data.length - 1].results.length > 0 &&
data.length === paginationLimit,
2021-12-30 10:24:16 +01:00
}
}
2022-01-04 09:15:33 +01:00
export const deleteResults = async (typebotId: string, ids: string[]) => {
const params = stringify(
{
ids,
},
{ indices: false }
)
return sendRequest({
url: `/api/typebots/${typebotId}/results?${params}`,
method: 'DELETE',
})
}
export const deleteAllResults = async (typebotId: string) =>
sendRequest({
url: `/api/typebots/${typebotId}/results`,
method: 'DELETE',
})
2022-01-04 15:50:56 +01:00
export const getAllResults = async (typebotId: string) =>
sendRequest<{ results: ResultWithAnswers[] }>({
url: `/api/typebots/${typebotId}/results`,
method: 'GET',
})
2021-12-30 10:24:16 +01:00
export const parseDateToReadable = (dateStr: string): string => {
const date = new Date(dateStr)
return (
date.toDateString().split(' ').slice(1, 3).join(' ') +
', ' +
date.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})
)
}
2022-01-04 15:50:56 +01:00
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,
}
}
const variable = answerOrVariable as VariableWithValue
if (isDefined(o[variable.id])) return o
return { ...o, [variable.id]: variable.value }
}, {}),
2022-01-04 15:50:56 +01:00
}))