2
0

🪥 Consult submissions

This commit is contained in:
Baptiste Arnaud
2021-12-30 10:24:16 +01:00
parent f088f694b9
commit 1093453c07
25 changed files with 575 additions and 138 deletions

View File

@ -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

View File

@ -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)

View 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',
})
)
}

View File

@ -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