Skip to content

Commit

Permalink
Fix errors when file is changed after selection (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
theosanderson authored Feb 8, 2024
1 parent 7733ab3 commit e6dc7f9
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions website/src/components/DataUploadForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DatePicker } from '@mui/x-date-pickers';
import { isErrorFromAlias } from '@zodios/core';
import type { AxiosError } from 'axios';
import { type DateTime } from 'luxon';
import { type ChangeEvent, type FormEvent, useMemo, useState } from 'react';
import { type ChangeEvent, type FormEvent, useMemo, useState, useRef, useEffect } from 'react';

import { withLocalizationProvider, withQueryProvider } from './common/withProvider.tsx';
import { getClientLogger } from '../clientLogger.ts';
Expand Down Expand Up @@ -46,6 +46,8 @@ const InnerDataUploadForm = ({
}: DataUploadFormProps) => {
const [metadataFile, setMetadataFile] = useState<File | null>(null);
const [sequenceFile, setSequenceFile] = useState<File | null>(null);
const metadataFileInputRef = useRef<HTMLInputElement>(null);
const sequenceFileInputRef = useRef<HTMLInputElement>(null);

const { zodiosHooks } = useGroupManagementClient(clientConfig);
const groupsOfUser = zodiosHooks.useGetGroupsOfUser({
Expand Down Expand Up @@ -114,6 +116,34 @@ const InnerDataUploadForm = ({
// window.location.href = routes.userSequenceReviewPage(organism);
};

useEffect(() => {
const interval = setInterval(() => {
// Check for files which are no longer readable - which generally indicates the file has been edited since being
// selected in the UI - and clear these.
metadataFile
?.slice(0, 1)
.arrayBuffer()
.catch(() => {
setMetadataFile(null);
if (metadataFileInputRef.current) {
metadataFileInputRef.current.value = '';
}
});

sequenceFile
?.slice(0, 1)
.arrayBuffer()
.catch(() => {
setSequenceFile(null);
if (sequenceFileInputRef.current) {
sequenceFileInputRef.current.value = '';
}
});
}, 500);

return () => clearInterval(interval);
}, [metadataFile, sequenceFile]);

return (
<form onSubmit={handleSubmit} className='p-6 space-y-6 max-w-md w-full'>
{action === 'submit' &&
Expand Down Expand Up @@ -152,7 +182,11 @@ const InnerDataUploadForm = ({
placeholder='Metadata File:'
size='small'
type='file'
onChange={(event: ChangeEvent<HTMLInputElement>) => setMetadataFile(event.target.files?.[0] || null)}
inputRef={metadataFileInputRef}
onChange={(event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0] || null;
setMetadataFile(file);
}}
disabled={false}
InputLabelProps={{
shrink: true,
Expand All @@ -166,7 +200,11 @@ const InnerDataUploadForm = ({
placeholder='Sequences File:'
size='small'
type='file'
onChange={(event: ChangeEvent<HTMLInputElement>) => setSequenceFile(event.target.files?.[0] || null)}
inputRef={sequenceFileInputRef}
onChange={(event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0] || null;
setSequenceFile(file);
}}
disabled={false}
InputLabelProps={{
shrink: true,
Expand Down

0 comments on commit e6dc7f9

Please sign in to comment.