Skip to content

Commit

Permalink
IEN-937 | Add restriction to BCCNM spreadsheet file upload (#666)
Browse files Browse the repository at this point in the history
* add restriction to file upload

* use toast

* remove console eslint

---------

Co-authored-by: Jerry Wang <jerryappleid761208@gmail.com>
  • Loading branch information
jerry-ey and c1495616js authored Oct 17, 2024
1 parent 3d49957 commit 05bbbc2
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions apps/web/src/components/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ export const FileUploader = ({ extensions, handleClose, open, upload }: FileUplo
setFile(null);
}
const handleOnDrop = (files: File[]) => {
const ext = files[0]?.name.split('.').pop();
if (ext && extensions.includes(ext)) {
setFile(files[0]);
} else {
toast.warning(`Select a file of type: ${extensions.join(', ')}`);
const file = handleFileWithRestrictions(files[0], extensions);
if (file) {
setFile(file);
}
};

Expand Down Expand Up @@ -78,3 +76,27 @@ export const FileUploader = ({ extensions, handleClose, open, upload }: FileUplo
</Modal>
);
};

function handleFileWithRestrictions(file: File, extensions: string[] = []) {
try {
// file non-empty
// file extensions
// Dropzone would make file empty if extension is not correct, so we don't need to check for extension here
if (!file) {
throw new Error(`Select a file of type: ${extensions.join(', ')}`);
}

// file size < 6MB
if (file.size > 6 * 1024 * 1024) {
throw new Error('File is too large! Please select a file smaller than 6MB');
}

return file;
} catch (err: unknown) {
if (err instanceof Error) {
toast.warning(err.message);
} else {
toast.error(`An unknown error occurred: ${err}`);
}
}
}

0 comments on commit 05bbbc2

Please sign in to comment.