From b7476d35f39bc09bb5201e39865cad23e029d5e6 Mon Sep 17 00:00:00 2001 From: Olava Faksdal <38139899+olavis@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:43:49 +0200 Subject: [PATCH] feat: add onChange function to fileuploader, closes #44 --- .../AddModelDialog/AddModelDialog.tsx | 12 --- src/features/FileUploader/FileUploader.tsx | 51 +++++++++---- .../ModelInputFilesTable.tsx | 73 ++++++++++++++----- 3 files changed, 90 insertions(+), 46 deletions(-) diff --git a/src/features/AddModelDialog/AddModelDialog.tsx b/src/features/AddModelDialog/AddModelDialog.tsx index 50f34fb1..b9c35172 100644 --- a/src/features/AddModelDialog/AddModelDialog.tsx +++ b/src/features/AddModelDialog/AddModelDialog.tsx @@ -16,17 +16,6 @@ export const AddModelDialog = ({ cancel, }: AddModelDialogProps) => { const [isFileDisplay, setFileDisplay] = useState(false) - const files = { - NC: { - name: 'CoarseSand_LargerFlow_1.nc', - size: 1.43, - onDelete: deleteInputFile, - }, - } - - // Temporary ignore because not implemented yet - // eslint-disable-next-line @typescript-eslint/no-empty-function - function deleteInputFile() {} function toggleINIFileContent() { setFileDisplay(!isFileDisplay) @@ -41,7 +30,6 @@ export const AddModelDialog = ({ ) => void + acceptType: 'NC' | 'INI' } -export const FileUploader = ({ file, INI }: FileUploaderProps) => { - const text = INI ? 'Select parameter INI file' : 'Select model NC file' - const accept = INI ? '.ini' : '.nc' +export const FileUploader = ({ + file, + onChange, + acceptType, +}: FileUploaderProps) => { + const INI = acceptType === 'INI' + const ref = useRef(null) + + function handleClick(e: ChangeEvent) { + e.preventDefault() + ref.current?.click() + } return file ? ( - <>{file.name} + <>{file} ) : ( - - {INI && ( - - )} - {text} {INI && '(optional)'} - - +
+ + {INI && ( + + )} + + {INI ? 'Select parameter INI file' : 'Select model NC file'} + + {INI && '(optional)'} + + +
) } diff --git a/src/features/ModelInputFilesTable/ModelInputFilesTable.tsx b/src/features/ModelInputFilesTable/ModelInputFilesTable.tsx index 92d28846..7cd1e9e1 100644 --- a/src/features/ModelInputFilesTable/ModelInputFilesTable.tsx +++ b/src/features/ModelInputFilesTable/ModelInputFilesTable.tsx @@ -1,53 +1,76 @@ import { Button, Table } from '@equinor/eds-core-react' import { delete_to_trash as deleteIcon } from '@equinor/eds-icons' +import { ChangeEvent, useState } from 'react' import IconButton from '../../components/IconButton/IconButton' import { FileUploader } from '../FileUploader/FileUploader' -// TODO: Temporary type -export type File = { name: string; size: number; onDelete: () => void } - type FileDisplay = { isVisible: boolean; toggle: () => void } -const FileColumn = ({ - file, - fileDisplay, -}: { +interface FileColumnProps { + onChange: (e: ChangeEvent) => void + onDelete: () => void + INI?: true file?: File fileDisplay?: FileDisplay -}) => { - const isINI = fileDisplay !== undefined +} +const FileColumn = ({ + onChange, + onDelete, + INI, + file, + fileDisplay, +}: FileColumnProps) => { const DeleteButton = ({ onDelete }: { onDelete: () => void }) => ( ) + function fileSize(size: number) { + if (size < 1024) { + return `${size} bytes` + } else if (size >= 1024 && size < 1048576) { + return `${(size / 1024).toFixed(1)} KB` + } else if (size >= 1048576) { + return `${(size / 1048576).toFixed(1)} MB` + } + } + return ( - + - + - {file && isINI && ( + {file && INI && ( )} - {file ? `${file.size} GB` : '-'} - - {file && } - + {file ? fileSize(file.size) : '-'} + {file && } ) } export const ModelInputFilesTable = ({ - files, fileDisplay, }: { - files: { NC?: File; INI?: File } fileDisplay: FileDisplay }) => { + const [files, setFiles] = useState<{ NC?: File; INI?: File }>() + + function updateFileDisplay(e: ChangeEvent) { + e.preventDefault() + const file = e.target.value + const type = e.target.name + setFiles({ ...files, [type]: file }) + } + return ( @@ -59,8 +82,18 @@ export const ModelInputFilesTable = ({ - - + setFiles({ ...files, NC: undefined })} + /> + setFiles({ ...files, INI: undefined })} + fileDisplay={fileDisplay} + />
)