2
0
Files
bot/apps/builder/layouts/results/SubmissionContent.tsx

139 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-01-04 15:50:56 +01:00
import { Stack, useToast, Flex } from '@chakra-ui/react'
import { ResultsActionButtons } from 'components/results/ResultsActionButtons'
2021-12-24 10:08:41 +01:00
import { SubmissionsTable } from 'components/results/SubmissionsTable'
import React, { useCallback, useMemo, useState } from 'react'
2022-01-04 15:50:56 +01:00
import {
convertResultsToTableData,
deleteAllResults,
deleteResults,
getAllResults,
useResults,
} from 'services/results'
import { unparse } from 'papaparse'
import { UnlockProPlanInfo } from 'components/shared/Info'
2021-12-30 10:24:16 +01:00
type Props = {
typebotId: string
totalResults: number
totalHiddenResults?: number
onDeleteResults: (total: number) => void
}
export const SubmissionsContent = ({
typebotId,
totalResults,
totalHiddenResults,
onDeleteResults,
}: Props) => {
2022-01-04 15:50:56 +01:00
const [selectedIndices, setSelectedIndices] = useState<number[]>([])
2022-01-04 09:15:33 +01:00
const [isDeleteLoading, setIsDeleteLoading] = useState(false)
2022-01-04 15:50:56 +01:00
const [isExportLoading, setIsExportLoading] = useState(false)
2021-12-30 10:24:16 +01:00
const toast = useToast({
position: 'top-right',
status: 'error',
})
const { data, mutate, setSize, hasMore } = useResults({
2021-12-30 10:24:16 +01:00
typebotId,
onError: (err) => toast({ title: err.name, description: err.message }),
})
const results = useMemo(() => data?.flatMap((d) => d.results), [data])
2022-01-04 15:50:56 +01:00
const handleNewSelection = (newSelectionIndices: number[]) => {
if (newSelectionIndices.length === selectedIndices.length) return
setSelectedIndices(newSelectionIndices)
2021-12-30 10:24:16 +01:00
}
2022-01-04 09:15:33 +01:00
const handleDeleteSelection = async () => {
setIsDeleteLoading(true)
2022-01-04 15:50:56 +01:00
const selectedIds = (results ?? [])
.filter((_, idx) => selectedIndices.includes(idx))
.map((result) => result.id)
const { error } =
totalSelected === totalResults
? await deleteAllResults(typebotId)
: await deleteResults(typebotId, selectedIds)
2022-01-04 09:15:33 +01:00
if (error) toast({ description: error.message, title: error.name })
else {
mutate(
totalSelected === totalResults
? []
: data?.map((d) => ({
results: d.results.filter((r) => !selectedIds.includes(r.id)),
}))
)
onDeleteResults(totalSelected)
}
2022-01-04 09:15:33 +01:00
setIsDeleteLoading(false)
}
const totalSelected =
selectedIndices.length > 0 && selectedIndices.length === results?.length
? totalResults - (totalHiddenResults ?? 0)
: selectedIndices.length
2021-12-24 10:08:41 +01:00
const handleScrolledToBottom = useCallback(
() => setSize((state) => state + 1),
[setSize]
)
2022-01-04 15:50:56 +01:00
const handleExportSelection = async () => {
setIsExportLoading(true)
const isSelectAll = totalSelected === totalResults
const dataToUnparse = isSelectAll
? await getAllTableData()
: tableData.filter((_, idx) => selectedIndices.includes(idx))
const csvData = new Blob([unparse(dataToUnparse)], {
type: 'text/csv;charset=utf-8;',
})
const fileName =
`typebot-export_${new Date().toLocaleDateString().replaceAll('/', '-')}` +
(isSelectAll ? `_all` : ``)
const tempLink = document.createElement('a')
tempLink.href = window.URL.createObjectURL(csvData)
tempLink.setAttribute('download', `${fileName}.csv`)
tempLink.click()
setIsExportLoading(false)
}
const getAllTableData = async () => {
const { data, error } = await getAllResults(typebotId)
if (error) toast({ description: error.message, title: error.name })
return convertResultsToTableData(data?.results)
}
const tableData: { [key: string]: string }[] = useMemo(
() => convertResultsToTableData(results),
// eslint-disable-next-line react-hooks/exhaustive-deps
2022-02-11 15:30:02 +01:00
[results]
2022-01-04 15:50:56 +01:00
)
2021-12-24 10:08:41 +01:00
return (
2022-02-11 15:30:02 +01:00
<Stack maxW="1200px" w="full" pb="28">
{totalHiddenResults && (
<UnlockProPlanInfo
buttonLabel={`Unlock ${totalHiddenResults} results`}
contentLabel="You are seeing complete submissions only."
/>
)}
2021-12-30 10:24:16 +01:00
<Flex w="full" justifyContent="flex-end">
2022-01-04 15:50:56 +01:00
<ResultsActionButtons
isDeleteLoading={isDeleteLoading}
isExportLoading={isExportLoading}
totalSelected={totalSelected}
onDeleteClick={handleDeleteSelection}
onExportClick={handleExportSelection}
/>
2021-12-30 10:24:16 +01:00
</Flex>
<SubmissionsTable
2022-01-04 15:50:56 +01:00
data={tableData}
onNewSelection={handleNewSelection}
onScrollToBottom={handleScrolledToBottom}
hasMore={hasMore}
/>
2021-12-30 10:24:16 +01:00
</Stack>
2021-12-24 10:08:41 +01:00
)
}