-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add download button to download responses
- Loading branch information
Showing
8 changed files
with
145 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
frontend/src/features/admin-form/responses/ResponsesPage/storage/StorageResponsesContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 16 additions & 10 deletions
26
...tend/src/features/admin-form/responses/ResponsesPage/storage/StorageResponsesProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../features/admin-form/responses/ResponsesPage/storage/UnlockedResponses/DownloadButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useCallback } from 'react' | ||
import { useMutation } from 'react-query' | ||
import { Box, MenuButton } from '@chakra-ui/react' | ||
|
||
import { BxsChevronDown } from '~assets/icons/BxsChevronDown' | ||
import { BxsChevronUp } from '~assets/icons/BxsChevronUp' | ||
import Badge from '~components/Badge' | ||
import Button from '~components/Button' | ||
import Menu from '~components/Menu' | ||
|
||
import { useStorageResponsesContext } from '../StorageResponsesContext' | ||
import useDecryptionWorkers, { | ||
DownloadEncryptedParams, | ||
} from '../useDecryptionWorkers' | ||
|
||
export const DownloadButton = (): JSX.Element => { | ||
const { downloadEncryptedResponses } = useDecryptionWorkers() | ||
const { downloadParams } = useStorageResponsesContext() | ||
|
||
const handleExportCsvMutation = useMutation( | ||
(params: DownloadEncryptedParams) => downloadEncryptedResponses(params), | ||
// TODO: add error and success handling | ||
) | ||
|
||
const handleExportCsvNoAttachments = useCallback(() => { | ||
if (!downloadParams) return | ||
return handleExportCsvMutation.mutate({ | ||
...downloadParams, | ||
downloadAttachments: false, | ||
}) | ||
}, [downloadParams, handleExportCsvMutation]) | ||
|
||
const handleExportCsvWithAttachments = useCallback(() => { | ||
if (!downloadParams) return | ||
return handleExportCsvMutation.mutate({ | ||
...downloadParams, | ||
downloadAttachments: true, | ||
}) | ||
}, [downloadParams, handleExportCsvMutation]) | ||
|
||
return ( | ||
<Box gridArea="export" justifySelf="flex-end"> | ||
<Menu placement="bottom-end"> | ||
{({ isOpen }) => ( | ||
<> | ||
<MenuButton | ||
as={Button} | ||
isDisabled={!downloadParams} | ||
isLoading={handleExportCsvMutation.isLoading} | ||
isActive={isOpen} | ||
aria-label="Download options" | ||
rightIcon={isOpen ? <BxsChevronUp /> : <BxsChevronDown />} | ||
> | ||
Download | ||
</MenuButton> | ||
<Menu.List> | ||
<Menu.Item onClick={handleExportCsvNoAttachments}> | ||
CSV only | ||
</Menu.Item> | ||
<Menu.Item onClick={handleExportCsvWithAttachments}> | ||
CSV with attachments | ||
<Badge ml="0.5rem" colorScheme="success"> | ||
beta | ||
</Badge> | ||
</Menu.Item> | ||
</Menu.List> | ||
</> | ||
)} | ||
</Menu> | ||
</Box> | ||
) | ||
} |
42 changes: 42 additions & 0 deletions
42
...atures/admin-form/responses/ResponsesPage/storage/UnlockedResponses/UnlockedResponses.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useMemo } from 'react' | ||
import { Box, Flex, Grid, Text } from '@chakra-ui/react' | ||
import simplur from 'simplur' | ||
|
||
import { useFormResponses } from '../../../queries' | ||
|
||
import { DownloadButton } from './DownloadButton' | ||
|
||
export const UnlockedResponses = (): JSX.Element => { | ||
const { data: { count } = {} } = useFormResponses() | ||
|
||
const prettifiedResponsesCount = useMemo(() => { | ||
if (!count) return | ||
return simplur` ${[count]}response[|s] to date` | ||
}, [count]) | ||
|
||
return ( | ||
<Flex flexDir="column" h="100%"> | ||
<Grid | ||
mb="1rem" | ||
alignItems="end" | ||
color="secondary.500" | ||
gridTemplateColumns={{ base: 'auto', md: 'auto 1fr' }} | ||
gridGap={{ base: '0.5rem', md: '1.5rem' }} | ||
gridTemplateAreas={{ | ||
base: "'submissions submissions' 'export'", | ||
md: "'submissions export'", | ||
}} | ||
> | ||
<Box gridArea="submissions"> | ||
<Text textStyle="h4"> | ||
<Text as="span" color="primary.500"> | ||
{count?.toLocaleString()} | ||
</Text> | ||
{prettifiedResponsesCount} | ||
</Text> | ||
</Box> | ||
<DownloadButton /> | ||
</Grid> | ||
</Flex> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
frontend/src/features/admin-form/responses/ResponsesPage/storage/UnlockedResponses/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { UnlockedResponses } from './UnlockedResponses' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters