Skip to content

Commit

Permalink
Add JSDoc.
Browse files Browse the repository at this point in the history
Add fallback catch to report upload.
Fix error popup not displaying error message in body of dialog for the csv upload dialogs.
  • Loading branch information
NickPhura committed Sep 18, 2024
1 parent 6150161 commit 9f3dcdf
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
6 changes: 1 addition & 5 deletions app/src/components/attachments/FileUploadWithMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import { useFormikContext } from 'formik';
export const FileUploadWithMeta = () => {
const { handleSubmit, setFieldValue, setFieldError, values, errors } = useFormikContext<IReportMetaForm>();

Check warning on line 15 in app/src/components/attachments/FileUploadWithMeta.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/attachments/FileUploadWithMeta.tsx#L15

Added line #L15 was not covered by tests

const onStatus = (status: UploadFileStatus) => {
console.log(status);
};

const onFile = (file: File | null) => {

Check warning on line 17 in app/src/components/attachments/FileUploadWithMeta.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/attachments/FileUploadWithMeta.tsx#L17

Added line #L17 was not covered by tests
setFieldValue('attachmentFile', file);
setFieldError('attachmentFile', '');

Check warning on line 19 in app/src/components/attachments/FileUploadWithMeta.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/attachments/FileUploadWithMeta.tsx#L19

Added line #L19 was not covered by tests
};

const onError = (error: string) => {
Expand All @@ -37,7 +34,6 @@ export const FileUploadWithMeta = () => {
</Typography>
<FileUploadSingleItem
file={values.attachmentFile}
onStatus={onStatus}
onFile={onFile}
onError={onError}
DropZoneProps={{
Expand Down
30 changes: 30 additions & 0 deletions app/src/components/dialog/attachments/FileUploadDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,40 @@ 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;
}

Expand Down
11 changes: 8 additions & 3 deletions app/src/components/dialog/attachments/ReportFileUploadDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface IReportFileUploadDialogProps {
/**
* Callback fired if the dialog is submitted (user clicks 'Save' or 'Submit', etc).
*
* The function should handle any errors thrown.
*
* @memberof IReportFileUploadDialogProps
*/
onSubmit: (reportMeta: IReportMetaForm) => Promise<void>;
Expand Down Expand Up @@ -61,9 +63,12 @@ export const ReportFileUploadDialog: React.FC<IReportFileUploadDialogProps> = (p
const onSubmit = (values: IReportMetaForm) => {
setIsSubmitting(true);

Check warning on line 64 in app/src/components/dialog/attachments/ReportFileUploadDialog.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/dialog/attachments/ReportFileUploadDialog.tsx#L64

Added line #L64 was not covered by tests

props.onSubmit(values).finally(() => {
setIsSubmitting(false);
});
props

Check warning on line 66 in app/src/components/dialog/attachments/ReportFileUploadDialog.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/dialog/attachments/ReportFileUploadDialog.tsx#L66

Added line #L66 was not covered by tests
.onSubmit(values)
.catch() // Errors should be handled in the onSubmit function, squash errors here to prevent unhandled errors
.finally(() => {
setIsSubmitting(false);

Check warning on line 70 in app/src/components/dialog/attachments/ReportFileUploadDialog.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/components/dialog/attachments/ReportFileUploadDialog.tsx#L70

Added line #L70 was not covered by tests
});
};

if (!props.open) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Typography from '@mui/material/Typography';
import { FileUploadSingleItemDialog } from 'components/dialog/attachments/FileUploadSingleItemDialog';
import { SurveyAnimalsI18N } from 'constants/i18n';
import { DialogContext } from 'contexts/dialogContext';
import { APIError } from 'hooks/api/useAxios';
import { useBiohubApi } from 'hooks/useBioHubApi';
import { useSurveyContext } from 'hooks/useContext';
import { useContext, useState } from 'react';
Expand Down Expand Up @@ -37,11 +38,14 @@ export const AnimalListToolbar = (props: IAnimaListToolbarProps) => {
try {
await biohubApi.survey.importCrittersFromCsv(file, surveyContext.projectId, surveyContext.surveyId);
surveyContext.critterDataLoader.refresh(surveyContext.projectId, surveyContext.surveyId);
} catch (err: any) {
} catch (error) {
const apiError = error as APIError;

Check warning on line 42 in app/src/features/surveys/animals/list/components/AnimalListToolbar.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/features/surveys/animals/list/components/AnimalListToolbar.tsx#L42

Added line #L42 was not covered by tests

dialogContext.setErrorDialog({
dialogTitle: SurveyAnimalsI18N.importRecordsErrorDialogTitle,
dialogText: SurveyAnimalsI18N.importRecordsErrorDialogText,
dialogErrorDetails: [err.message],
dialogError: apiError.message,
dialogErrorDetails: apiError.errors,
open: true,
onClose: () => {
dialogContext.setErrorDialog({ open: false });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FileUploadSingleItemDialog } from 'components/dialog/attachments/FileUp
import { ObservationsTableI18N } from 'constants/i18n';
import { DialogContext } from 'contexts/dialogContext';
import { SurveyContext } from 'contexts/surveyContext';
import { APIError } from 'hooks/api/useAxios';
import { useBiohubApi } from 'hooks/useBioHubApi';
import { useContext, useState } from 'react';

Expand Down Expand Up @@ -89,11 +90,14 @@ export const ImportObservationsButton = (props: IImportObservationsButtonProps)
});

onSuccess?.();
} catch (apiError: any) {
} catch (error) {
const apiError = error as APIError;

Check warning on line 94 in app/src/features/surveys/observations/observations-table/import-obsevations/ImportObservationsButton.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/features/surveys/observations/observations-table/import-obsevations/ImportObservationsButton.tsx#L94

Added line #L94 was not covered by tests

dialogContext.setErrorDialog({
dialogTitle: ObservationsTableI18N.importRecordsErrorDialogTitle,
dialogText: ObservationsTableI18N.importRecordsErrorDialogText,
dialogErrorDetails: [apiError.message],
dialogError: apiError.message,
dialogErrorDetails: apiError.errors,
open: true,
onClose: () => {
dialogContext.setErrorDialog({ open: false });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FileUploadSingleItemDialog } from 'components/dialog/attachments/FileUp
import { ObservationsTableI18N } from 'constants/i18n';
import { DialogContext } from 'contexts/dialogContext';
import { SurveyContext } from 'contexts/surveyContext';
import { APIError } from 'hooks/api/useAxios';
import { useBiohubApi } from 'hooks/useBioHubApi';
import { useContext, useState } from 'react';

Expand Down Expand Up @@ -106,11 +107,14 @@ export const ImportObservationsButton = (props: IImportObservationsButtonProps)
});

onSuccess?.();
} catch (apiError: any) {
} catch (error) {
const apiError = error as APIError;

Check warning on line 111 in app/src/features/surveys/observations/sampling-sites/components/ImportObservationsButton.tsx

View check run for this annotation

Codecov / codecov/patch

app/src/features/surveys/observations/sampling-sites/components/ImportObservationsButton.tsx#L111

Added line #L111 was not covered by tests

dialogContext.setErrorDialog({
dialogTitle: ObservationsTableI18N.importRecordsErrorDialogTitle,
dialogText: ObservationsTableI18N.importRecordsErrorDialogText,
dialogErrorDetails: [apiError.message],
dialogError: apiError.message,
dialogErrorDetails: apiError.errors,
open: true,
onClose: () => {
dialogContext.setErrorDialog({ open: false });
Expand Down

0 comments on commit 9f3dcdf

Please sign in to comment.