Skip to content

Commit

Permalink
Merge branch 'dev' into bugFix-standards-page
Browse files Browse the repository at this point in the history
  • Loading branch information
mauberti-bc authored Sep 18, 2024
2 parents 455d968 + 87a60fe commit 9a856f8
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 456 deletions.
101 changes: 35 additions & 66 deletions app/src/components/attachments/FileUploadWithMeta.tsx
Original file line number Diff line number Diff line change
@@ -1,84 +1,53 @@
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import ReportMetaForm, { IReportMetaForm } from 'components/attachments/ReportMetaForm';
import FileUpload, { IReplaceHandler } from 'components/file-upload/FileUpload';
import {
IFileHandler,
IOnUploadSuccess,
IUploadHandler,
UploadFileStatus
} from 'components/file-upload/FileUploadItem';
import { AttachmentType, AttachmentTypeFileExtensions } from 'constants/attachments';
import { UploadFileStatus } from 'components/file-upload/FileUploadItem';
import { FileUploadSingleItem } from 'components/file-upload/FileUploadSingleItem';
import { AttachmentTypeFileExtensions } from 'constants/attachments';
import { useFormikContext } from 'formik';
import React from 'react';

export interface IFileUploadWithMetaProps {
attachmentType: AttachmentType.REPORT | AttachmentType.KEYX | AttachmentType.CFG | AttachmentType.OTHER;
uploadHandler: IUploadHandler;
fileHandler?: IFileHandler;
onSuccess?: IOnUploadSuccess;
}
/**
* File upload with meta form. Used to upload a report with accompanying meta data.
*
* @return {*}
*/
export const FileUploadWithMeta = () => {
const { handleSubmit, setFieldValue, setFieldError, values, errors } = useFormikContext<IReportMetaForm>();

export const FileUploadWithMeta: React.FC<IFileUploadWithMetaProps> = (props) => {
const { handleSubmit, setFieldValue, errors } = useFormikContext<IReportMetaForm>();

const fileHandler: IFileHandler = (file) => {
const onFile = (file: File | null) => {
setFieldValue('attachmentFile', file);

props.fileHandler?.(file);
setFieldError('attachmentFile', '');
};

const replaceHandler: IReplaceHandler = () => {
setFieldValue('attachmentFile', null);
const onError = (error: string) => {
setFieldError('attachmentFile', error);
};

return (
<form onSubmit={handleSubmit}>
{props.attachmentType === AttachmentType.REPORT && (
<Box mb={3}>
<ReportMetaForm />
</Box>
)}
{props.attachmentType === AttachmentType.REPORT && (
<Box component="fieldset">
<Typography component="legend" variant="body1" id="report_upload">
Attach File
</Typography>
<FileUpload
uploadHandler={props.uploadHandler}
fileHandler={fileHandler}
onReplace={replaceHandler}
onSuccess={props.onSuccess}
dropZoneProps={{
maxNumFiles: 1,
multiple: false,
acceptedFileExtensions: AttachmentTypeFileExtensions.REPORT
}}
status={UploadFileStatus.STAGED}
replace={true}
/>
{errors?.attachmentFile && (
<Box>
{/* TODO is errors.attachmentFile correct here? (added `as string` to appease compile warning) */}
<Typography style={{ fontSize: '12px', color: '#f44336' }}>{errors.attachmentFile as string}</Typography>
</Box>
)}
</Box>
)}
{props.attachmentType === AttachmentType.KEYX && (
<FileUpload
uploadHandler={props.uploadHandler}
fileHandler={fileHandler}
onSuccess={props.onSuccess}
dropZoneProps={{
acceptedFileExtensions: AttachmentTypeFileExtensions.KEYX
<Box mb={3}>
<ReportMetaForm />
</Box>
<Box component="fieldset">
<Typography component="legend" variant="body1" id="report_upload">
Attach File
</Typography>
<FileUploadSingleItem
file={values.attachmentFile}
onFile={onFile}
onError={onError}
DropZoneProps={{
acceptedFileExtensions: AttachmentTypeFileExtensions.REPORT
}}
enableErrorDetails={true}
status={UploadFileStatus.STAGED}
/>
)}
{props.attachmentType === AttachmentType.OTHER && (
<FileUpload uploadHandler={props.uploadHandler} onSuccess={props.onSuccess} />
)}
{errors?.attachmentFile && (
<Box>
{/* TODO is errors.attachmentFile correct here? (added `as string` to appease compile warning) */}
<Typography style={{ fontSize: '12px', color: '#f44336' }}>{errors.attachmentFile as string}</Typography>
</Box>
)}
</Box>
</form>
);
};
Expand Down
84 changes: 0 additions & 84 deletions app/src/components/dialog/FileUploadDialog.tsx

This file was deleted.

87 changes: 87 additions & 0 deletions app/src/components/dialog/attachments/FileUploadDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import useTheme from '@mui/material/styles/useTheme';
import useMediaQuery from '@mui/material/useMediaQuery';
import { IDropZoneConfigProps } from 'components/file-upload/DropZone';
import FileUpload from 'components/file-upload/FileUpload';
import { IUploadHandler } from 'components/file-upload/FileUploadItem';
import { IComponentDialogProps } from '../ComponentDialog';

interface IFileUploadDialogProps extends IComponentDialogProps {
/**
* Set to `true` to open the dialog, `false` to close the dialog.
*
* @type {boolean}
* @memberof IFileUploadDialogProps
*/
open: boolean;
/**
* The title of the dialog.
*
* @type {string}
* @memberof IFileUploadDialogProps
*/
dialogTitle: string;
/**
* Callback fired when a file is added.
*
* @memberof IReportFileUploadDialogProps
*/
uploadHandler: IUploadHandler;
/**
* Callback fired when the dialog is closed.
*
* This function does not need to handle any errors, as the `FileUpload` component handles errors internally.
*
* @memberof IFileUploadDialogProps
*/
onClose: () => void;
/**
* Drop zone configuration properties.
*
* @type {IDropZoneConfigProps}
* @memberof IFileUploadDialogProps
*/
dropZoneProps?: IDropZoneConfigProps;
}

/**
* Wraps the standard `FileUpload` component in a dialog.
*
* The wrapped `FileUpload` component allows for drag-and-drop file uploads of any number of files with any file type.
*
* @param {IFileUploadDialogProps} props
* @return {*}
*/
export const FileUploadDialog = (props: IFileUploadDialogProps) => {
const { open, dialogTitle, uploadHandler, onClose, dropZoneProps } = props;

const theme = useTheme();
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));

if (!open) {
return null;
}

return (
<Dialog
fullScreen={fullScreen}
maxWidth="xl"
open={open}
aria-labelledby="file-upload-dialog-title"
aria-describedby="file-upload-dialog-description">
<DialogTitle id="file-upload-dialog-title">{dialogTitle}</DialogTitle>
<DialogContent>
<FileUpload uploadHandler={uploadHandler} dropZoneProps={dropZoneProps} />
</DialogContent>
<DialogActions>
<Button onClick={onClose} color="primary" variant="outlined">
Close
</Button>
</DialogActions>
</Dialog>
);
};
101 changes: 101 additions & 0 deletions app/src/components/dialog/attachments/FileUploadSingleItemDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { LoadingButton } from '@mui/lab';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import useTheme from '@mui/material/styles/useTheme';
import Typography from '@mui/material/Typography';
import useMediaQuery from '@mui/material/useMediaQuery';
import { IDropZoneConfigProps } from 'components/file-upload/DropZone';
import { UploadFileStatus } from 'components/file-upload/FileUploadItem';
import { FileUploadSingleItem } from 'components/file-upload/FileUploadSingleItem';
import { useEffect, useState } from 'react';

interface IFileUploadSingleItemDialog {
open: boolean;
dialogTitle: string;
uploadButtonLabel: string;
onUpload: (file: File) => Promise<void>;
onClose?: () => void;
dropZoneProps: Pick<IDropZoneConfigProps, 'acceptedFileExtensions' | 'maxFileSize'>;
}

/**
*
*
* @param {IFileUploadSingleItemDialog} props
* @return {*}
*/
export const FileUploadSingleItemDialog = (props: IFileUploadSingleItemDialog) => {
const { open, dialogTitle, uploadButtonLabel, onUpload, onClose, dropZoneProps } = props;

const theme = useTheme();
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));

const [currentFile, setCurrentFile] = useState<File | null>(null);
const [status, setStatus] = useState<UploadFileStatus>(UploadFileStatus.STAGED);
const [error, setError] = useState<string>('');
const [isUploading, setIsUploading] = useState<boolean>(false);

const isDisabled = !currentFile;

const handleUpload = () => {
if (!currentFile) {
return;
}

setIsUploading(true);
onUpload(currentFile).finally(() => setIsUploading(false));
};

useEffect(() => {
setCurrentFile(null);
}, [open]);

if (!open) {
return null;
}

return (
<Dialog
fullScreen={fullScreen}
maxWidth="xl"
open={open}
aria-labelledby="file-upload-dialog-title"
aria-describedby="file-upload-dialog-description">
<DialogTitle id="file-upload-dialog-title">{dialogTitle}</DialogTitle>
<DialogContent>
<FileUploadSingleItem
file={currentFile}
status={status}
onStatus={(status) => setStatus(status)}
onFile={(file) => {
setCurrentFile(file);
setError('');
}}
onError={(error) => setError(error)}
onCancel={() => {}}
DropZoneProps={dropZoneProps}
/>
<Typography variant="body2" color="error">
{error}
</Typography>
</DialogContent>
<DialogActions>
<LoadingButton
loading={isUploading}
disabled={isDisabled}
onClick={() => handleUpload()}
color="primary"
variant="contained"
autoFocus>
{uploadButtonLabel}
</LoadingButton>
<Button onClick={onClose} color="primary" variant="outlined" disabled={isUploading}>
Close
</Button>
</DialogActions>
</Dialog>
);
};
Loading

0 comments on commit 9a856f8

Please sign in to comment.