🪥 Consult submissions
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { DashboardFolder } from '.prisma/client'
|
||||
import useSWR from 'swr'
|
||||
import { fetcher, sendRequest } from './utils'
|
||||
import { stringify } from 'qs'
|
||||
|
||||
export const useFolders = ({
|
||||
parentId,
|
||||
@ -9,9 +10,7 @@ export const useFolders = ({
|
||||
parentId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const params = new URLSearchParams(
|
||||
parentId ? { parentId: parentId.toString() } : undefined
|
||||
)
|
||||
const params = stringify({ parentId })
|
||||
const { data, error, mutate } = useSWR<{ folders: DashboardFolder[] }, Error>(
|
||||
`/api/folders?${params}`,
|
||||
fetcher
|
||||
|
@ -8,6 +8,9 @@ import {
|
||||
} from 'bot-engine'
|
||||
import { sendRequest } from './utils'
|
||||
import shortId from 'short-uuid'
|
||||
import { HStack, Text } from '@chakra-ui/react'
|
||||
import { CalendarIcon } from 'assets/icons'
|
||||
import { StepIcon } from 'components/board/StepTypesList/StepIcon'
|
||||
|
||||
export const parseTypebotToPublicTypebot = (
|
||||
typebot: Typebot
|
||||
@ -44,12 +47,32 @@ export const updatePublishedTypebot = async (
|
||||
export const parseSubmissionsColumns = (
|
||||
typebot?: PublicTypebot
|
||||
): {
|
||||
Header: string
|
||||
Header: JSX.Element
|
||||
accessor: string
|
||||
}[] =>
|
||||
(typebot?.blocks ?? [])
|
||||
.filter(blockContainsInput)
|
||||
.map((block) => ({ Header: block.title, accessor: block.id }))
|
||||
}[] => [
|
||||
{
|
||||
Header: (
|
||||
<HStack>
|
||||
<CalendarIcon />
|
||||
<Text>Submitted at</Text>
|
||||
</HStack>
|
||||
),
|
||||
accessor: 'createdAt',
|
||||
},
|
||||
...(typebot?.blocks ?? []).filter(blockContainsInput).map((block) => ({
|
||||
Header: (
|
||||
<HStack>
|
||||
<StepIcon
|
||||
type={
|
||||
block.steps.find((step) => step.target)?.type ?? StepType.TEXT_INPUT
|
||||
}
|
||||
/>
|
||||
<Text>{block.title}</Text>
|
||||
</HStack>
|
||||
),
|
||||
accessor: block.id,
|
||||
})),
|
||||
]
|
||||
|
||||
const blockContainsInput = (block: Block) => block.steps.some(stepIsInput)
|
||||
|
60
apps/builder/services/results.ts
Normal file
60
apps/builder/services/results.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { Result } from 'bot-engine'
|
||||
import useSWR from 'swr'
|
||||
import { fetcher } from './utils'
|
||||
import { stringify } from 'qs'
|
||||
import { Answer } from 'db'
|
||||
|
||||
export const useResults = ({
|
||||
lastResultId,
|
||||
typebotId,
|
||||
onError,
|
||||
}: {
|
||||
lastResultId?: string
|
||||
typebotId: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const params = stringify({
|
||||
lastResultId,
|
||||
})
|
||||
const { data, error, mutate } = useSWR<
|
||||
{ results: (Result & { answers: Answer[] })[] },
|
||||
Error
|
||||
>(`/api/typebots/${typebotId}/results?${params}`, fetcher)
|
||||
if (error) onError(error)
|
||||
return {
|
||||
results: data?.results,
|
||||
isLoading: !error && !data,
|
||||
mutate,
|
||||
}
|
||||
}
|
||||
|
||||
export const useResultsCount = ({
|
||||
typebotId,
|
||||
onError,
|
||||
}: {
|
||||
typebotId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const { data, error, mutate } = useSWR<{ totalResults: number }, Error>(
|
||||
typebotId ? `/api/typebots/${typebotId}/results/count` : null,
|
||||
fetcher
|
||||
)
|
||||
if (error) onError(error)
|
||||
return {
|
||||
totalResults: data?.totalResults,
|
||||
isLoading: !error && !data,
|
||||
mutate,
|
||||
}
|
||||
}
|
||||
|
||||
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',
|
||||
})
|
||||
)
|
||||
}
|
@ -11,6 +11,7 @@ import { Typebot } from 'bot-engine'
|
||||
import useSWR from 'swr'
|
||||
import { fetcher, sendRequest, toKebabCase } from './utils'
|
||||
import { deepEqual } from 'fast-equals'
|
||||
import { stringify } from 'qs'
|
||||
|
||||
export const useTypebots = ({
|
||||
folderId,
|
||||
@ -19,9 +20,7 @@ export const useTypebots = ({
|
||||
folderId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const params = new URLSearchParams(
|
||||
folderId ? { folderId: folderId.toString() } : undefined
|
||||
)
|
||||
const params = stringify({ folderId })
|
||||
const { data, error, mutate } = useSWR<{ typebots: Typebot[] }, Error>(
|
||||
`/api/typebots?${params}`,
|
||||
fetcher
|
||||
|
Reference in New Issue
Block a user