-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip HAI-2066 File list for upload component
- Loading branch information
1 parent
d351dda
commit 3f58bab
Showing
8 changed files
with
259 additions
and
6 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
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,102 @@ | ||
import { useState } from 'react'; | ||
import { IconTrash, Notification } from 'hds-react'; | ||
import { Box } from '@chakra-ui/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useMutation } from 'react-query'; | ||
import { AttachmentMetadata } from '../../types/attachment'; | ||
import ConfirmationDialog from '../HDSConfirmationDialog/ConfirmationDialog'; | ||
import FileListItem from './FileListItem'; | ||
import { FileDeleteFunction, FileDownLoadFunction } from './types'; | ||
|
||
type Props = { | ||
files: AttachmentMetadata[]; | ||
fileDownLoadFunction?: FileDownLoadFunction; | ||
fileDeleteFunction: FileDeleteFunction; | ||
onFileDelete?: () => void; | ||
}; | ||
|
||
export default function FileList({ | ||
files, | ||
fileDownLoadFunction, | ||
fileDeleteFunction, | ||
onFileDelete, | ||
}: Props) { | ||
const { t } = useTranslation(); | ||
const deleteMutation = useMutation(fileDeleteFunction, { | ||
onSuccess() { | ||
onFileDelete && onFileDelete(); | ||
}, | ||
}); | ||
const [fileToDelete, setFileToDelete] = useState<AttachmentMetadata | null>(null); | ||
const [showFileDeleteDialog, setShowFileDeleteDialog] = useState(false); | ||
|
||
function handleFileDelete(file: AttachmentMetadata) { | ||
setFileToDelete(file); | ||
setShowFileDeleteDialog(true); | ||
} | ||
|
||
function confirmFileDelete() { | ||
deleteMutation.mutate(fileToDelete); | ||
setShowFileDeleteDialog(false); | ||
} | ||
|
||
function closeFileDeleteDialog() { | ||
setFileToDelete(null); | ||
setShowFileDeleteDialog(false); | ||
} | ||
|
||
function closeDeleteSuccessNotification() { | ||
setFileToDelete(null); | ||
deleteMutation.reset(); | ||
} | ||
|
||
return ( | ||
<> | ||
<Box as="ul" tabIndex={-1} marginTop="var(--spacing-m)"> | ||
{files.map((file) => { | ||
return ( | ||
<FileListItem | ||
fileMetadata={file} | ||
key={file.id} | ||
onDeleteFile={handleFileDelete} | ||
fileDownLoadFunction={fileDownLoadFunction} | ||
deletingFile={fileToDelete?.id === file.id && deleteMutation.isLoading} | ||
/> | ||
); | ||
})} | ||
</Box> | ||
|
||
{/* File remove dialog */} | ||
<ConfirmationDialog | ||
title={t('form:dialog:titles:removeAttachment')} | ||
description={t('form:dialog:descriptions:removeAttachment', { | ||
fileName: fileToDelete?.fileName, | ||
})} | ||
isOpen={showFileDeleteDialog} | ||
close={closeFileDeleteDialog} | ||
mainAction={confirmFileDelete} | ||
mainBtnLabel={t('common:buttons:remove')} | ||
mainBtnIcon={<IconTrash aria-hidden />} | ||
variant="danger" | ||
/> | ||
|
||
{/* Attachment delete success notification */} | ||
{deleteMutation.isSuccess && ( | ||
<Notification | ||
position="top-right" | ||
dismissible | ||
autoClose | ||
autoCloseDuration={3000} | ||
type="success" | ||
label={t('form:notifications:labels:attachmentRemoved')} | ||
closeButtonLabelText={t('common:components:notification:closeButtonLabelText')} | ||
onClose={closeDeleteSuccessNotification} | ||
> | ||
{t('form:notifications:descriptions:attachmentRemoved', { | ||
fileName: fileToDelete?.fileName, | ||
})} | ||
</Notification> | ||
)} | ||
</> | ||
); | ||
} |
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,33 @@ | ||
@import 'src/assets/styles/layout.scss'; | ||
|
||
.fileListItem { | ||
// display: flex; | ||
// align-items: flex-start; | ||
background-color: var(--color-info-light); | ||
border-bottom: 2px dotted var(--color-coat-of-arms); | ||
box-sizing: border-box; | ||
max-width: 100%; | ||
width: 600px; | ||
padding: var(--spacing-s) var(--spacing-2-xs); | ||
flex-wrap: wrap; | ||
|
||
// @include respond-above(l) { | ||
// flex-wrap: nowrap; | ||
// } | ||
} | ||
|
||
.fileListItemName { | ||
// word-break: break-word; | ||
hyphens: auto; | ||
} | ||
|
||
.fileListItemDownloadIcon { | ||
flex-shrink: 0; | ||
} | ||
|
||
.fileListItemButton { | ||
--file-list-item-button-y-offset: calc(-1 * var(--spacing-2-xs) - 2px); | ||
|
||
display: flex; | ||
margin: var(--file-list-item-button-y-offset) auto var(--file-list-item-button-y-offset) 0; | ||
} |
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,80 @@ | ||
import { Box, Flex } from '@chakra-ui/react'; | ||
import { format, isToday } from 'date-fns'; | ||
import { fi } from 'date-fns/locale'; | ||
import { Button, IconCross, IconDocument, IconDownload } from 'hds-react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { AttachmentMetadata } from '../../types/attachment'; | ||
import FileDownloadLink from '../fileDownloadLink/FileDownloadLink'; | ||
import Text from '../text/Text'; | ||
import styles from './FileListItem.module.scss'; | ||
import { FileDownLoadFunction } from './types'; | ||
|
||
type Props = { | ||
fileMetadata: AttachmentMetadata; | ||
onDeleteFile: (file: AttachmentMetadata) => void; | ||
fileDownLoadFunction?: FileDownLoadFunction; | ||
deletingFile?: boolean; | ||
}; | ||
|
||
export default function FileListItem({ | ||
fileMetadata, | ||
onDeleteFile, | ||
fileDownLoadFunction, | ||
deletingFile = false, | ||
}: Props) { | ||
const { t } = useTranslation(); | ||
const showDeleteButton = true; | ||
const dateAdded = new Date(fileMetadata.createdAt); | ||
const dateAddedText = isToday(dateAdded) | ||
? t('form:labels:today') | ||
: format(dateAdded, 'd.M.yyyy', { locale: fi }); | ||
|
||
return ( | ||
<Flex | ||
as="li" | ||
tabIndex={-1} | ||
className={styles.fileListItem} | ||
alignItems="flex-start" | ||
gridColumnGap="var(--spacing-xs)" | ||
gridRowGap="var(--spacing-2-xs)" | ||
mb="var(--spacing-xs)" | ||
> | ||
<IconDocument aria-hidden /> | ||
<Flex wrap="nowrap" flex="1" alignItems="flex-start"> | ||
{fileDownLoadFunction === undefined ? ( | ||
<Text tag="p" className={styles.fileListItemName}> | ||
{fileMetadata.fileName} | ||
</Text> | ||
) : ( | ||
<> | ||
<FileDownloadLink | ||
linkText={fileMetadata.fileName} | ||
fileName={fileMetadata.fileName} | ||
linkTextStyles={styles.fileListItemName} | ||
queryKey={['attachmentContent', fileMetadata.id]} | ||
queryFunction={() => fileDownLoadFunction(fileMetadata)} | ||
/> | ||
<IconDownload aria-hidden className={styles.fileListItemDownloadIcon} /> | ||
</> | ||
)} | ||
</Flex> | ||
<Box as="p" color="var(--color-black-60)" className="text-sm"> | ||
{t('form:labels:added')} {dateAddedText} | ||
</Box> | ||
{showDeleteButton && ( | ||
<Button | ||
className={styles.fileListItemButton} | ||
iconLeft={<IconCross aria-hidden />} | ||
variant="supplementary" | ||
size="small" | ||
theme="black" | ||
onClick={() => onDeleteFile(fileMetadata)} | ||
data-testid={`delete-${fileMetadata.id}`} | ||
isLoading={deletingFile} | ||
> | ||
{t('common:buttons:remove')} | ||
</Button> | ||
)} | ||
</Flex> | ||
); | ||
} |
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
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,5 @@ | ||
import { AttachmentMetadata } from '../../types/attachment'; | ||
|
||
export type FileDownLoadFunction = (file: AttachmentMetadata) => Promise<string>; | ||
|
||
export type FileDeleteFunction = (file: AttachmentMetadata | null) => Promise<void>; |
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